Documentation for this module may be created at Module:th-headword/doc

local export = {}
local pos_functions = {}

local lang = require("Module:languages").getByCode("th")
local script = require('Module:scripts').getByCode("Thai")
local PAGENAME = mw.title.getCurrentTitle().text
local pron = require("Module:th-pron")

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 head = args["head"] or PAGENAME
	local inflections = {}
	local categories = {poscat .. "សៀម"}

	if args["cat2"] then
		table.insert(categories, "Thai " .. args["cat2"])
	end
	if args["cat3"] then
		table.insert(categories, "Thai " .. args["cat3"])
	end

	if pos_functions[poscat] then
		pos_functions[poscat](args, inflections, categories)
	end

	local phon, tr
	local content = mw.title.new(PAGENAME):getContent()
	local code = mw.ustring.match(content, '{{th%-pron[^}]*}}')

	if not code then
		phon = args["phon"] or args["mono"] or "-"
		tr = pron.translit(phon , "th", "Thai", "paiboon")
		table.insert(categories, "បច្ចេកសព្ទសៀមគ្មានទំព័រគំរូ th-pron")
	else
		phon = args["phon"] or args["mono"] or mw.ustring.match(code, '|([^|}]+)') or PAGENAME -- detects only first one
		tr = pron.translit(phon, "th", "Thai", "paiboon")
	end

	if mw.ustring.match(PAGENAME, '.-[ก-ฮ](รร)$') or mw.ustring.match(PAGENAME, '.-[ก-ฮ](รร)[^ะ-ฺๅ็-๎].-') then
		if PAGENAME ~= 'วงจรรวม' and PAGENAME ~= 'สรรวง' then
			table.insert(categories, "បច្ចេកសព្ទសៀមប្រកបជាមួយ รร")
		end
	end
	local letter = true
	local specialLetter = mw.ustring.gmatch(PAGENAME, '.-([ฤฦ]ๅ?).-')
	while true do
		letter = specialLetter()
		if (not letter) then break end
		table.insert(categories, "បច្ចេកសព្ទសៀមប្រកបជាមួយ " .. letter)
	end
	local specialLetter2 = mw.ustring.gmatch(PAGENAME, '.-([ฃฅฆฌฎฏฐฑฒฬฮใๆ…]).-')
	while true do
		letter = specialLetter2()
		if (not letter) then break end
		table.insert(categories, "បច្ចេកសព្ទសៀមប្រកបជាមួយ " .. letter)
	end

	return require("Module:headword").full_headword(lang, nil, head, tr, nil, inflections, categories, nil)

end

pos_functions["nouns"] = function(args, inflections, categories)

	local classifiers = {label = "classifier", accel = "classifier-of"}

	if args[1] then
		for _,par in ipairs(args) do
			if par == "*" then
				table.insert(classifiers, PAGENAME) -- shorthand
				table.insert(categories, "Thai classifiers")
			else
				table.insert(classifiers, par)
			end
		end
		table.insert(inflections, classifiers)
	end

end

pos_functions["កិរិយាសព្ទ"] = function(args, inflections, categories)

	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 = "abstract-noun-form-of"}

	if par1 ~= "-" then
		if not par1 then
			table.insert(abstract_forms, "การ" .. PAGENAME)
		else
			if par1 == "~" then
				table.insert(abstract_forms, "การ" .. PAGENAME)
				table.insert(abstract_forms, "ความ" .. PAGENAME)
			else
				table.insert(abstract_forms, par1)
				table.insert(abstract_forms, par2)
				table.insert(abstract_forms, par3)
			end
		end
		table.insert(inflections, abstract_forms)
	end

end

pos_functions["adjectives"] = function(args, inflections, categories)

	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 = "abstract noun", accel = "abstract-noun-form-of"}

	if par1 ~= "-" then
		if not par1 then
			table.insert(abstract_forms, "ความ" .. PAGENAME)
		else
			if par1 == "~" then
				table.insert(abstract_forms, "ความ" .. PAGENAME)
				table.insert(abstract_forms, "การ" .. PAGENAME)
			else
				table.insert(abstract_forms, par1)
				table.insert(abstract_forms, par2)
				table.insert(abstract_forms, par3)
			end
		end
		table.insert(inflections, abstract_forms)
	end

end

-- same logic
pos_functions["adverbs"] = pos_functions["adjectives"]
-- same logic
pos_functions["proper nouns"] = pos_functions["nouns"]

return export