This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local m_links = require("Module:links")

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

local export = {}


local function postprocess(args, data)
	for key, form in pairs(data.forms) do
		-- Do not show singular or plural forms for nominals that don't have them
		if ((args["n"] == "sg") and not key:find("_sg$")) or ((args["n"] == "pl") and not key:find("_pl$")) then
			form = nil
		end
		
		data.forms[key] = form
	end
end


function export.weak_m(frame)
	local params = {
		[1] = {default = mw.title.getCurrentTitle().nsText == "Template" and "{{{1}}}" or mw.title.getCurrentTitle().subpageText},
		[2] = {default = mw.title.getCurrentTitle().nsText == "Template" and "{{{2}}}" or nil},
		
		["n"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "weak masculine", categories = {lang:getCanonicalName() .. " weak masculine nouns"}}
	
	data.forms["nom_sg"] = {args[1]}
	data.forms["acc_sg"] = {args[1]}
	data.forms["gen_sg"] = {(args[2] or args[1]) .. "n"}
	data.forms["dat_sg"] = {args[1]}
	
	data.forms["nom_pl"] = {(args[2] or args[1]) .. "n"}
	data.forms["acc_pl"] = {(args[2] or args[1]) .. "n"}
	data.forms["gen_pl"] = {(args[2] or args[1]) .. "n"}
	data.forms["dat_pl"] = {(args[2] or args[1]) .. "n"}
	
	postprocess(args, data)
	return make_table(data)
end


function export.weak_f(frame)
	local params = {
		[1] = {default = mw.title.getCurrentTitle().nsText == "Template" and "{{{1}}}" or mw.title.getCurrentTitle().subpageText},
		[2] = {default = mw.title.getCurrentTitle().nsText == "Template" and "{{{2}}}" or nil},
		
		["n"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "weak feminine", categories = {lang:getCanonicalName() .. " weak feminine nouns"}}
	
	data.forms["nom_sg"] = {args[1]}
	data.forms["acc_sg"] = {args[1]}
	data.forms["gen_sg"] = {(args[2] or args[1]) .. "n"}
	data.forms["dat_sg"] = {args[1], (args[2] or args[1]) .. "n"}
	
	data.forms["nom_pl"] = {(args[2] or args[1]) .. "n"}
	data.forms["acc_pl"] = {(args[2] or args[1]) .. "n"}
	data.forms["gen_pl"] = {(args[2] or args[1]) .. "n"}
	data.forms["dat_pl"] = {(args[2] or args[1]) .. "n"}
	
	postprocess(args, data)
	return make_table(data)
end


-- Make the table
function make_table(data)
	local function repl(param)
		if param == "info" then
			return mw.getContentLanguage():ucfirst(data.info or "")
		end
		
		local form = data.forms[param]
		
		if not form or #form == 0 then
			return "—"
		end
		
		local ret = {}
		
		for key, subform in ipairs(form) do
			table.insert(ret, m_links.full_link({lang = lang, alt = subform}))
		end
		
		return table.concat(ret, ", ")
	end
	
	local names = {
		["nom"] = "nominative",
		["acc"] = "accusative",
		["gen"] = "genitive",
		["dat"] = "dative",
		
		["sg"] = "singular",
		["pl"] = "plural",
	}
	
	local cases = {"nom", "acc", "gen", "dat"}
	local numbers = {"sg", "pl"}
	
	local wikicode = {}
	
	table.insert(wikicode, "{| class=\"inflection-table vsSwitcher\" data-toggle-category=\"inflection\" style=\"border: 1px solid #CCCCFF;\" cellspacing=\"1\" cellpadding=\"3\"")
	table.insert(wikicode, "|- style=\"background: #CCCCFF;\"")
	table.insert(wikicode, "! class=\"vsToggleElement\" style=\"text-align: left; min-width: " .. tostring(8 + 12 * #numbers) .. "em;\" colspan=\"" .. tostring(#numbers + 1) .. "\" | {{{info}}}")
	
	table.insert(wikicode, "|- class=\"vsHide\" style=\"background: #CCCCFF;\"")
	table.insert(wikicode, "| style=\"background: #E6E6FF; min-width: 8em;\" |")
	
	for _, num in ipairs(numbers) do
		table.insert(wikicode, "! style=\"background: #CCCCFF; min-width: 12em;\" | " .. mw.getContentLanguage():ucfirst(names[num]))
	end
	
	for _, case in ipairs(cases) do
		table.insert(wikicode, "|- class=\"vsHide\" style=\"background: #F2F2FF;\"")
		table.insert(wikicode, "! style=\"background: #CCCCFF;\" | " .. mw.getContentLanguage():ucfirst(names[case]))
		
		for _, num in ipairs(numbers) do
			table.insert(wikicode, "| {{{" .. case .. "_" .. num .. "}}}")
		end
	end
	
	table.insert(wikicode, "|}")
	
	wikicode = table.concat(wikicode, "\n")
	
	return (mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl)) .. require("Module:utilities").format_categories(data.categories, lang)
end

return export