Used in {{swb-noun}}.


local export = {}

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

local plural_classes = {
	["1"] = "c2", ["1a"] = "c2", ["9/2"] = "c2", ["3"] = "c4", ["7"] = "c8", ["9"] = "c10",
	["5"] = "c6", ["11/6"] = "c6", ["11/4"] = "c4", ["11/10"] = "c10"}

local plural_rules = {
		   ["1"] = {[1] = {"mw",    "w", 3}, [2] = {"mu",   "wa", 3}, [3] = {"m", "wa", 2}},
		  ["1a"] = {[1] = {  "",   "wa", 1}},
		  ["9/2"] = {[1] = {  "",   "wa", 1}}, --a copy of 1a in case people prefer calling it this
		   ["3"] = {[1] = {"mu",  "mi", 3}, [2] = {"mw", "m", 3}, [3] = { "mo",  "me", 3}, [4] = {"m", "mi", 2}},
		   ["5"] = {[1] = {"dzi", "ma", 4}, [2] = {"ɗ", "mal", 2}, [3] = {"k", "mah", 2}, [4] = {"p", "mavh", 2}, [5] = {"tr", "mar", 3}, [6] = {"sh", "maz", 3}, [7] = {"", "ma", 1}},
		   ["7"] = {[1] = {"sh",   "z", 3}},
		   ["9"] = {[1] = {  "",    "", 1}},
		 ["11/6"] = {[1] = {  "",  "ma", 1}},
	   	["11/4"] = {[1] = {  "u",  "mi", 2}},
	   	["11/10"] = {[1] = {  "u",  "ngu", 2}, [2] = {"wa", "nya", 3}, [3] = {"wi", "mbi", 3}, [4] = {"wo", "mao", 3}}
}

function export.noun(frame)
	local params = {
		[1] = {required=true},
		[2] = {},
		["head"] = {},
		["h"] = {alias_of = "head"}
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "nouns", categories = {}, heads = {args["head"]}, genders = {mw.text.split(args[1], "%/")[1]}, inflections = {}}
	table.insert(data.categories, lang:getCanonicalName() .. " class " .. data.genders[1] .. " nouns")
	
	if args[2] ~= "-" and plural_classes[args[1]] then
		local infl_plural = {label = "plural", accel = {form = "p", gender = plural_classes[args[1]]}, request = true}
		
		-- If no plural was provided, generate one
		if not args[2] then
			local singular = args["head"] and require("Module:links").remove_links(args["head"]) or mw.title.getCurrentTitle().text
			args[2] = export.generate_plural(singular, args[1])
		end
		
		if args[2] then	
			table.insert(infl_plural, {term = args[2], genders = {plural_classes[args[1]]}})
		end
		
		table.insert(data.inflections, infl_plural)
	end
	
	return require("Module:headword").full_headword(data)
end


function match_case(string1, string2)
	local c1 = mw.ustring.sub(string1, 1, 1)
	local c2 = mw.ustring.sub(string2, 1, 1)
	
	if (mw.ustring.lower(c1) == c1) then
		return mw.ustring.lower(c2) .. mw.ustring.sub(string2, 2)
	else
		return mw.ustring.upper(c2) .. mw.ustring.sub(string2, 2)
	end
end


function export.generate_plural(singular, class)
	if plural_rules[class] then
		for k, v in ipairs(plural_rules[class]) do
			if mw.ustring.find(mw.ustring.lower(singular), "^" .. v[1]) then
				return match_case(singular, v[2] .. mw.ustring.sub(singular, v[3]))
			end
		end
	end
end

return export