"इस मॉड्यूल हेतु प्रलेख Module:xmf-noun/doc पर बनाया जा सकता है"

local export = {}

local uncountable = 0
local countable = 1
local uncertain = 2
local countable_and_uncountable = 3
local usually_uncountable = 4

function export.show(frame)
	local lang = require("Module:languages").getByCode("xmf")

	local args = frame:getParent().args
	local pos_category = frame.args[1]

	local data = {
		lang = lang,
		pos_category = pos_category,
		categories = {},
		heads = { args["head"] },
		inflections = {},
		genders = { args["g"] },
	}

	local custom_plurals, countability = custom_plurals_and_countability(args, pos_category == "proper nouns")

	if countability == uncountable then
		add_uncountable(data)
	elseif countability ~= uncertain then
		add_plurals(data, custom_plurals, countability)
	end

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

function add_categories(data, countable, uncountable)
	if countable then
		table.insert(data.categories, "Mingrelian countable " .. data.pos_category)
	end
	if uncountable and data.pos_category == "nouns" then
		table.insert(data.categories, "Mingrelian uncountable nouns")
	end
end

function add_uncountable(data)
	if data.pos_category == "nouns" then
		table.insert(data.inflections, { label = "[[Appendix:Glossary#uncountable|uncountable]]" })
	end
	add_categories(data, false, true)
end

function add_countable(data, countability)
	if countability == countable_and_uncountable then
		table.insert(data.inflections, { label = "[[Appendix:Glossary#countable|countable]] and [[Appendix:Glossary#uncountable|uncountable]]" })
		add_categories(data, true, true)
	elseif countability == usually_uncountable then
		table.insert(data.inflections, { label = "usually [[Appendix:Glossary#uncountable|uncountable]]" })
		add_categories(data, true, true)
	else
		add_categories(data, true, false)
	end
end

function add_plurals(data, custom_plurals, countability)
	add_countable(data, countability)

	if not is_iempty(custom_plurals) then
		add_custom_plurals(data, custom_plurals)
	else
		add_standard_plurals(data)
	end
end

function add_custom_plurals(data, plurals)
	table.insert(data.inflections, { label = "plural", plurals[1], plurals[2] })
end

function add_standard_plurals(data)
	local plurals = { label = "plural" }

	local term = data.heads[1] or mw.title.getCurrentTitle().text
	local auslaut = mw.ustring.sub(term, -1)

	if auslaut == "ა" or auslaut == "ი" then
		table.insert(plurals, mw.ustring.sub(term, 0, -2) .. "ეფი")
	elseif auslaut == "ე" or auslaut == "უ" then
		table.insert(plurals, term .. "ეფი")
	elseif auslaut == "ო" then
		-- more trouble than its worth?
		-- table.insert(plurals, mw.ustring.sub(term, 0, -2) .. "უეფი")
		table.insert(plurals, term .. "ეფი")
	elseif auslaut == "ჷ" then
		-- -ჷ ← -უ / -ი
		table.insert(plurals, mw.ustring.sub(term, 0, -2) .. "ეფი")
	else
		table.insert(plurals, term .. "ეფი")
	end

	table.insert(data.inflections, plurals)
end

function custom_plurals_and_countability(args, is_proper_noun)
	local custom_plurals = {}
	local countability = countable
	if is_proper_noun then
		countability = uncountable
	end

	for _, s in ipairs(args) do
		if s == "-" then
			countability = uncountable
		elseif s == "+" then
			countability = countable
		elseif s == "+-" then
			countability = countable_and_uncountable
		elseif s == "-+" then
			countability = usually_uncountable
		elseif s == "?" then
			countability = uncertain 
		else
			table.insert(custom_plurals, s)
		end
	end

	return custom_plurals, countability
end

function is_iempty(table)
	for _, _ in ipairs(table) do
		return false
	end

	return true
end

return export