local export = {}
local pos_functions = {}

local lang = require("Module:languages").getByCode("khb")
local PAGENAME = mw.title.getCurrentTitle().text
local script = lang:findBestScript(PAGENAME) -- Talu or Lana

function export.show(frame)

	local args = frame:getParent().args
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	-- หมวดหมู่เป็นภาษาไทย
	local poscat_th = require("Module:utilities").translate_term(poscat)

	local data = {lang = lang, sc = script, pos_category = poscat_th, categories = {poscat_th .. "ภาษาไทลื้อใน" .. script:getCategoryName()}, heads = {args["head"]}, inflections = {}}

	local lana = {label = "อักษรไทธรรม", lang = lang, sc = require("Module:scripts").getByCode("Lana")}
	if args["l"] then table.insert(lana, args["l"]) end
	if args["l2"] then table.insert(lana, args["l2"]) end
	if args["l3"] then table.insert(lana, args["l3"]) end
	if #lana > 0 then table.insert(data.inflections, lana) end

	if pos_functions[poscat_th] then
		pos_functions[poscat_th](args, data)
	end

	local content = mw.title.new(PAGENAME):getContent()
	local code = mw.ustring.match(content, "{{khb%-pron[^o}]*}}")

	if not code and script:getCode() == "Talu" then
		table.insert(data.categories, "ศัพท์ภาษาไทลื้อที่ไม่มี khb-pron")
	end

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

end

pos_functions["คำนาม"] = function(args, data)

	local classifiers = {label = "คำลักษณนาม"}

	if args[1] then
		for _,par in ipairs(args) do
			if par == "*" then
				table.insert(classifiers, PAGENAME) -- shorthand
				table.insert(data.categories, "คำลักษณนามภาษาไทลื้อ")
				table.insert(data.categories, "คำนามภาษาไทลื้อที่ใช้คำลักษณนาม " .. PAGENAME)
			else
				table.insert(classifiers, par)
				table.insert(data.categories, "คำนามภาษาไทลื้อที่ใช้คำลักษณนาม " .. par)
			end
		end
		table.insert(data.inflections, classifiers)
	end

end

pos_functions["คำกริยา"] = function(args, data)

	local par1 = args[1]; if par1 == "" then par1 = nil end
	local par2 = args[2]; if par2 == "" then par2 = nil end
	local par3 = args[3]; if par3 == "" then par3 = nil end
	local abstract_forms = {label = "คำอาการนาม", accel = {form = "abstract-noun"}}

	if par1 ~= "-" then
		if not par1 then
			table.insert(abstract_forms, (data.sc:getCode() == "Talu" and "ᦂᦱᧃ" or "ᨠᩣ᩠ᩁ") .. PAGENAME)
		else
			if par1 == "~" then
				table.insert(abstract_forms, (data.sc:getCode() == "Talu" and "ᦂᦱᧃ" or "ᨠᩣ᩠ᩁ") .. PAGENAME)
				table.insert(abstract_forms, (data.sc:getCode() == "Talu" and "ᦩᦱᧄ" or "ᨤ᩠ᩅᩣ᩠ᨾ") .. PAGENAME)
			else
				table.insert(abstract_forms, par1)
				table.insert(abstract_forms, par2)
				table.insert(abstract_forms, par3)
			end
		end
		table.insert(data.inflections, abstract_forms)
	end

end

pos_functions["คำคุณศัพท์"] = function(args, data)

	local par1 = args[1]; if par1 == "" then par1 = nil end
	local par2 = args[2]; if par2 == "" then par2 = nil end
	local par3 = args[3]; if par3 == "" then par3 = nil end
	local abstract_forms = {label = "คำอาการนาม", accel = {form = "abstract-noun"}}

	if par1 ~= "-" then
		if not par1 then
			table.insert(abstract_forms, (data.sc:getCode() == "Talu" and "ᦩᦱᧄ" or "ᨤ᩠ᩅᩣ᩠ᨾ") .. PAGENAME)
		else
			table.insert(abstract_forms, par1)
			table.insert(abstract_forms, par2)
			table.insert(abstract_forms, par3)
		end
		table.insert(data.inflections, abstract_forms)
	end

end

-- same logic
pos_functions["คำกริยาวิเศษณ์"] = pos_functions["คำคุณศัพท์"]

return export