local export = {}

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

-- Functions that do the actual inflecting by creating the forms of a basic term.
local inflections = {}

-- Checks each form for the presence of accent marks.
-- If any form doesn't contain any accent marks, flag the entry for attention.
local function check_accents(args, data)
	-- Do we need to check?
	local noacc = args["noacc"]; if noacc == "" then noacc = nil end
	
	if noacc then
		return
	end
	
	-- Yes, we do need to check.
	for key, subforms in pairs(data.forms) do
		for key2, subform in ipairs(subforms) do
			if not require("Module:sl-common").has_accents(subform) then
				table.insert(data.categories, "缺少音調的斯洛文尼亞語名詞")
				return  -- So that we don't add the category more than once
			end
		end
	end
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local infl_type = frame.args[1] or error("Inflection type has not been specified.")
	local args = frame:getParent().args
	local num = args["n"]; if num == "" then num = nil end
	
	local data = {forms = {}, info = nil, categories = {}, num = num}
	
	if inflections[infl_type] then
		inflections[infl_type](args, data)
	else
		error("Unknown inflection type '" .. infl_type .. "'")
	end
	
	check_accents(args, data)
	
	return make_table(data)
end

-- Masculine o-stem
inflections["m"] = function(args, data)
	local stem_noending = args[1]; if stem_noending == "" then stem_noending = nil end
	local stem = args[2]; if stem == "" then stem = nil end
	local anim = args["a"]; if not anim or anim == "" then anim = false else anim = true end
	
	local nom_pl = args["nom_pl"]; if nom_pl == "" then nom_pl = nil end
	local nom_pl2 = args["nom_pl2"]; if nom_pl2 == "" then nom_pl2 = nil end
	
	if not stem_noending then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem_noending = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	if not stem then
		stem = stem_noending
		stem_noending = make_endingless_stem(stem)
	end
	
	-- Is this a hard stem or a soft stem?
	local oe = "o"
	local hs = "硬音"
	data.info = "陽性" .. (anim and "anim." or "inan.")
	
	if require("Module:sl-common").is_soft(stem) then
		oe = "e"
		hs = "軟音"
	end
	
	data.info = data.info .. "," .. hs .. "o-詞幹"
	table.insert(data.categories, "斯洛文尼亞語陽性" .. hs .. "o-詞幹名詞")
	
	-- Categorize further
	local stem_noaccents = require("Module:sl-common").remove_accents(stem)
	local stem_noending_noaccents = require("Module:sl-common").remove_accents(stem_noending)
	local corrected_stem = nil
	
	if stem_noaccents == stem_noending_noaccents then
		corrected_stem = stem_noending
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e([ln]j)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e([ln]j)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e(.)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e(.)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "[aeiou]$", "") then
		table.insert(data.categories, "帶詞尾元音的斯洛文尼亞語陽性o-詞幹名詞")
		corrected_stem = mw.ustring.gsub(stem_noending, "[aeiou]$", "")
	elseif stem_noaccents == stem_noending_noaccents .. "j" then
		table.insert(data.categories, "帶j-中綴的斯洛文尼亞語陽性軟音o-詞幹名詞")
		corrected_stem = stem_noending .. "j"
	elseif stem_noaccents == stem_noending_noaccents .. "-j" then
		table.insert(data.categories, "帶j-中綴的斯洛文尼亞語陽性軟音o-詞幹名詞")
		corrected_stem = stem_noending .. "-j"
	elseif stem_noaccents == stem_noending_noaccents .. "n" then
		table.insert(data.categories, "帶n-中綴的斯洛文尼亞語陽性硬音o-詞幹名詞")
		corrected_stem = stem_noending .. "n"
	elseif stem_noaccents == stem_noending_noaccents .. "t" then
		table.insert(data.categories, "帶t-中綴的斯洛文尼亞語陽性硬音o-詞幹名詞")
		corrected_stem = stem_noending .. "t"
	else
		table.insert(data.categories, "斯洛文尼亞語不規則名詞")
	end
	
	if corrected_stem and stem ~= corrected_stem then
		table.insert(data.categories, "有重音交替的斯洛文尼亞語名詞")
	end
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {anim and stem .. "a" or stem_noending}
	data.forms["gen_sg"] = {stem .. "a"}
	data.forms["dat_sg"] = {stem .. "u"}
	data.forms["loc_sg"] = {stem .. "u"}
	data.forms["ins_sg"] = {stem .. oe .. "m"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "a"}
	data.forms["acc_du"] = {stem .. "a"}
	data.forms["gen_du"] = {stem .. oe .. "v"}
	data.forms["dat_du"] = {stem .. oe .. "ma"}
	data.forms["loc_du"] = {stem .. "ih"}
	data.forms["ins_du"] = {stem .. oe .. "ma"}
	
	-- Plural
	data.forms["nom_pl"] = {nom_pl or stem .. "i", nom_pl2}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem .. oe .. "v"}
	data.forms["dat_pl"] = {stem .. oe .. "m"}
	data.forms["loc_pl"] = {stem .. "ih"}
	data.forms["ins_pl"] = {stem .. "i"}
end

-- Masculine o-stem, plural in -ôv-
inflections["m-ov"] = function(args, data)
	local stem_noending = args[1]; if stem_noending == "" then stem_noending = nil end
	local stem = args[2]; if stem == "" then stem = nil end
	local gen_sg_ending = args[3]; if gen_sg_ending == "" then gen_sg_ending = nil end
	local anim = args["a"]; if not anim or anim == "" then anim = false else anim = true end
	
	if not stem_noending then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem_noending = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	if not stem then
		stem = stem_noending
		stem_noending = make_endingless_stem(stem)
	end
	
	local stem_noaccents = require("Module:sl-common").remove_accents(stem)
	
	local gen_sg = stem .. "a"
	
	if gen_sg_ending == "á" or gen_sg_ending == "ú" then
		gen_sg = stem_noaccents .. gen_sg_ending
	elseif gen_sg_ending then
		error("3rd parameter (genitive singular ending) must be \"á\" or \"ú\".")
	end
	
	-- Is this a hard stem or a soft stem?
	local oe = "o"
	local hs = "硬音"
	data.info = "陽性" .. (anim and "anim." or "inan.")
	
	if require("Module:sl-common").is_soft(stem) then
		oe = "e"
		hs = "軟音"
	end
	
	data.info = data.info .. "," .. hs .. "o-詞幹"
	table.insert(data.categories, "斯洛文尼亞語陽性" .. hs .. " o-詞幹名詞")
	
	if gen_sg_ending then
		data.info = data.info .. ", mobile accent"
		table.insert(data.categories, "Slovene masculine o-stem nouns with mobile accent")
	end
	
	if data.num ~= "sg" then
		data.info = data.info .. ", plural in " .. require("Module:links").full_link({lang = lang, alt = "-ôv-"}, "term")
		table.insert(data.categories, "Slovene masculine o-stem nouns with plural in -ov-")
	end
	
	-- Categorize further
	local stem_noending_noaccents = require("Module:sl-common").remove_accents(stem_noending)
	local corrected_stem = nil
	
	if stem_noaccents == stem_noending_noaccents then
		corrected_stem = stem_noending
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e([ln]j)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e([ln]j)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e(.)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e(.)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "[aeiou]$", "") then
		table.insert(data.categories, "帶詞尾元音的斯洛文尼亞語陽性o-詞幹名詞")
		corrected_stem = mw.ustring.gsub(stem_noending, "[aeiou]$", "")
	elseif stem_noaccents == stem_noending_noaccents .. "j" then
		table.insert(data.categories, "帶j-中綴的斯洛文尼亞語陽性軟音o-詞幹名詞")
		corrected_stem = stem_noending .. "j"
	elseif stem_noaccents == stem_noending_noaccents .. "-j" then
		table.insert(data.categories, "帶j-中綴的斯洛文尼亞語陽性軟音o-詞幹名詞")
		corrected_stem = stem_noending .. "-j"
	elseif stem_noaccents == stem_noending_noaccents .. "n" then
		table.insert(data.categories, "帶n-中綴的斯洛文尼亞語陽性硬音o-詞幹名詞")
		corrected_stem = stem_noending .. "n"
	elseif stem_noaccents == stem_noending_noaccents .. "t" then
		table.insert(data.categories, "帶t-中綴的斯洛文尼亞語陽性硬音o-詞幹名詞")
		corrected_stem = stem_noending .. "t"
	else
		table.insert(data.categories, "斯洛文尼亞語不規則名詞")
	end
	
	if corrected_stem and stem ~= corrected_stem then
		table.insert(data.categories, "有重音交替的斯洛文尼亞語名詞")
	end
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {anim and gen_sg or stem_noending}
	data.forms["gen_sg"] = {gen_sg}
	data.forms["dat_sg"] = {stem .. "u"}
	data.forms["loc_sg"] = {stem .. "u"}
	data.forms["ins_sg"] = {stem .. oe .. "m"}
	
	-- Dual
	data.forms["nom_du"] = {stem_noaccents .. "ôva"}
	data.forms["acc_du"] = {stem_noaccents .. "ôva"}
	data.forms["gen_du"] = {stem_noaccents .. "ôv"}
	data.forms["dat_du"] = {stem_noaccents .. "ôvoma"}
	data.forms["loc_du"] = {stem_noaccents .. "ôvih"}
	data.forms["ins_du"] = {stem_noaccents .. "ôvoma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem_noaccents .. "ôvi"}
	data.forms["acc_pl"] = {stem_noaccents .. "ôve"}
	data.forms["gen_pl"] = {stem_noaccents .. "ôv"}
	data.forms["dat_pl"] = {stem_noaccents .. "ôvom"}
	data.forms["loc_pl"] = {stem_noaccents .. "ôvih"}
	data.forms["ins_pl"] = {stem_noaccents .. "ôvi"}
end

-- Masculine a-stem
inflections["m-a"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	local anim = args["a"]; if not anim or anim == "" then anim = false else anim = true end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	data.info = "陽性" .. (anim and "anim." or "inan.") .. ",a-詞幹"
	table.insert(data.categories, "斯洛文尼亞語陽性a-詞幹名詞")
	
	-- Singular
	data.forms["nom_sg"] = {stem .. "a"}
	data.forms["acc_sg"] = {stem .. "o"}
	data.forms["gen_sg"] = {stem .. "e"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. "o"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. "ama"}
	data.forms["loc_du"] = {stem .. "ah"}
	data.forms["ins_du"] = {stem .. "ama"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "e"}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. "am"}
	data.forms["loc_pl"] = {stem .. "ah"}
	data.forms["ins_pl"] = {stem .. "ami"}
end

-- Feminine a-stem
inflections["f"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	data.info = "陰性,a-詞幹"
	table.insert(data.categories, "斯洛文尼亞語陰性a-詞幹名詞")
	
	-- Singular
	data.forms["nom_sg"] = {stem .. "a"}
	data.forms["acc_sg"] = {stem .. "o"}
	data.forms["gen_sg"] = {stem .. "e"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. "o"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. "ama"}
	data.forms["loc_du"] = {stem .. "ah"}
	data.forms["ins_du"] = {stem .. "ama"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "e"}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. "am"}
	data.forms["loc_pl"] = {stem .. "ah"}
	data.forms["ins_pl"] = {stem .. "ami"}
end

-- Feminine a-stem, mobile accent paradigm
inflections["f-mobile"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	local gen_pl = args["gen_pl"]; if gen_pl == "" then gen_pl = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noaccents = require("Module:sl-common").remove_accents(stem)
	gen_pl = gen_pl or stem_noaccents .. "á"
	
	data.info = "陰性,a-詞幹,mobile accent"
	table.insert(data.categories, "斯洛文尼亞語陰性a-詞幹名詞")
	table.insert(data.categories, "有可移動重音的斯洛文尼亞語陰性a-詞幹名詞")
	
	-- Singular
	data.forms["nom_sg"] = {stem .. "a"}
	data.forms["acc_sg"] = {stem_noaccents .. "ó"}
	data.forms["gen_sg"] = {stem_noaccents .. "é"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem_noaccents .. "ó"}
	
	-- Dual
	data.forms["nom_du"] = {stem_noaccents .. "é"}
	data.forms["acc_du"] = {stem_noaccents .. "é"}
	data.forms["gen_du"] = {gen_pl}
	data.forms["dat_du"] = {stem_noaccents .. "áma"}
	data.forms["loc_du"] = {stem_noaccents .. "àh"}
	data.forms["ins_du"] = {stem_noaccents .. "áma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem_noaccents .. "é"}
	data.forms["acc_pl"] = {stem_noaccents .. "é"}
	data.forms["gen_pl"] = {gen_pl}
	data.forms["dat_pl"] = {stem_noaccents .. "àm"}
	data.forms["loc_pl"] = {stem_noaccents .. "àh"}
	data.forms["ins_pl"] = {stem_noaccents .. "ámi"}
end

-- Feminine v-stem
inflections["f-v"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = stem .. "ev"
	stem = stem .. "v"
	
	data.info = "陰性,v-詞幹"
	table.insert(data.categories, "斯洛文尼亞語陰性v-詞幹名詞")
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {stem_noending}
	data.forms["gen_sg"] = {stem .. "e"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. "ijo"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. "ama"}
	data.forms["loc_du"] = {stem .. "ah"}
	data.forms["ins_du"] = {stem .. "ama"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "e"}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. "am"}
	data.forms["loc_pl"] = {stem .. "ah"}
	data.forms["ins_pl"] = {stem .. "ami"}
end

-- Feminine i-stem
inflections["f-i"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	data.info = "陰性,i-詞幹"
	table.insert(data.categories, "斯洛文尼亞語陰性i-詞幹名詞")
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {stem_noending}
	data.forms["gen_sg"] = {stem .. "i"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. (stem ~= stem_noending and "i" or "") .. "jo"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem .. "i"}
	data.forms["dat_du"] = {stem .. (stem ~= stem_noending and "i" or "") .. "ma"}
	data.forms["loc_du"] = {stem .. "ih"}
	data.forms["ins_du"] = {stem .. (stem ~= stem_noending and "i" or "") .. "ma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "i"}
	data.forms["acc_pl"] = {stem .. "i"}
	data.forms["gen_pl"] = {stem .. "i"}
	data.forms["dat_pl"] = {stem .. "im"}
	data.forms["loc_pl"] = {stem .. "ih"}
	data.forms["ins_pl"] = {stem .. (stem ~= stem_noending and "i" or "") .. "mi"}
end

-- Feminine i-stem, mobile accent paradigm
inflections["f-i-mobile"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	local stem_dat_loc_sg = args[2]; if stem_dat_loc_sg == "" then stem_dat_loc_sg = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	local stem_noaccents = require("Module:sl-common").remove_accents(stem)
	stem_dat_loc_sg = stem_dat_loc_sg or stem
	
	data.info = "陰性,i-詞幹,mobile accent"
	table.insert(data.categories, "斯洛文尼亞語陰性i-詞幹名詞")
	table.insert(data.categories, "有可移動重音的斯洛文尼亞語陰性i-詞幹名詞")
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {stem_noending}
	data.forms["gen_sg"] = {stem_noaccents .. "í"}
	data.forms["dat_sg"] = {stem_dat_loc_sg .. "i"}
	data.forms["loc_sg"] = {stem_dat_loc_sg .. "i"}
	data.forms["ins_sg"] = {stem_noaccents .. (stem ~= stem_noending and "i" or "") .. "jó"}
	
	-- Dual
	data.forms["nom_du"] = {stem_noaccents .. "í"}
	data.forms["acc_du"] = {stem_noaccents .. "í"}
	data.forms["gen_du"] = {stem_noaccents .. "í"}
	data.forms["dat_du"] = {stem_noaccents .. "éma"}
	data.forms["loc_du"] = {stem_noaccents .. "éh"}
	data.forms["ins_du"] = {stem_noaccents .. "éma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem_noaccents .. "í"}
	data.forms["acc_pl"] = {stem_noaccents .. "í"}
	data.forms["gen_pl"] = {stem_noaccents .. "í"}
	data.forms["dat_pl"] = {stem_noaccents .. "ém"}
	data.forms["loc_pl"] = {stem_noaccents .. "éh"}
	data.forms["ins_pl"] = {stem_noaccents .. (stem ~= stem_noending and "i" or "") .. "mí"}
end

-- Neuter o-stem
inflections["n"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	-- Is this a hard stem or a soft stem?
	local oe = "o"
	data.info = "中性"
	
	if require("Module:sl-common").is_soft(stem) then
		oe = "e"
		data.info = data.info .. ",軟音"
		table.insert(data.categories, "斯洛文尼亞語中性軟音o-詞幹名詞")
	else
		data.info = data.info .. ",硬音"
		table.insert(data.categories, "斯洛文尼亞語中性硬音o-詞幹名詞")
	end
	
	-- Singular
	data.forms["nom_sg"] = {stem .. oe}
	data.forms["acc_sg"] = {stem .. oe}
	data.forms["gen_sg"] = {stem .. "a"}
	data.forms["dat_sg"] = {stem .. "u"}
	data.forms["loc_sg"] = {stem .. "u"}
	data.forms["ins_sg"] = {stem .. oe .. "m"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. oe .. "ma"}
	data.forms["loc_du"] = {stem .. "ih"}
	data.forms["ins_du"] = {stem .. oe .. "ma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "a"}
	data.forms["acc_pl"] = {stem .. "a"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. oe .. "m"}
	data.forms["loc_pl"] = {stem .. "ih"}
	data.forms["ins_pl"] = {stem .. "i"}
end

function make_endingless_stem(stem)
	local stem_noaccents = mw.ustring.lower(require("Module:sl-common").remove_accents(stem))
	
	if mw.ustring.find(stem_noaccents, "[^aeioujlrv][ln]j$") then
		return mw.ustring.gsub(stem, "([ln]j)$", "e%1")
	elseif mw.ustring.find(stem_noaccents, "[^aeiouln]j$") then
		return mw.ustring.gsub(stem, "j$", "ij")
	elseif mw.ustring.find(stem_noaccents, "[^aeiou]r$") or mw.ustring.find(stem_noaccents, "[^aeioujlrv][lmnv]$") then
		return mw.ustring.gsub(stem, "(.)$", "e%1")
	else
		return stem
	end
end

-- Make the table
function make_table(data)
	local function repl(param)
		local accel = false  -- Temporary
		local no_store = false
		
		if param == "info" then
			return mw.getContentLanguage():ucfirst(data.info or "")
		elseif string.sub(param, 1, 1) == "!" then
			no_store = true
			param = string.sub(param, 2)
		elseif string.sub(param, 1, 1) == "#" then
			accel = false
			param = string.sub(param, 2)
		end
		
		local forms = data.forms[param]
		
		if not forms then
			return "—"
		end
		
		local ret = {}
		
		for key, subform in ipairs(forms) do
			table.insert(ret, require("Module:links").full_link({lang = lang, term = subform, accel = accel and {form = param, no_store = no_store} or nil}))
		end
		
		return table.concat(ret, "<br/>")
	end
	
	local wikicode
	
	if data.num then
		wikicode = [=[
{| class="inflection-table vsSwitcher vsToggleCategory-inflection" style="background: #F9F9F9; border: 1px solid #aaaaaa;"
|- style="background: #d9ebff; text-align: left;"
! class="vsToggleElement" colspan="2" | {{{info}}}
|- class="vsShow"
! style="width: 11em; background: #eff7ff;" | 主格
| style="width: 11em;" colspan="2" | {{{!nom_]=] .. data.num .. [=[}}}
|- class="vsShow"
! style="background: #eff7ff;" | 屬格
| colspan="2" | {{{!gen_]=] .. data.num .. [=[}}}
|- class="vsHide"
! style="background: #d9ebff; width: 11em;" | 
! style="background: #d9ebff; width: 11em;" | ]=] .. (data.num == "pl" and "複數" or data.num == "du" and "雙數" or "單數") .. [=[

|- class="vsHide"
! style="background:#eff7ff" | 主格
| {{{nom_]=] .. data.num .. [=[}}}
|- class="vsHide"
! style="background:#eff7ff" | 賓格
| {{{acc_]=] .. data.num .. [=[}}}
|- class="vsHide"
! style="background:#eff7ff" | 屬格
| {{{gen_]=] .. data.num .. [=[}}}
|- class="vsHide"
! style="background:#eff7ff" | 與格
| {{{dat_]=] .. data.num .. [=[}}}
|- class="vsHide"
! style="background:#eff7ff" | 方位格
| {{{loc_]=] .. data.num .. [=[}}}
|- class="vsHide"
! style="background:#eff7ff" | 工具格
| {{{ins_]=] .. data.num .. [=[}}}
|}]=]
	else
		wikicode = [=[
{| class="inflection-table vsSwitcher vsToggleCategory-inflection" style="background: #F9F9F9; border: 1px solid #aaaaaa;"
|- style="background: #d9ebff; text-align: left;"
! class="vsToggleElement" colspan="4" | {{{info}}}
|- class="vsShow"
! style="width: 11em; background: #eff7ff;" | 主格單數
| style="width: 11em;" colspan="3" | {{{!nom_sg}}}
|- class="vsShow"
! style="background: #eff7ff;" | 屬格單數
| colspan="3" | {{{!gen_sg}}}
|- class="vsHide"
! style="background: #d9ebff; width: 11em;" | 
! style="background: #d9ebff; width: 11em;" | 單數
! style="background: #d9ebff; width: 11em;" | 雙數
! style="background: #d9ebff; width: 11em;" | 複數
|- class="vsHide"
! style="background: #eff7ff;" | 主格
| {{{nom_sg}}}
| {{{nom_du}}}
| {{{nom_pl}}}
|- class="vsHide"
! style="background: #eff7ff;" | 賓格
| {{{acc_sg}}}
| {{{acc_du}}}
| {{{acc_pl}}}
|- class="vsHide"
! style="background: #eff7ff;" | 屬格
| {{{gen_sg}}}
| {{{gen_du}}}
| {{{gen_pl}}}
|- class="vsHide"
! style="background: #eff7ff;" | 與格
| {{{dat_sg}}}
| {{{dat_du}}}
| {{{dat_pl}}}
|- class="vsHide"
! style="background: #eff7ff;" | 方位格
| {{{loc_sg}}}
| {{{loc_du}}}
| {{{loc_pl}}}
|- class="vsHide"
! style="background: #eff7ff;" | 工具格
| {{{ins_sg}}}
| {{{ins_du}}}
| {{{ins_pl}}}
|}]=]
	end
	
	return mw.ustring.gsub(wikicode, "{{{([^}]+)}}}", repl) .. require("Module:utilities").format_categories(data.categories, lang)
end

return export