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

local export = {}

local pos_functions = {}
local rfind = mw.ustring.find
local rmatch = mw.ustring.match
local rsplit = mw.text.split

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

local function is_not_empty(term)
	return term ~= nil and term ~= ''
  end

local suffix_categories = {["nouns"] = true}

local function track(page)
	require("Module:debug").track("klj-headword/" .. page)
	return true
end

local function add_space_word_links(space_word, split_dash)
	local space_word_no_punct, punct = rmatch(space_word, "^(.*)([,;:?!])$")
	space_word_no_punct = space_word_no_punct or space_word
	punct = punct or ""
	local words
	-- don’t split prefixes and suffixes
	if not split_dash or rfind(space_word_no_punct, "^%-") or
		rfind(space_word_no_punct, "%-$") then
		words = {space_word_no_punct}
	else
		words = rsplit(space_word_no_punct, "%-")
	end
	local linked_words = {}
	for _, word in ipairs(words) do
		word = "[[" .. word .. "]]"
		table.insert(linked_words, word)
	end
	return table.concat(linked_words, "-") .. punct
end

local function add_lemma_links(lemma, split_dash)
	if not rfind(lemma, " ") then split_dash = true end
	local words = rsplit(lemma, " ")
	local linked_words = {}
	for _, word in ipairs(words) do
		table.insert(linked_words, add_space_word_links(word, split_dash))
	end
	local retval = table.concat(linked_words, " ")
	-- If we ended up with a single link consisting of the entire lemma,
	-- remove the link.
	local unlinked_retval = rmatch(retval, "^%[%[([^%[%]]*)%]%]$")
	return unlinked_retval or retval
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local PAGENAME = mw.title.getCurrentTitle().text

	local poscat = frame.args[1] or error(
					   "Part of speech has not been specified. Please pass parameter 1 to the module invocation.")

	local params = {
		["head"] = {list = true},
		["suff"] = {type = "boolean"},
		["splitdash"] = {alias_of = "splithyph", type = "boolean"},
		["splithyph"] = {type = "boolean"},
		["nolinkhead"] = {type = "boolean"}
	}

	if rfind(PAGENAME, " ") then track("space") end

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

	local parargs = frame:getParent().args
	if parargs.splitdash then track("splitdash") end
	local args = require("Module:parameters").process(parargs, params)

	local heads = args["head"]
	if pos_functions[poscat] and pos_functions[poscat].param1_is_head and
		args[1] then table.insert(heads, 1, args[1]) end
	if args.nolinkhead then
		if #heads == 0 then heads = {PAGENAME} end
	else
		local auto_linked_head = add_lemma_links(PAGENAME, args["splithyph"])
		if #heads == 0 then
			heads = {auto_linked_head}
		else
			for _, head in ipairs(heads) do
				if head == auto_linked_head then
					track("redundant-head")
				end
			end
		end
	end

	local data = {
		lang = lang,
		pos_category = poscat,
		categories = {},
		heads = heads,
		genders = {},
		inflections = {}
	}

	if args["suff"] then
		data.pos_category = "အဆက်လက္ကရဴ"

		if suffix_categories[poscat] then
			local singular_poscat = poscat:gsub("s$", "")
			table.insert(data.categories, lang:getCanonicalName() .. " " ..
							 singular_poscat .. "-forming suffixes")
		else
			error("No category exists for suffixes forming " .. poscat .. ".")
		end
	end

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

	return require("Module:headword").full_headword(data)
end

local function get_noun_pos(is_proper)
	return {
		params = {[1] = {}, [2] = {}, ["stem"] = {}, ["nb"] = {}, ["sing"] = {}},
		func = function(args, data)
			local PAGENAME = mw.title.getCurrentTitle().text
			local script = lang:findBestScript(PAGENAME):getCode()

			local args1 = is_not_empty(args[1]) and args[1] or ""
			local args2 = is_not_empty(args[2]) and args[2] or ""
			local stem = args["stem"]
			local nb = args["nb"]
			local sing = args["sing"]

			if args1 == "" and args2 == "" then
				args1, args2 = m_common.getType(PAGENAME)
			elseif args1 == "" then
				track("args2 was overridden")
				args1, _ = m_common.getType(PAGENAME)
			elseif args2 == "" then
				track("args1 was overridden")
				_, args2 = m_common.getType(PAGENAME)
			else
				track("args1 and args2 were overridden")
			end

			local function stemforacc()
				if args2 == "q" then
					return mw.ustring.sub(PAGENAME, 1, -2) .. 'ğ'
				elseif args2 == "k" then
					return mw.ustring.sub(PAGENAME, 1, -2) .. 'y'
				else
					return PAGENAME
				end
			end
			local function stemforpl()
				if args2 == "cc" then
					return mw.ustring.sub(PAGENAME, 1, -2)
				else
					return PAGENAME
				end
			end
			local function kljv2()
				if rfind(args1, "[əeiə̂êîöüö̂ü̂]") then
					return "ə"
				else
					return "a"
				end
			end
			local function kljv3()
				if rfind(args1, "[əeiə̂êî]") then
					return "i"
				elseif rfind(args1, "[öüö̂ü̂]") then
					return "ü"
				elseif rfind(args1, "[aıâ]") then
					return "ı"
				else
					return "u"
				end
			end
			local function n() return "n" end
			local function l() return "l" end
			local function r() return "r" end

			if script == "Latn" then
				if args2 == "c" or args2 == "cc" or args2 == "q" or args2 == "k" then
					if stem then
						if sing == "no" then
							if nb == "no" then
								table.insert("?")
							else
								table.insert(data.genders, {"p"})
							end
						else
							if nb == "no" then
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									stem .. kljv3()
								})
							else
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									stem .. kljv3()
								})
								table.insert(data.inflections, {
									label = "plural",
									accel = {form = "nom|p"},
									PAGENAME .. l() .. kljv2() .. r()
								})
							end
						end
					else
						if sing == "no" then
							if nb == "no" then
								table.insert("?")
							else
								table.insert(data.genders, {"p"})
							end
						else
							if nb == "no" then
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									stemforacc() .. kljv3()
								})
							else
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									stemforacc() .. kljv3()
								})
								table.insert(data.inflections, {
									label = "plural",
									accel = {form = "nom|p"},
									stemforpl() .. l() .. kljv2() .. r()
								})
							end
						end
					end
				else
					if stem then
						if sing == "no" then
							if nb == "no" then
								table.insert("?")
							else
								table.insert(data.genders, {"p"})
							end
						else
							if nb == "no" then
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									stem .. kljv3()
								})
							else
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									stem .. kljv3()
								})
								table.insert(data.inflections, {
									label = "plural",
									accel = {form = "nom|p"},
									PAGENAME .. l() .. kljv2() .. r()
								})
							end
						end
					else
						if sing == "no" then
							if nb == "no" then
								table.insert("?")
							else
								table.insert(data.genders, {"p"})
							end
						else
							if nb == "no" then
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									PAGENAME .. n() .. kljv3()
								})
							else
								table.insert(data.inflections, {
									label = "definite accusative",
									accel = {form = "def|acc|s"},
									PAGENAME .. n() .. kljv3()
								})
								table.insert(data.inflections, {
									label = "plural",
									accel = {form = "nom|p"},
									PAGENAME .. l() .. kljv2() .. r()
								})
							end
						end
					end
				end
			end
		end
	}
end

pos_functions["နာမ်"] = get_noun_pos(false)

pos_functions["နာမ်မကိတ်ညဳ"] = get_noun_pos(true)

return export