local export = {}

local lang = require("Module:languages").getByCode("mn")
local m_scripts = require("Module:scripts")

local params = {
	["head"] = {}, --with no diacritics I don't think we'll need more than one head
	["cat"] = {list = true},
	[1] = {list = true}
}

local function get_script(args) 
	return m_scripts.findBestScript(args["head"] or mw.title.getCurrentTitle().subpageText, lang)
end
	
local function otherscript(inflections, args, sc, categories)
	if #args[1] < 1 then
		if sc then
			if sc:getCode() == "Mong" then
				table.insert(categories, "Mongolian terms without Cyrillic script")
			elseif sc:getCode() == "Cyrl" then
				table.insert(categories, "Mongolian terms without Mongolian script")
			end
		end
		return 
	end

	local other_sc
	
	local scripts = {
		m = "Mong",
		c = "Cyrl",
		Mong = true,
		Cyrl = true,
	}
	if scripts[args[1][1]] then
		other_sc =  m_scripts.getByCode(scripts[args[1][1]])
		table.remove(args[1], 1)
		require("Module:debug").track("mn-headword/script-param")
	else
		other_sc = m_scripts.findBestScript(args[1][1], lang)
	end
	
	if sc and sc:getCode() == other_sc:getCode() then
		error("The headword and the alternative spelling should be in different scripts.")
	end
	
	if not scripts[other_sc:getCode()] then
		error("Mongolian entries in " .. other_sc:getCanonicalName() .. " script are not currently allowed.")
	end
	
	local spelling = {label = "ejaan " .. other_sc:getCanonicalName(), sc = other_sc, enable_auto_translit=true}
	
	for i, arg in ipairs(args[1]) do
		table.insert(spelling, arg)
	end
	
	table.insert(inflections, spelling)
end

local function categorize(args)
	local cats = args["cat"]
	local categories = {}
	if cats then
		for i, arg in ipairs(cats) do
			table.insert(categories, lang:getCanonicalName() .. " " .. arg)
		end
	end
	if (args["head"] or mw.title.getCurrentTitle().subpageText):find(" ") then
		table.insert(categories, lang:getCanonicalName() .. " multiword terms")
	end
	return categories
end

function export.basic(frame)
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local inflections = {}
	local categories = categorize(args)
	
	local sc = get_script(args)
	
	otherscript(inflections, args, sc, categories)
	
	local data = {lang = lang, sc = sc, heads = {args["head"] or ""}, genders = nil, inflections = inflections, pos_category = frame.args[1], categories = categories, sort_key = nil}
	return require("Module:headword").full_headword(data)
end

function export.noun(frame)

	params["pl"] = {list = true}
	params["dec"] = {}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local sc = get_script(args)
	
	local inflections = {}
	
	local categories = categorize(args)
	
	local declension = ""
	
	otherscript(inflections, args, sc, categories)
	
	if #args["pl"] > 0 then
		local plurals = {label = "plural"}
		for i, pl in ipairs(args["pl"]) do
			table.insert(plurals, pl)
		end
		table.insert(inflections, plurals)
	end

		
	if args["dec"] then
		declension = "; <small><i>("
		if args["dec"] == "r" then
			declension = declension .. "regular declension [[Kategori:Kata nama deklensi nalar bahasa Mongolia]]"
		elseif args["dec"] == "n" then
			declension = declension .. "hidden-n declension [[Kategori:Kata nama n tersembunyi bahasa Mongolia]]"
		elseif args["dec"] == "g" then
			declension = declension .. "hidden-g declension [[Kategori:Kata nama g tersembunyi bahasa Mongolia]]"
		elseif args["dec"] == "m" then
			declension = declension .. "mixed declension [[Kategori:Kata nama deklensi campuran bahasa Mongolia]]"
		else
			--error("non-existent declension")
		end
		declension = declension .. ")</i></small>"
	end
	
	local data = {lang = lang, sc = sc, heads = {args["head"] or ""}, genders = nil, inflections = inflections, pos_category = "kata nama", categories = categories, sort_key = nil}
	return require("Module:headword").full_headword(data) .. declension
end


function export.verb(frame)
	params["caus"] = {list = true}
	params["pass"] = {list = true}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	
	local inflections = {}
	local categories = categorize(args)
	
	local sc = get_script(args)
	
	otherscript(inflections, args, sc, categories)
	
	if #args["caus"] > 0 then
		causatives = {label = "causative"}
		for i, x in ipairs(args["caus"]) do
			table.insert(causatives, x)
		end
		table.insert(inflections, causatives)
	end
	
	if #args["pass"] > 0 then
		passives = {label = "passive"}
		for i, x in ipairs(args["pass"]) do
			table.insert(passives, x)
		end
		table.insert(inflections, passives)
	end

	local data = {lang = lang, sc = sc, heads = {args["head"] or ""}, genders = nil, inflections = inflections, pos_category = "verbs", categories = categories, sort_key = nil}
	return require("Module:headword").full_headword(data)
end


return export