-- Based on [[Module:ar-headword]] by: Benwing, CodeCat
-- Adapted by Fenakhay

local lang = require("Module:languages").getByCode("acm")

local export = {}
local pos_functions = {}

local u = mw.ustring.char
local TAM = u(0x0629)
local ALIF = u(0x0627) -- ʾalif = ا

-----------------------
-- Utility functions --
-----------------------

-- If Not Empty
local function ine(arg)
	if arg == "" then
		return nil
	else
		return arg
	end
end

local function list_to_set(list)
	local set = {}
	for _, item in ipairs(list) do
		set[item] = true
	end
	return set
end

-- version of mw.ustring.gsub() that discards all but the first return value
function rsub(term, foo, bar)
	local retval = mw.ustring.gsub(term, foo, bar)
	return retval
end

local rfind = mw.ustring.find

-- Tracking functions

local trackfn = require("Module:debug").track

function track(page)
	trackfn("acm-headword/" .. page)
	return true
end

local function append_cat(data, pos)
	table.insert(data.categories, lang:getCanonicalName() .. pos)
end

function remove_links(text)
	text = rsub(text, "%[%[[^|%]]*|", "")
	text = rsub(text, "%[%[", "")
	text = rsub(text, "%]%]", "")
	return text
end

-- The main entry point.
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 = {
		[1] = {list = "head", allow_holes = true, default = ""},
		["head"] = {default = ""},
		["tr"] = {list = true, allow_holes = true},
	}
	
	local args = frame:getParent().args
	
	-- Gather parameters
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = {}, translits = {}, genders = {}, inflections = {}}
	
	local head = args["head"] or PAGENAME or ""
	local translit = ine(args["tr"])
	local i = 1
	
	local irreg_translit = false
	
	while head do
		table.insert(data.heads, head)
		data.translits[#data.heads] = translit
		
		i = i + 1
		head = ine(args["head" .. i])
		translit = ine(args["tr" .. i])
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end
	
	return require("Module:headword").full_headword(data)
end

-- Get a list of inflections. See handle_infl() for meaning of ARGS, ARGPREF
-- and DEFGENDER.
local function getargs(args, argpref, defgender)
	-- Gather parameters
	local forms = {}
	
	local form = ine(args[argpref])
	local translit = ine(args[argpref .. "tr"])
	local gender = ine(args[argpref .. "g"])
	local gender2 = ine(args[argpref .. "g2"])
	local i = 1
	
	while form do
		local genderlist = (gender or gender2) and {gender, gender2} or defgender and {defgender} or nil
		
		table.insert(forms, {term = form, translit = translit, gender = genderlist})

		i = i + 1
		form = ine(args[argpref .. i])
		translit = ine(args[argpref .. i .. "tr"])
		gender = ine(args[argpref .. i .. "g"])
		gender2 = ine(args[argpref .. i .. "g2"])
	end

	return forms
end

local function handle_infl(args, data, argpref, label, defgender)
	local newinfls = getargs(args, argpref, defgender)
	newinfls.label = label

	if #newinfls > 0 then
		table.insert(data.inflections, newinfls)
	end
end

local function handle_all_infl(args, data, argpref, label, nobase)
	if not nobase and argpref ~= "" then
		handle_infl(args, data, argpref, label)
	end
	
	local labelsp = label == "" and "" or label .. " "
	handle_infl(args, data, argpref .. "cons", labelsp .. "construct state")
end

-- Handle the case where pl=-, indicating an uncountable noun.
local function handle_noun_plural(args, data)
	if args["pl"] == "-" then
		table.insert(data.inflections, {label = "usually [[Appendix:Glossary#uncountable|uncountable]]"})
		append_cat(data, "uncountable nouns")
	else
		handle_infl(args, data, "pl", "plural")
	end
end

local valid_genders = list_to_set(
	{"m", "m-s",
	 "f", "f-s",
	 "m-p", "f-p", "p",
	 "d", "m-d", "f-d",
    })

local function is_masc_sg(g)
	return g == "m"
end
local function is_fem_sg(g)
	return g == "f"
end

-- Handle gender in unnamed param 2 and a second gender in param g2, inserting
-- into the list of genders in GENDER. Also, if a lemma, insert categories
-- into CATS if the gender is unexpected for the form of the noun or if multiple
-- genders occur. If gender unspecified, default to DEFAULT, which may be
-- omitted.
local function handle_gender(args, data, default, nonlemma)
	
	local PAGENAME = mw.title.getCurrentTitle().text
	
	local g = ine(args["g"]) or default
	local g2 = ine(args["g2"])

	local function process_gender(g)
		if not g then
			table.insert(data.genders, "?")
		elseif valid_genders[g] then
			table.insert(data.genders, g)
		else
			error("Unrecognized gender: " .. g)
		end
	end

	process_gender(g)
	if g2 then
		process_gender(g2)
	end

	if nonlemma then
		return
	end
	
	if g and g2 then
		append_cat(data, "terms with multiple genders")
	elseif is_masc_sg(g) or is_fem_sg(g) then
		local head = PAGENAME or ine(args["head"]) or ine(args[1])
		if head then
			head = remove_links(head)
			local ends_with_tam = rfind(head, "^[^ ]*" .. TAM .. "$") or
				rfind(head, "^[^ ]*" .. TAM .. " ")
			if is_masc_sg(g) and ends_with_tam then
				append_cat(data, "masculine terms with feminine ending")
			elseif is_fem_sg(g) and not ends_with_tam and
				not rfind(head, ALIF .. "$") then
				append_cat(data, "feminine terms lacking feminine ending")
			end
		end
	end
end

-- Part-of-speech functions

pos_functions["adjectives"] = {
	func = function(args, data)
		handle_all_infl(args, data, "", "")
		handle_all_infl(args, data, "f", "feminine")
		handle_all_infl(args, data, "cpl", "common plural")
		handle_all_infl(args, data, "pl", "masculine plural")
		handle_all_infl(args, data, "fpl", "feminine plural")
		handle_all_infl(args, data, "dim", "diminutive")
		handle_infl(args, data, "el", "elative")
	end
}

function handle_sing_coll_noun_infls(args, data)
	handle_all_infl(args, data, "", "")
	handle_all_infl(args, data, "d", "dual")
	handle_all_infl(args, data, "pauc", "paucal")
	handle_noun_plural(args, data)
	handle_all_infl(args, data, "pl", "plural", "nobase")
end

pos_functions["集合名詞"] = {
	func = function(args, data)
		data.pos_category = "名詞"
		append_cat(data, "集合名詞")
		table.insert(data.inflections, {label = "collective"})
		
		handle_gender(args, data, "m")
		-- Handle sing= (the corresponding singulative noun) and singg= (its gender)
		handle_infl(args, data, "sing", "singulative", "f")
		handle_sing_coll_noun_infls(args, data)
	end
}

pos_functions["singulative nouns"] = {
	func = function(args, data)
		data.pos_category = "nouns"
		append_cat(data, "singulative nouns")
		table.insert(data.inflections, {label = "singulative"})
		
		handle_gender(args, data, "f")
		-- Handle coll= (the corresponding collective noun) and collg= (its gender)
		handle_infl(args, data, "coll", "collective", "m")
		handle_sing_coll_noun_infls(args, data)
	end
}

function handle_noun_infls(args, data, singonly)
	handle_all_infl(args, data, "", "")
	
	if not singonly then
		handle_all_infl(args, data, "d", "雙數")
		handle_noun_plural(args, data)
		handle_all_infl(args, data, "pl", "複數", "nobase")
	end
	
	handle_all_infl(args, data, "f", "feminine")
	handle_all_infl(args, data, "m", "masculine")
	
	if not singonly then
		handle_all_infl(args, data, "dim", "小稱")
	end
end

pos_functions["名詞"] = {
	func = function(args, data)
		handle_gender(args, data)
		handle_noun_infls(args, data)
		
		local g = ine(args["g"]) or default
		local g2 = ine(args["g2"])
	
		if is_masc_sg(g) or is_masc_sg(g2) then
			append_cat(data, "陽性名詞")
		elseif is_fem_sg(g) or is_fem_sg(g2) then
			append_cat(data, "陰性名詞")
		end
	end
}

pos_functions["數詞"] = {
	func = function(args, data)
		append_cat(data, "基數詞")
		handle_gender(args, data)
		handle_noun_infls(args, data)
	end
}

pos_functions["專有名詞"] = {
	func = function(args, data)
		handle_gender(args, data)
		handle_noun_infls(args, data, "僅限單數")
	end
}

pos_functions["形容詞陰性變格形"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		["pl"] = {},
		["islemma"] = { type = boolean },
		},
	func = function(args, data)
		data.pos_category = "形容詞變格形"
		handle_noun_plural(args, data)
		handle_gender(args, data, "f", "nonlemma")
	end
}

pos_functions["形容詞複數變格形"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		},
	func = function(args, data)
		data.pos_category = "形容詞變格形"
		handle_gender(args, data, "p", "nonlemma")
	end
}

pos_functions["名詞變格形"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		},
	func = function(args, data)
		handle_gender(args, data, nil, "nonlemma")
	end
}

pos_functions["名詞雙數形式"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		},
	func = function(args, data)
		data.pos_category = "名詞變格形"
		append_cat(data, "名詞雙數形式")
		handle_gender(args, data, "m-d", "nonlemma")
	end
}

pos_functions["active participles"] = {
	params = {
		[2] = {},
		},
	func = function(args, data)
		data.pos_category = "分詞"
		append_cat(data, "active participles")
		handle_conj_form(args, data)
		handle_all_infl(args, data, "", "")
		handle_all_infl(args, data, "f", "陰性")
		handle_all_infl(args, data, "cpl", "通用複數")
		handle_all_infl(args, data, "pl", "陽性複數")
		handle_all_infl(args, data, "fpl", "陰性複數")
	end
}

pos_functions["過去分詞"] = {
	params = {
		[2] = {},
		},
	func = function(args, data)
		data.pos_category = "分詞"
		append_cat(data, "過去分詞")
		handle_conj_form(args, data)
		handle_all_infl(args, data, "", "")
		handle_all_infl(args, data, "f", "陰性")
		handle_all_infl(args, data, "cpl", "通用複數")
		handle_all_infl(args, data, "pl", "陽性複數")
		handle_all_infl(args, data, "fpl", "陰性複數")
	end
}

return export