local export = {}

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

local plural_classes = {
	["1"] = "c2", ["1a"] = "c2", ["3"] = "c4", ["7"] = "c8", ["9"] = "c10", ["11"] = "c10", ["11/6"] = "c6",
	["12"] = "c13", ["5"] = "c6", ["9/6"] = "c6", ["1a/6"] = "c6", ["14"] = "c6", ["21"] = "c6"}

local plural_rules = {
		   ["1"] = {[1] = {"mw",    "v", 3}, [2] = {"mu",   "va", 3}},
		   ["1a"] = {[1] = {"",    "va", 1}},
		   ["3"] = {[1] = {"mu",  "mi", 3}, [2] = {"mw", "mw", 3}},
		   ["5"] = {[1] = {"i", "ma", 2}, [2] = {"zi", "me", 3}, [3] = {"dz", "mats", 3}, [4] = {"bv", "mapf", 3}, [5] = {"bh", "mabh", 3}, [6] = {"b", "map", 2}, [7] = {"g", "mak", 2}, [8] = {"dh", "madh", 3}, [9] = {"d", "mat", 2}, [10] = {"j", "mach", 2}, [11] = {"", "ma", 1}},
		   ["7"] = {[1] = {"ch",   "zv", 3}},
		   ["9"] = {[1] = {  "",    "", 1}},
		 ["9/6"] = {[1] = {  "",  "ma", 1}},
		 ["11"] = {[1] = {"ruk", "h", 4}, [2] = {"ru", "", 3}, [3] = {"rw", "", 3}},
		 ["11/6"] = {[1] = {  "",  "ma", 1}},
		["14"] = {[1] = {  "",  "ma", 1}}
}

function export.noun(frame)
	local params = {
		[1] = {},
		[2] = {},
		[3] = {}
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local categories = {}
	local head
	if not args[1] then
		args[1] = mw.title.getCurrentTitle().subpageText
		head = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(categories, "Requests for tone in " .. lang:getCanonicalName() .. " noun entries")
	end
	
	local class
	if args[2] then
		class = mw.text.split(args[2], "%/")[1]
	else
		class = "?"
	end
	
	local data = {lang = lang, pos_category = "名詞", categories = categories, heads = {head or args[1]}, genders = {"c" .. class}, inflections = {}}
	
	if class ~= "?" then
		table.insert(data.categories, lang:getCanonicalName() .. "第" .. class .. "類名詞")
	end
	
	if args[3] ~= "-" and plural_classes[args[2]] then
		local infl_plural = {label = "複數", accel = {form = "p", gender = plural_classes[args[2]]}, request = true}
		
		-- If no plural was provided, generate one
		if not args[3] then
			local singular = args[1] and require("Module:links").remove_links(args[1]) or mw.title.getCurrentTitle().text
			args[3] = export.generate_plural(singular, args[2])
		end
		
		table.insert(infl_plural, {term = args[3], genders = {plural_classes[args[2]]}})
		
		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