local export = {}

local lang = require("Module:languages").getByCode("mn")
local mn = require("Module:mn-common")

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 lang:findBestScript(args["head"] or mw.title.getCurrentTitle().subpageText)
end
	
local function otherscript(inflections, args, sc, categories)
	if #args[1] < 1 then
		if sc then
			if sc:getCode() == "Mong" then
			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 =  require("Module:scripts").getByCode(scripts[args[1][1]])
		table.remove(args[1], 1)
		require("Module:debug").track("mn-headword/script-param")
	else
		other_sc = lang:findBestScript(args[1][1])
	end
	
	if sc and sc:getCode() == other_sc:getCode() then
		error("The headword and the alternative spelling should be in different scripts.")
	end
	
	local spelling = {label = "อักษร" .. 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)
			table.insert(categories, arg .. lang:getCategoryName())
		end
	end
	if (args["head"] or mw.title.getCurrentTitle().subpageText):find(" ") then
		table.insert(categories, lang:getCanonicalName() .. " multiword terms")
	elseif mw.ustring.sub( mw.title.getCurrentTitle().subpageText, 1, 1 ) ~= "-" and get_script(args):getCode() == "Cyrl" then
		--table.insert(categories, lang:getCanonicalName() .. " " .. #mn.syllables( mw.title.getCurrentTitle().subpageText ) .. "-syllable words" )
		table.insert(categories, "ศัพท์" .. lang:getCategoryName() .. "ที่มี " .. #mn.syllables( mw.title.getCurrentTitle().subpageText ) .. " พยางค์" )
	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"]}, 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 = "พหูพจน์ชี้เฉพาะ"}
		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"
			table.insert(categories, lang:getCanonicalName() .. " regular declension nouns")
		elseif args["dec"] == "n" then
			declension = declension .. "hidden-n declension"
			table.insert(categories, lang:getCanonicalName() .. " hidden-n declension nouns")
		elseif args["dec"] == "g" then
			declension = declension .. "hidden-g declension"
			table.insert(categories, lang:getCanonicalName() .. " hidden-g declension nouns")
		elseif args["dec"] == "m" then
			declension = declension .. "mixed declension"
			table.insert(categories, lang:getCanonicalName() .. " mixed declension nouns")
		else
			--error("non-existent declension")
		end
		declension = declension .. ")</i></small>"
	end
	
	local data = {lang = lang, sc = sc, heads = {args["head"]}, genders = nil, inflections = inflections, pos_category = "คำนาม", 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 = "การิตวาจก"}
		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 = "กรรมวาจก"}
		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"]}, genders = nil, inflections = inflections, pos_category = "คำกริยา", categories = categories, sort_key = nil}
	return require("Module:headword").full_headword(data)
end


return export