Documentation for this module may be created at မဝ်ဂျူ:ro-headword/doc

local export = {}
local pos_functions = {}

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

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	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"] = {},
		["suff"] = {type = "boolean"},
		["cat"] = {list = true},
	}
	
	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end
	
	args = require("Module:parameters").process(args, params)
	
	local data = {lang = lang, pos_category = args["suff"] and "suffixes" or poscat, categories = args["suff"] and {"ရဝ်မေနဳယျာ " .. poscat:sub(1, -2) .. "-forming suffixes"} or {}, heads = {args.head}, genders = {}, inflections = {}}
	local appendix = {}
	
	for key, val in ipairs(args.cat) do
		table.insert(data.categories, "ရဝ်မေနဳယျာ " .. val)
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(class, args, data, appendix)
	end
	
	if #appendix == 0 then
		appendix[1] = ""
	end
	
	return
		require("Module:headword").full_headword(data) .. appendix[1]
end

local function is_equal(a1, a2)
	if a1 == a2 then
		return true
	end
	
	if #a1 == #a2 then
		for i = 1, #a1 do
			if a1[i] ~= a2[i] then
				return false
			end
		end
		return true
	else
		return false
	end
end

pos_functions["နာမဝိသေသန"] = {
	params = {
		[1] = {},
		["f"] = {list = true, allow_holes = true},
		["mp"] = {list = true, allow_holes = true},
		["fp"] = {list = true, allow_holes = true},
		["pl"] = {list = true, allow_holes = true},
		["only"] = {},
	},
	func = function(class, args, data, appendix)
		local mode = args[1]
		local only = {}
		
		if args.only then
			for i, val in ipairs(mw.text.split(args.only, "-")) do
				only[val] = true
			end
		end
		
		if mode == "inv" or mode == "i" or args["f"].maxindex > 0 or args["mp"].maxindex > 0 or args["fp"].maxindex > 0 or args["pl"].maxindex > 0 then
			require("Module:debug").track("ro-adj new")
		end
		
		if mode == "inv" or mode == "ind" then
			table.insert(data.genders, "m")
			table.insert(data.genders, "f")
			table.insert(data.genders, "n")
			table.insert(data.inflections, {label = "ပါဲဗလေတ်ဟွံမာန်"})
			table.insert(data.categories, "နာမဝိသေသနရဝ်မေနဳယျာပါ်ပါဲထောံဟွံဂွံဂမၠိုင်")
		else
			local form_order = {"m_sg", "f_sg", "m_pl", "f_pl"}
			local only_order = {"m", "f", "sg", "pl"}
			local allowed = {m_sg = true, f_sg = true, m_pl = true, f_pl = true}
			local only_names = {m = "ပုလ္လိင်", f = "ဣတ္တိလိင်", sg = "ကိုန်ဨကဝုစ်", pl = "ကိုန်ဗဟုဝစ်"}
			
			for i, val in pairs(only_names) do
				if only[i] then
					for key, form in pairs(allowed) do
						if not key:find(i) then
							allowed[key] = false
						end
					end
				end
			end
			
			for i, val in ipairs(form_order) do
				if allowed[val] then
					allowed[val] = "lemma"
					break
				end
			end
		
			if allowed.m_sg == "lemma" then
				table.insert(data.genders, "m")
				
				if not only.m then
					table.insert(data.genders, "n")
				end
			elseif allowed.f_sg == "lemma" then
				table.insert(data.genders, "f")
			elseif allowed.m_pl == "lemma" then
				table.insert(data.genders, "m-p")
			elseif allowed.f_pl == "lemma" then
				table.insert(data.genders, "f-p")
				
				if not only.f then
					table.insert(data.genders, "n-p")
				end
			else
				error("All forms are excluded by the \"only\" parameter")
			end
			
			for i, val in ipairs(only_order) do
				if only[val] then
					table.insert(data.inflections, {label = only_names[val] .. " ပါဲနူ"})
				end
			end
			
			local f, mp, fp = args["f"], args["pl"].maxindex > 0 and args["pl"] or args["mp"], args["pl"].maxindex > 0 and args["pl"] or args["fp"]
			
			require("Module:ro-adjective").make_basic_forms({PAGENAME}, f, mp, fp, mode == "i", only)
			
			local unified_sg, unified_pl = false, false
			
			if allowed.m_sg and allowed.f_sg and f.maxindex == 1 and f[1] == PAGENAME then
				unified_sg = true
				table.insert(data.genders, 2, "f")
			end
		
			if not unified_sg and allowed.f_sg and allowed.f_sg ~= "lemma" then
				f.label = "ဣတ္တိလိင်" .. (only.sg and "" or " ကိုန်ဨကဝုစ်")
				table.insert(data.inflections, f)
			end
			
			if allowed.m_pl and allowed.f_pl and is_equal(mp, fp) then
				unified_pl = true
				mp.label = "ကိုန်ဗဟုဝစ်"
				table.insert(data.inflections, mp)
			end
			
			if not unified_pl and allowed.m_pl and allowed.m_pl ~= "lemma" then
				mp.label = (only.m and "" or "ပုလ္လိင်") .. " " .. (only.pl and "" or "ကိုန်ဗဟုဝစ်")
				table.insert(data.inflections, mp)
			end
				
			if not unified_pl and allowed.f_pl and allowed.f_pl ~= "lemma" then
				fp.label = (only.f and "" or "ဣတ္တိလိင်ကဵုနပုလ္လိင်") .. " " .. (only.pl and "" or "ကိုန်ဗဟုဝစ်")
				table.insert(data.inflections, fp)
			end
		end
		
		return args["head"]
	end
}

pos_functions["determiners"] = pos_functions["နာမဝိသေသန"]

pos_functions["pronouns"] = pos_functions["နာမဝိသေသန"]

pos_functions["articles"] = pos_functions["နာမဝိသေသန"]

pos_functions["နာမ်"] = {
	params = {
		[1] = {list = "g", default = "?"},
		[2] = {list = "pl"},
		["f"] = {list = true},
		["m"] = {list = true},
		["sg"] = {},
		["pl\1_qual"] = {list = true, allow_holes = true},
		["f\1_qual"] = {list = true, allow_holes = true},
		["m\1_qual"] = {list = true, allow_holes = true},
		["sort"] = {}
	},
	func = function(class, args, data, appendix)
		local type
		
		for _, val in ipairs(args[1]) do
			table.insert(data.genders, val)
			
			if val:match("p") then
				type = "ကိုန်ဗဟုဝစ်"
				break
			end
		end
	
		if args["suff"] then
			data.pos_category = "အဆက်လက္ကရဴ"
			-- table.insert(data.categories, "Romanian noun-forming suffixes")
			type = "suffix"
		end
		
		if type == "ကိုန်ဗဟုဝစ်" then
			if args["sg"] then
				args[2] = {label = "ကိုန်ဗဟုဝစ်ဗီုဓမ္မတာ"}
			else
				args[2] = {label = "ပါဲနူကိုန်ဗဟုဝစ်"}
			end
			
			-- table.insert(data.categories, "Romanian pluralia tantum")
		else
			if args[2][1] == "-" then
				args[2] = {label = "[[:en:Appendix:Glossary#uncountable|တော်ဟွံမာန်]]"}
				
				if type ~= "အဆက်လက္ကရဴ" then
					table.insert(data.categories, "နာမ်ရဝ်မေနဳယျာမတော်ဟွံဂွံမာန်ဂမၠိုင်")
				end
			elseif args[2][1] == "!" then
				args[2] = {}
				
				if type ~= "အဆက်လက္ကရဴ" then
					-- table.insert(data.categories, "Romanian nouns with unattested forms")
				end
			else
				args[2].label = "ကိုန်ဗဟုဝစ်"
				args[2].accel = {form = "p"}
				
				if #args[2] > 0 then
					for key, val in ipairs(args[2]) do
						if ((val == "e" or val == "uri") and args[1][1] == "n") or (val == "i" and args[1][1] == "m") then
							args[2][key] = PAGENAME .. val
						end
					end
					
					for key, val in ipairs(args[2]) do
						if not mw.title.new(val).exists then
							-- table.insert(data.categories, "Romanian nouns with red links in their headword lines")
						end
								
						if args.pl_qual[key] then
							args[2][key] = {term = args[2][key], q = {args.pl_qual[key]}}
						end
					end
					
					if type ~= "အဆက်လက္ကရဴ" then
						table.insert(data.categories, "နာမ်ရဝ်မေနဳယျာမတော်ဂွံမာန်ဂမၠိုင်")
					end
				else
					if type ~= "အဆက်လက္ကရဴ" then
						args[2].request = true
					end
				end
			end
		end
	
		if args[2].label then
			table.insert(data.inflections, args[2])
		end
		
		if args["sg"] then
			if type == "ကိုန်ဗဟုဝစ်" then
				table.insert(data.inflections, {label = "singular", args["sg"]})
			else
				error("Parameter \"sg\" can only be used for pluralia tantum")
			end
		end
		
		if #args["f"] > 0 then
			if args.f_qual.maxindex > 0 then
				for key, val in ipairs(args["f"]) do
					if args.f_qual[key] then
						args["f"][key] = {term = args["f"][key], q = {args.f_qual[key]}}
					end
				end
			end
			
			args["f"].label = "feminine equivalent"
			table.insert(data.inflections, args["f"])
		end
		
		if #args["m"] > 0 then
			if args.m_qual.maxindex > 0 then
				for key, val in ipairs(args["m"]) do
					if args.m_qual[key] then
						args["m"][key] = {term = args["m"][key], q = {args.m_qual[key]}}
					end
				end
			end
			
			args["m"].label = "masculine equivalent"
			table.insert(data.inflections, args["m"])
		end
		
		return args["head"]
	end
}

function format_conj(conj)
	if not conj then
		return ''
	else
		if conj == 1 then
			return '&nbsp;<i>[[Appendix:Romanian first conjugation|1st conjugation]]</i>'
		elseif conj == 2 then
			return '&nbsp;<i>[[Appendix:Romanian second conjugation|2nd conjugation]]</i>'
		elseif conj == 3 then
			return '&nbsp;<i>[[Appendix:Romanian third conjugation|3rd conjugation]]</i>'
		elseif conj == 4 then
			return '&nbsp;<i>[[Appendix:Romanian fourth conjugation|4th conjugation]]</i>'
		else
			return '&nbsp;<small>[conj?]</small>'
		end
	end
end

function get_conj(head, ind)
	local inf, conj = mw.ustring.match(require("Module:links").remove_links(head), '^a (.+)') or error('Head must begin with "a"')
	
	inf = mw.ustring.gsub(inf, 'á', 'a')
	inf = mw.ustring.gsub(inf, 'í', 'i')
	inf = mw.ustring.gsub(inf, '́', '')		-- remove combining acute
	
	if mw.ustring.match(inf, '[iî]$') then
		conj = 4
	elseif mw.ustring.match(inf, 'e$') then
		conj = 3
	elseif mw.ustring.match(inf, 'ea$') then
		if mw.ustring.match(inf, '[cg]hea$') then
			conj = 1
		elseif ind then
			if mw.ustring.match(ind, 'ează$') then
				conj = 1
			else
				conj = 2
			end
		end
	elseif mw.ustring.match(inf, 'a$') then
		conj = 1
	else
		error('Unrecognized verb ending')
	end
	
	return conj
end

pos_functions["ကြိယာ"] = {
	params = {
		[1] = {list = "pres"},
		[2] = {list = "past"},
		["inf"] = {},
		["conj"] = {type = "number"},
		["pres_qual"] = {list = "pres\1_qual", allow_holes = true},
		["past_qual"] = {list = "past\1_qual", allow_holes = true}
	},
	
	func = function(class, args, data, appendix)
		local multi = false
		
		if mw.ustring.match(PAGENAME, ' ') then
			multi = true
		end
		
		if #args[1] > 0 and args.pres_qual.maxindex > 0 then
			for key, val in ipairs(args[1]) do
				if args.pres_qual[key] then
					args[1][key] = {term = args[1][key], q = {args.pres_qual[key]}}
				end
			end
		end
		
		if #args[2] > 0 and args.past_qual.maxindex > 0 then
			for key, val in ipairs(args[2]) do
				if args.past_qual[key] then
					args[2][key] = {term = args[2][key], q = {args.past_qual[key]}}
				end
			end
		end
		
		if args[1][1] == "!" then
			table.insert(data.inflections, {label = "third-person singular present not attested"})
			-- table.insert(data.categories, 'Romanian verbs with unattested forms')
		elseif args[1][1] == "-" then
			table.insert(data.inflections, {label = "third-person singular present not used"})
			-- table.insert(data.categories, "Romanian defective verbs")
		else
			args[1].label = 'third-person singular present'
			args[1].request = true
			table.insert(data.inflections, args[1])
		end
		
		if args[2][1] == "!" then
			table.insert(data.inflections, {label = "past participle not attested"})
			table.insert(data.categories, 'Romanian verbs with unattested forms')
		elseif args[2][1] == "-" then
			table.insert(data.inflections, {label = "past participle not used"})
			table.insert(data.categories, "Romanian defective verbs")
		else
			args[2].label = 'past participle'
			args[2].request = true
			table.insert(data.inflections, args[2])
		end
		
		local sc = lang:findBestScript(data.heads[1] or args.inf or PAGENAME):getCode()
		
		if not data.heads[1] then
			if not args.inf then
				if multi then
					args.inf = mw.text.split(PAGENAME, ' ')
					args.inf = '[[' .. table.concat(args.inf, ']] [[') .. ']]'
				else
					args.inf = PAGENAME
				end
			end
			
			if sc == 'Cyrl' then
				data.heads = {'а ' .. args.inf} --Cyrillic 'а'
			else
				data.heads = {'a ' .. args.inf}
			end
		end
	
		local conj
		
		if multi or NAMESPACE ~= '' or sc == 'Cyrl' then
			conj = args.conj or 0
		else
			conj = get_conj(data.heads[1], args[1][1])
			
			if args.conj and args.conj ~= conj then
				require("Module:debug").track('ro-verb with wrong conjugation')
			end
		end
	
		if conj then
			if conj == 1 then
			-- 	table.insert(data.categories, 'Romanian verbs in 1st conjugation')
			elseif conj == 2 then
			-- 	table.insert(data.categories, 'Romanian verbs in 2nd conjugation')
			elseif conj == 3 then
			-- 	table.insert(data.categories, 'Romanian verbs in 3rd conjugation')
			elseif conj == 4 then
			-- 	table.insert(data.categories, 'Romanian verbs in 4th conjugation')
			else
			-- 	table.insert(data.categories, 'Requests for inflections in Romanian entries')
			end
		end
	
		appendix[1] = format_conj(conj)
	end
}

return export