Documentation for this module may be created at Modul:ziman/şablon/belge

local export = {}

local b = require("Module:bingeh")

--[[	If language is an etymology language, iterates through parent languages
		until it finds a non-etymology language. ]]
function export.navebine(frame)
    local args = frame:getParent().args
    local lang  = args[1]
	local lang = require("Modul:ziman").getByCode(lang, "allow etym", "allow family")
	local non_etym_lang = require("Modul:ziman").getNonEtymological(lang)

	return non_etym_lang
end

function export.exists(frame)
	local args = frame.args
	local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
	
	lang = require("Modul:ziman").getByCode(lang)
	
	if lang then
		return "1"
	else
		return ""
	end
end

-- Used by the following JS:
-- * [[WT:ACCEL]]
-- * [[WT:EDIT]]
-- * [[WT:NEC]]
function export.getByCode(frame)
	local iparams = {
		[1] = {required = true},
		[2] = {required = true},
		[3] = {},
		[4] = {},
		[5] = {},
	}
	
	local iargs = require("Module:parameters").process(frame.args, iparams)
	local langcode = iargs[1]
	
	local lang = require("Modul:ziman").getByCode(langcode, true)
	
	return require("Module:language-like").templateGetByCode(lang, iargs,
		function(itemname)
			local list
			if itemname == "getWikimediaLanguages" then
				list = lang:getWikimediaLanguages()
			elseif itemname == "getScripts" then
				list = lang:getScriptCodes()
			elseif itemname == "getAncestors" then
				list = lang:getAncestors()
			end
			if list then
				local index = iargs[3]
				index = tonumber(index) or error("Please specify the numeric index of the desired item.")
				local retval = list[index]
				if retval then
					if type(retval) == "string" then
						return retval
					else
						return retval:getCode()
					end
				else
					return ""
				end
			end
			if itemname == "transliterate" then
				local text = iargs[3]
				local sc = iargs[4]
				local module_override = iargs[5]
				sc = sc and require("Module:scripts").getByCode(sc, 4) or nil
				return lang:transliterate(text, sc, module_override) or ""
			elseif itemname == "makeEntryName" then
				local text = iargs[3]
				return lang:makeEntryName(text) or ""
			elseif itemname == "makeSortKey" then
				local text = iargs[3]
				return lang:makeSortKey(text) or ""
			elseif itemname == "countCharacters" then
				local text = args[3] or ""
				local sc = require("Module:scripts").getByCode(iargs[4], 4)
				return sc:countCharacters(text)
			end
		end
	)
end

function export.getByCanonicalName(frame)
	local args = frame.args
	local langname = args[1] or error("Language name has not been specified. Please pass parameter 1 to the module invocation.")
	
	local lang = require("Modul:ziman").getByCanonicalName(langname)
	
	if lang then
		return lang:getCode()
	else
		return ""
	end
end

function export.getByName(frame)
	local args = frame.args
	local langname = args[1] or error("Language name has not been specified. Please pass parameter 1 to the module invocation.")
	
	local lang = require("Modul:ziman").getByName(langname)
	
	if lang then
		return lang:getCode()
	else
		return ""
	end
end

function export.makeEntryName(frame)
	local args = frame.args
	local langname = args[1] or error("Language name has not been specified. Please pass parameter 1 to the module invocation.")
	
	local lang = require("Modul:ziman").getByCode(langname)
	
	if lang then
		return lang:makeEntryName(args[2])
	else
		return ""
	end
end

function export.getCanonicalName(frame)
	local langCode, args
	if require("Module:yesno")(frame.args.parent) then
		args = frame:getParent().args
	else
		args = frame.args
	end
	langCode = args[1]
	
	if not langCode or langCode == "" or (mw.loadData("Modul:ziman/ji kod li nav")[langCode] == nil) then
		return "<span style='color:red;font-size:85%;'>'''Ji kerema xwe koda zimanî lê zêde bikin.'''</span>"
		.. b.bike_kategori_naverok('Rûpelên bi çewtiyên koda zimanî')
	else 
		return mw.loadData("Modul:ziman/ji kod li nav")[langCode]
	end
	
end

return export