-- Common code between [[Module:hi-headword]] and [[Module:pa-headword]].

local export = {}
local m_links = require("Module:links")

export.pos_functions = {}

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

local function glossary_link(anchor, text)
	text = text or anchor
	return "[[ภาคผนวก:อภิธานศัพท์#" .. anchor .. "|" .. text .. "]]"
end

local function process_genders(data, genders)
	for _, g in ipairs(genders) do
		if g == "m" or g == "f" or g == "m-p" or g == "f-p" or g == "mf" or g == "mf-p" or g == "mfbysense" or g == "mfbysense-p" or g == "?" then
			table.insert(data.genders, g)
		else
			error("Invalid gender: " .. (g or "(nil)"))
		end
	end
end

export.pos_functions["คำคุณศัพท์"] = {
	params = {
		["comparative"] = {},
		["superlative"] = {},
		[1] = {alias_of = "comparative"},
		[2] = {alias_of = "superlative"},
		["f"] = {list = true},
		["m"] = {list = true},
		["ind"] = {type = "boolean"},
	},
	func = function(args, data)
		if args["ind"] then
			table.insert(data.inflections, {label = glossary_link("ผันรูปไม่ได้")})
			table.insert(data.categories, "คำคุณศัพท์ผันรูปไม่ได้" .. data.langname)
		end
		if args["comparative"] then
			table.insert(data.inflections, {label = "ขั้นกว่า", args["comparative"]})
		end
		if args["superlative"] then
			table.insert(data.inflections, {label = "ขั้นสุด", args["superlative"]})
		end
		if #args["m"] > 0 then
			args["m"].label = "เพศชาย"
			table.insert(data.inflections, args["m"])
		end
		if #args["f"] > 0 then
			args["f"].label = "เพศหญิง"
			table.insert(data.inflections, args["f"])
		end
	end,
}

export.pos_functions["เลขเชิงอันดับที่"] = {
	params = {
		["f"] = {list = true},
		["m"] = {list = true},
		["ind"] = {type = "boolean"},
	},
	func = function(args, data)
		data.pos_category = "คำคุณศัพท์"
		table.insert(data.categories, "เลข" .. data.langname)
		if args["ind"] then
			table.insert(data.inflections, {label = glossary_link("ผันรูปไม่ได้")})
			table.insert(data.categories, "เลขผันรูปไม่ได้" .. data.langname)
		end
		if #args["m"] > 0 then
			args["m"].label = "เพศชาย"
			table.insert(data.inflections, args["m"])
		end
		if #args["f"] > 0 then
			args["f"].label = "เพศหญิง"
			table.insert(data.inflections, args["f"])
		end
	end,
}

export.pos_functions["เลขเชิงการนับ"] = {
	params = {
		["g"] = {list = true},
		["sym"] = {list = true},
	},
	func = function(args, data)
		data.pos_category = "เลข"
		process_genders(data, args["g"])
		if #args["sym"] > 0 then
			args["sym"].label = "native script symbol"
			table.insert(data.inflections, args["sym"])
		end
	end,
}

local function nouns(plpos)
	return {
		params = {
			["g"] = {list = true, default = "?"},
			["pl"] = {list = true},
			["f"] = {list = true},
			["m"] = {list = true},
			["ind"] = {type = "boolean"},
		},
		func = function(args, data)
			process_genders(data, args["g"])
			if args["ind"] then
				if #args["pl"] > 0 then
					error("Can't specify both ind= and pl=")
				end
				table.insert(data.inflections, {label = glossary_link("ผันรูปไม่ได้")})
				table.insert(data.categories, plpos .. "ผันรูปไม่ได้" .. data.langname)
			elseif #args["pl"] > 0 then
				args["pl"].label = "พหูพจน์"
				table.insert(data.inflections, args["pl"])
			end
			if #args["m"] > 0 then
				args["m"].label = "เพศชาย"
				table.insert(data.inflections, args["m"])
			end
			if #args["f"] > 0 then
				args["f"].label = "เพศหญิง"
				table.insert(data.inflections, args["f"])
			end
			if #args["m"] > 0 or #args["f"] > 0 then
				table.insert(data.categories, data.langname .. " " .. plpos .. " with other-gender equivalents")
			end
		end,
	}
end

export.pos_functions["คำนาม"] = nouns("คำนาม")
export.pos_functions["คำวิสามานยนาม"] = nouns("คำวิสามานยนาม")

export.pos_functions["คำสรรพนาม"] = {
	params = {
		["g"] = {list = true},
	},
	func = function(args, data)
		process_genders(data, args["g"])
	end,
}

export.pos_functions["คำกริยา"] = {
	params = {
		[1] = {},
		["g"] = {list = true},
	},
	func = function(args, data)
		data.genders = args["g"]

		if args[1] then
			local label, cat
			if args[1] == "t" then
				label = "สกรรม"
				table.insert(data.categories, "คำสกรรมกริยา" .. data.langname)
			elseif args[1] == "i" then
				label = "อกรรม"
				table.insert(data.categories, "คำอกรรมกริยา" .. data.langname)
			elseif args[1] == "d" then
				label = "ทวิกรรม"
				table.insert(data.categories, "คำทวิกรรมกริยา" .. data.langname)
			elseif args[1] == "it" or args[1] == "ti" then
				label = "สกรรมและอกรรม"
				table.insert(data.categories, "คำสกรรมกริยา" .. data.langname)
				table.insert(data.categories, "คำอกรรมกริยา" .. data.langname)
			else
				error("Unrecognized param 1=" .. args[1] .. ": Should be 'i' = intransitive, 't' = transitive, or 'it'/'ti' = ambitransitive")
			end
			table.insert(data.inflections, {label = glossary_link(label)})
		end

		local head = data.heads[1]
		if head:find(" ") then
			local base_verb = m_links.remove_links(head):gsub("^.* ", "")
			table.insert(data.categories, data.langname .. " compound verbs with base verb " .. base_verb)
		end
	end,
}

local function pos_with_gender()
	return {
		params = {
			["g"] = {list = true},
		},
		func = function(args, data)
			data.genders = args["g"]
		end,
	}
end

export.pos_functions["เลข"] = pos_with_gender()
export.pos_functions["ปัจจัย"] = pos_with_gender()
export.pos_functions["รูปผันคำคุณศัพท์"] = pos_with_gender()
export.pos_functions["รูปผันคำนาม"] = pos_with_gender()
export.pos_functions["รูปผันคำสรรพนาม"] = pos_with_gender()
export.pos_functions["รูปผันคำกำหนด"] = pos_with_gender()
export.pos_functions["รูปผันคำกริยา"] = pos_with_gender()
export.pos_functions["รูปผันคำปัจฉบท"] = pos_with_gender()

return export