ထာမ်ပလိက်တဏအ်ဒးနွံကဵုလိက်စၟတ်သမ္တီမချူလဝ်သာဓက။
သ္ပဂုန်တုဲထာမ်ပလိက်တဏအ်ဒးစၟတ်သမ္တီကဵုလိက်မချူလဝ်နူကဵုဓမံက်ထ္ၜးကဵုဗီုပြၚ်ကိစ္စမရန်တၟအ်လဝ် ကဵု ပရေၚ်မရပ်စပ်လ္တူမုက်လိက်နကဵုလိက်စၟတ်သမ္တီမချူလဝ်သာဓက

local export = {}
local pos_functions = {}

local force_cat = false -- for testing; if true, categories appear in non-mainspace pages

local lang = require("Module:languages").getByCode("zlw-ocs")
local langname = "ချက်ခ်တြေံ"

-- Table of all valid genders.
local valid_genders = {
	["mfbysense"] = true,
	["mfbysense-p"] = true,
	["m-pr"] = true,
	["m-anml"] = true,
	["m-in"] = true,
	["m-d"] = true,
	["m-p"] = true,
	["f"] = true,
	["f-d"] = true,
	["f-p"] = true,
	["n"] = true,
	["n-d"] = true,
	["n-p"] = true,
	["?"] = true,
	["?-d"] = true,
	["?-p"] = true,
}

-- Table of all valid aspects.
local valid_aspects = {
	["impf"] = true,
	["pf"] = true,
	["both"] = true,
	["biasp"] = true,
	["?"] = true,
}

local rfind = mw.ustring.find

local function track(track_id)
	require("Module:debug/track")("zlw-ocs-headword/" .. track_id)
	return true
end

local function glossary_link(anchor, text)
	text = text or anchor
	return "[[Appendix:Glossary#" .. anchor .. "|" .. text .. "]]"
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local iparams = {
		[1] = {required = true},
		["def"] = {},
	}
	local iargs = require("Module:parameters").process(frame.args, iparams)
	local args = frame:getParent().args
	local poscat = iargs[1]
	local def = iargs.def

	local parargs = frame:getParent().args

	local params = {
		["head"] = {list = true},
		["id"] = {},
		["sort"] = {},
		["splithyph"] = {type = "boolean"},
		["nolinkhead"] = {type = "boolean"},
		["json"] = {type = "boolean"},
		["pagename"] = {}, -- for testing
	}

	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end

    local args = require("Module:parameters").process(parargs, params)

	local pagename = args.pagename or mw.title.getCurrentTitle().text

	local data = {
		lang = lang,
		pos_category = poscat,
		categories = {},
		heads = args.head,
		genders = {},
		inflections = {},
		pagename = pagename,
		id = args.id,
		sort_key = args.sort,
		force_cat_output = force_cat,
	}

	local is_suffix = false
	if pagename:find("^%-") and poscat ~= "suffix forms" then
		is_suffix = true
		data.pos_category = "အဆက်လက္ကရဴ"
		local singular_poscat = require("Module:string utilities").singularize(poscat)

		table.insert(data.inflections, {label = singular_poscat .. "-forming suffix"})
	end

	if pos_functions[poscat] then
		pos_functions[poscat].func(def, args, data, is_suffix)
	end

	-- mw.ustring.toNFD performs decomposition, so letters that decompose
	-- to an ASCII vowel and a diacritic, such as é, are counted as vowels and
	-- do not need to be included in the pattern.
	if not pagename:find("[ %-]") and not rfind(mw.ustring.lower(mw.ustring.toNFD(pagename)), "[aeiouyæœø]") then
		table.insert(data.categories, langname .. " words without vowels")
	end

    if args.json then
        return require("Module:JSON").toJSON(data)
    end
	
	return require("Module:headword").full_headword(data)
end

local function get_noun_params(is_proper)
	params = {
		[1] = {alias_of = "g"},
		["indecl"] = {type = "boolean"},
	}
	local function insert_list_param(arg)
		params[arg] = {list = true}
		params[arg .. "_qual"] = {list = arg .. "=_qual", allow_holes = true}
	end
	insert_list_param("g")
	insert_list_param("m")
	insert_list_param("f")
	insert_list_param("adj")
	insert_list_param("dim")
	insert_list_param("aug")
	insert_list_param("pej")
	insert_list_param("dem")
	insert_list_param("fdem")
	return params
end

local function do_nouns(is_proper, def, args, data, is_suffix)
	for i, g in ipairs(args.g) do
		if not valid_genders[g] then
			error("Unrecognized gender: '" .. g .. "'")
		end
		-- mfbysense should always be animate so add that
		if g == "mfbysense" then
			g = "mfbysense-an"
		elseif g == "mfbysense-p" then
			g = "mfbysense-an-p"
		end
		track("gender-" .. g)
		if args.g_qual[i] then
			table.insert(data.genders, {spec = g, qualifiers = {args.g_qual[i]}})
		else
			table.insert(data.genders, g)
		end
	end
	if args.indecl then
		table.insert(data.inflections, {label = glossary_link("ကၞိက်ဟွံမာန်")})
		table.insert(data.categories, "နာမ်" .. langname .. "ပါ်ပါဲထောံဟွံဂွံဂမၠိုင်")
	end
	local function handle_infl(arg, label)
		local vals = args[arg]
		local quals = args[arg .. "_qual"]
		if #vals > 0 then
			local inflections = {}
			for i, val in ipairs(vals) do
				table.insert(inflections, {term = val, q = quals and quals[i] and {quals[i]} or nil})
			end
			inflections.label = label
			table.insert(data.inflections, inflections)
		end
	end
	
	handle_infl("m", "ပုလ္လိၚ်")
	handle_infl("f", "ဣတ္တိလိၚ်")
	handle_infl("adj", "related adjective")
	handle_infl("dim", "diminutive")
	handle_infl("aug", "augmentative")
	handle_infl("pej", "pejorative")
	handle_infl("dem", "demonym")
	handle_infl("fdem", "female demonym")
end

pos_functions["နာမ်"] = {
	 params = get_noun_params(false),
	 func = function(def, args, data, is_suffix)
	 	return do_nouns(false, def, args, data, is_suffix)
	 end,
}

pos_functions["နာမ်မကိတ်ညဳ"] = {
	 params = get_noun_params("proper noun"),
	 func = function(def, args, data, is_suffix)
	 	return do_nouns("proper noun", def, args, data, is_suffix)
	 end,
}

pos_functions["ကြိယာ"] = {
	params = {
		["a"] = {default = "?"},
		["pf"] = {list = true},
		["impf"] = {list = true},
	},
	func = function(def, args, data, is_suffix)
		if not valid_aspects[args.a] then
			error("Unrecognized aspect: '" .. args.a .. "'")
		end
		data.genders = args.a == "both" and {"biasp"} or {args.a}
	
		local pf = args.pf
		if #pf > 0 then
			pf.label = "perfective"
			table.insert(data.inflections, pf)
		end
		local impf = args.impf
		if #impf > 0 then
			impf.label = "imperfective"
			table.insert(data.inflections, impf)
		end
	end,
}

pos_functions["နာမဝိသေသန"] = {
	params = {
		[1] = {list = "comp"},
		[2] = {list = "sup"},
		["s"] = {list = true},
		["sc"] = {list = true},
		["ss"] = {list = "ss"},
		["adv"] = {list = true},
		["indecl"] = {type = "boolean"},
	},
	func = function(def, args, data, is_suffix)
		if args.indecl then
			table.insert(data.inflections, {label = glossary_link("ကၞိက်ဟွံမာန်")})
			table.insert(data.categories, "နာမဝိသေသန" .. langname .. "ပါ်ပါဲထောံဟွံဂွံဂမၠိုင်")
		end
		if #args.s > 0 then
			args.s.label = "short"
			table.insert(data.inflections, args.s)
		end
		if args[1][1] == "-" then
			table.insert(data.inflections, {label = "ပတုပ်ရံၚ်ဟွံမာန်"})
			table.insert(data.categories, "နာမဝိသေသန" .. langname .. "မတော်ဟွံဂွံဂမၠိုင်")
		elseif #args[1] > 0 then
			local comp = args[1]
			local sup = args[2]
			if #sup == 0 then
				for _, c in ipairs(comp) do
					table.insert(sup, "naj" .. c)
				end
			end
			if #args.ss == 0 then
				for _, c in ipairs(args.sc) do
					table.insert(args.ss, "naj" .. c)
				end
			end 
			comp.label = "comparative"
			comp.accel = {form = "comparative"}
			sup.label = "superlative"
			sup.accel = {form = "superlative"}
			table.insert(data.inflections, comp)
			if #args.sc > 0 then
			args.sc.label = "short comparative"
			table.insert(data.inflections, args.sc)
			end
			table.insert(data.inflections, sup)
			if #args.sc > 0 then
			args.ss.label = "short superlative"
			table.insert(data.inflections, args.ss)
			end
			table.insert(data.categories, langname .. " comparable adjectives")
		end
		if #args.adv > 0 then
			args.adv.label = "adverb"
			table.insert(data.inflections, args.adv)
		end
	end,
}

return export