local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
local m_translit = require("Module:Goth-translit")

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

local export = {}


local function postprocess(args, data)
	data.sg = true
	data.pl = true
	
	if args["n"] == "pl" then
		data.sg = false
		table.insert(data.categories, lang:getCanonicalName() .. " pluralia tantum")
	elseif args["n"] == "sg" then
		data.pl = false
		table.insert(data.categories, lang:getCanonicalName() .. " uncountable nouns")
	end
	
	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"] == "pl" and key:find("_sg$")) or (args["n"] == "sg" and key:find("_pl$")) then
			form = nil
		end
		
		data.forms[key] = form
	end
	
	-- Check if the lemma form matches the page name
	if data.forms["nom_" .. (args["n"] or "sg")] and (lang:makeEntryName(m_translit.tr(data.forms["nom_" .. (args["n"] or "sg")][1]))) ~= mw.title.getCurrentTitle().text then
		table.insert(data.categories, lang:getCanonicalName() .. " entries with inflection not matching pagename")
	end
end


local function make_Latn(args, names)
	for _, name in ipairs(names) do
		if args[name] then
			if type(args[name]) == "table" then
				for i, _ in ipairs(args[name]) do
					args[name][i] = m_translit.tr(args[name][i])
				end
			else
				args[name] = m_translit.tr(args[name])
			end
		end
	end
end


-- Inflection functions

export["a-m"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		[2] = {},
		["nomsg"] = {},
		
		["j"] = {},
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, 2, "nomsg"})
	
	if args["j"] and not (args["j"] == "ji" or args["j"] == "ei") then
		error("Invalid value for parameter j=, must be \"ji\", \"ei\" or empty.")
	end
	
	local data = {forms = {}, info = "masculine " .. (args["j"] and "j" or "") .. "a-stem", categories = {lang:getCanonicalName() .. " masculine a-stem nouns"}}
	
	data.forms["nom_sg"] = {args["nomsg"] or ((args[2] or args[1]) .. (args["j"] or "") .. "s")}
	data.forms["voc_sg"] = {(args[2] or args[1]) .. (args["j"] and "i" or "")}
	data.forms["acc_sg"] = {(args[2] or args[1]) .. (args["j"] and "i" or "")}
	data.forms["gen_sg"] = {args[1] .. (args["j"] or "i") .. "s"}
	data.forms["dat_sg"] = {args[1] .. (args["j"] and "j" or "") .. "a"}
	
	data.forms["nom_pl"] = {args[1] .. (args["j"] and "j" or "") .. "ōs"}
	data.forms["voc_pl"] = {args[1] .. (args["j"] and "j" or "") .. "ōs"}
	data.forms["acc_pl"] = {args[1] .. (args["j"] and "j" or "") .. "ans"}
	data.forms["gen_pl"] = {args[1] .. (args["j"] and "j" or "") .. "ē"}
	data.forms["dat_pl"] = {args[1] .. (args["j"] and "j" or "") .. "am"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["a-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		[2] = {},
		
		["j"] = {},
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, 2})
	
	if args["j"] and not (args["j"] == "ji" or args["j"] == "ei") then
		error("Invalid value for parameter j=, must be \"ji\", \"ei\" or empty.")
	end
	
	local data = {forms = {}, info = "neuter " .. (args["j"] and "j" or "") .. "a-stem", categories = {lang:getCanonicalName() .. " neuter a-stem nouns"}}
	
	data.forms["nom_sg"] = {(args[2] or args[1]) .. (args["j"] and "i" or "")}
	data.forms["voc_sg"] = {(args[2] or args[1]) .. (args["j"] and "i" or "")}
	data.forms["acc_sg"] = {(args[2] or args[1]) .. (args["j"] and "i" or "")}
	data.forms["gen_sg"] = {args[1] .. (args["j"] or "i") .. "s"}
	data.forms["dat_sg"] = {args[1] .. (args["j"] and "j" or "") .. "a"}
	
	data.forms["nom_pl"] = {args[1] .. (args["j"] and "j" or "") .. "a"}
	data.forms["voc_pl"] = {args[1] .. (args["j"] and "j" or "") .. "a"}
	data.forms["acc_pl"] = {args[1] .. (args["j"] and "j" or "") .. "a"}
	data.forms["gen_pl"] = {args[1] .. (args["j"] and "j" or "") .. "ē"}
	data.forms["dat_pl"] = {args[1] .. (args["j"] and "j" or "") .. "am"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["an-m"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "masculine an-stem", categories = {lang:getCanonicalName() .. " masculine an-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "a"}
	data.forms["voc_sg"] = {args[1] .. "a"}
	data.forms["acc_sg"] = {args[1] .. "an"}
	data.forms["gen_sg"] = {args[1] .. "ins"}
	data.forms["dat_sg"] = {args[1] .. "in"}
	
	data.forms["nom_pl"] = {args[1] .. "ans"}
	data.forms["voc_pl"] = {args[1] .. "ans"}
	data.forms["acc_pl"] = {args[1] .. "ans"}
	data.forms["gen_pl"] = {args[1] .. "anē"}
	data.forms["dat_pl"] = {args[1] .. "am"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["an-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "neuter an-stem", categories = {lang:getCanonicalName() .. " neuter an-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "ō"}
	data.forms["voc_sg"] = {args[1] .. "ō"}
	data.forms["acc_sg"] = {args[1] .. "ō"}
	data.forms["gen_sg"] = {args[1] .. "ins"}
	data.forms["dat_sg"] = {args[1] .. "in"}
	
	data.forms["nom_pl"] = {args[1] .. "ōna"}
	data.forms["voc_pl"] = {args[1] .. "ōna"}
	data.forms["acc_pl"] = {args[1] .. "ōna"}
	data.forms["gen_pl"] = {args[1] .. "anē"}
	data.forms["dat_pl"] = {args[1] .. "am"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["cons-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		["datpl"] = {},
		["nomsg"] = {},
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, "nomsg", "datpl"})
	
	local data = {forms = {}, info = "masculine/feminine consonant stem", categories = {lang:getCanonicalName() .. " consonant stem nouns"}}
	
	data.forms["nom_sg"] = {args["nomsg"] or args[1] .. "s"}
	data.forms["voc_sg"] = {args[1] .. ""}
	data.forms["acc_sg"] = {args[1] .. ""}
	data.forms["gen_sg"] = {args[1] .. "s"}
	data.forms["dat_sg"] = {args[1] .. ""}
	
	data.forms["nom_pl"] = {args[1] .. "s"}
	data.forms["voc_pl"] = {args[1] .. "s"}
	data.forms["acc_pl"] = {args[1] .. "s"}
	data.forms["gen_pl"] = {args[1] .. "ē"}
	data.forms["dat_pl"] = {args["datpl"] or args[1] .. "um"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["eins"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		[2] = {},
		["nomsg"] = {},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, 2, "nomsg"})
	
	local data = {forms = {}, info = "feminine i/ō-stem", categories = {lang:getCanonicalName() .. " i/ō-stem nouns"}}
	
	data.forms["nom_sg"] = {args["nomsg"] or (args[2] or args[1]) .. "s"}
	data.forms["voc_sg"] = {(args[2] or args[1]) .. ""}
	data.forms["acc_sg"] = {(args[2] or args[1]) .. ""}
	data.forms["gen_sg"] = {args[1] .. "ais"}
	data.forms["dat_sg"] = {args[1] .. "ai"}
	
	data.forms["nom_pl"] = {args[1] .. "ōs"}
	data.forms["voc_pl"] = {args[1] .. "ōs"}
	data.forms["acc_pl"] = {args[1] .. "ins"}
	data.forms["gen_pl"] = {args[1] .. "ō"}
	data.forms["dat_pl"] = {args[1] .. "im"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["i-f"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		[2] = {},
		["nomsg"] = {},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, 2, "nomsg"})
	
	local data = {forms = {}, info = "feminine i-stem", categories = {lang:getCanonicalName() .. " feminine i-stem nouns"}}
	
	data.forms["nom_sg"] = {args["nomsg"] or (args[2] or args[1]) .. "s"}
	data.forms["voc_sg"] = {(args[2] or args[1]) .. ""}
	data.forms["acc_sg"] = {(args[2] or args[1]) .. ""}
	data.forms["gen_sg"] = {args[1] .. "ais"}
	data.forms["dat_sg"] = {args[1] .. "ai"}
	
	data.forms["nom_pl"] = {args[1] .. "eis"}
	data.forms["voc_pl"] = {args[1] .. "eis"}
	data.forms["acc_pl"] = {args[1] .. "ins"}
	data.forms["gen_pl"] = {args[1] .. "ē"}
	data.forms["dat_pl"] = {args[1] .. "im"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["i-m"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		[2] = {},
		["nomsg"] = {},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, 2, "nomsg"})
	
	local data = {forms = {}, info = "masculine i-stem", categories = {lang:getCanonicalName() .. " masculine i-stem nouns"}}
	
	data.forms["nom_sg"] = {args["nomsg"] or (args[2] or args[1]) .. "s"}
	data.forms["voc_sg"] = {(args[2] or args[1]) .. ""}
	data.forms["acc_sg"] = {(args[2] or args[1]) .. ""}
	data.forms["gen_sg"] = {args[1] .. "is"}
	data.forms["dat_sg"] = {args[1] .. "a"}
	
	data.forms["nom_pl"] = {args[1] .. "eis"}
	data.forms["voc_pl"] = {args[1] .. "eis"}
	data.forms["acc_pl"] = {args[1] .. "ins"}
	data.forms["gen_pl"] = {args[1] .. "ē"}
	data.forms["dat_pl"] = {args[1] .. "im"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["in-f"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "feminine īn-stem", categories = {lang:getCanonicalName() .. " īn-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "ei"}
	data.forms["voc_sg"] = {args[1] .. "ei"}
	data.forms["acc_sg"] = {args[1] .. "ein"}
	data.forms["gen_sg"] = {args[1] .. "eins"}
	data.forms["dat_sg"] = {args[1] .. "ein"}
	
	data.forms["nom_pl"] = {args[1] .. "eins"}
	data.forms["voc_pl"] = {args[1] .. "eins"}
	data.forms["acc_pl"] = {args[1] .. "eins"}
	data.forms["gen_pl"] = {args[1] .. "einō"}
	data.forms["dat_pl"] = {args[1] .. "eim"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["loanword"] = function(frame)
	local params = {
		["nom_sg"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{nom_sg}}}" or nil},
		["voc_sg"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{voc_sg}}}" or nil},
		["acc_sg"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{acc_sg}}}" or nil},
		["gen_sg"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{gen_sg}}}" or nil},
		["dat_sg"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{dat_sg}}}" or nil},
		
		["nom_pl"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{nom_pl}}}" or nil},
		["voc_pl"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{voc_pl}}}" or nil},
		["acc_pl"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{acc_pl}}}" or nil},
		["gen_pl"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{gen_pl}}}" or nil},
		["dat_pl"] = {list = true, default = mw.title.getCurrentTitle().nsText == "Template" and "{{{dat_pl}}}" or nil},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {"nom_sg", "voc_sg", "acc_sg", "gen_sg", "dat_sg", "nom_pl", "voc_pl", "acc_pl", "gen_pl", "dat_pl"})
	
	local data = {forms = {}, info = "loanword; irregular/mixed declension", categories = {lang:getCanonicalName() .. " irregularly declined borrowed nouns"}}
	
	data.forms["nom_sg"] = args["nom_sg"]
	data.forms["voc_sg"] = args["voc_sg"]
	data.forms["acc_sg"] = args["acc_sg"]
	data.forms["gen_sg"] = args["gen_sg"]
	data.forms["dat_sg"] = args["dat_sg"]
	
	data.forms["nom_pl"] = args["nom_pl"]
	data.forms["voc_pl"] = args["voc_pl"]
	data.forms["acc_pl"] = args["acc_pl"]
	data.forms["gen_pl"] = args["gen_pl"]
	data.forms["dat_pl"] = args["dat_pl"]
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["nd-m"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "masculine nd-stem", categories = {lang:getCanonicalName() .. " nd-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "s"}
	data.forms["voc_sg"] = {args[1] .. ""}
	data.forms["acc_sg"] = {args[1] .. ""}
	data.forms["gen_sg"] = {args[1] .. "is"}
	data.forms["dat_sg"] = {args[1] .. ""}
	
	data.forms["nom_pl"] = {args[1] .. "s"}
	data.forms["voc_pl"] = {args[1] .. "s"}
	data.forms["acc_pl"] = {args[1] .. "s"}
	data.forms["gen_pl"] = {args[1] .. "ē"}
	data.forms["dat_pl"] = {args[1] .. "am"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["o-f"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		["nomsg"] = {},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1, "nomsg"})
	
	local data = {forms = {}, info = "feminine ō-stem", categories = {lang:getCanonicalName() .. " ō-stem nouns"}}
	
	data.forms["nom_sg"] = {args["nomsg"] or args[1] .. "a"}
	data.forms["voc_sg"] = {args["nomsg"] or args[1] .. "a"}
	data.forms["acc_sg"] = {args[1] .. "a"}
	data.forms["gen_sg"] = {args[1] .. "ōs"}
	data.forms["dat_sg"] = {args[1] .. "ai"}
	
	data.forms["nom_pl"] = {args[1] .. "ōs"}
	data.forms["voc_pl"] = {args[1] .. "ōs"}
	data.forms["acc_pl"] = {args[1] .. "ōs"}
	data.forms["gen_pl"] = {args[1] .. "ō"}
	data.forms["dat_pl"] = {args[1] .. "ōm"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["on-f"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "feminine ōn-stem", categories = {lang:getCanonicalName() .. " ōn-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "ō"}
	data.forms["voc_sg"] = {args[1] .. "ō"}
	data.forms["acc_sg"] = {args[1] .. "ōn"}
	data.forms["gen_sg"] = {args[1] .. "ōns"}
	data.forms["dat_sg"] = {args[1] .. "ōn"}
	
	data.forms["nom_pl"] = {args[1] .. "ōns"}
	data.forms["voc_pl"] = {args[1] .. "ōns"}
	data.forms["acc_pl"] = {args[1] .. "ōns"}
	data.forms["gen_pl"] = {args[1] .. "ōnō"}
	data.forms["dat_pl"] = {args[1] .. "ōm"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["r-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "masculine/feminine r-stem", categories = {lang:getCanonicalName() .. " r-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "ar"}
	data.forms["voc_sg"] = {args[1] .. "ar"}
	data.forms["acc_sg"] = {args[1] .. "ar"}
	data.forms["gen_sg"] = {args[1] .. "rs"}
	data.forms["dat_sg"] = {args[1] .. "r"}
	
	data.forms["nom_pl"] = {args[1] .. "rjus"}
	data.forms["voc_pl"] = {args[1] .. "rjus"}
	data.forms["acc_pl"] = {args[1] .. "runs"}
	data.forms["gen_pl"] = {args[1] .. "rē"}
	data.forms["dat_pl"] = {args[1] .. "rum"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["u-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})
	
	local data = {forms = {}, info = "masculine/feminine u-stem", categories = {lang:getCanonicalName() .. " masculine/feminine u-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "us"}
	data.forms["voc_sg"] = {args[1] .. "au"}
	data.forms["acc_sg"] = {args[1] .. "u"}
	data.forms["gen_sg"] = {args[1] .. "aus"}
	data.forms["dat_sg"] = {args[1] .. "au"}
	
	data.forms["nom_pl"] = {args[1] .. "jus"}
	data.forms["voc_pl"] = {args[1] .. "jus"}
	data.forms["acc_pl"] = {args[1] .. "uns"}
	data.forms["gen_pl"] = {args[1] .. "iwē"}
	data.forms["dat_pl"] = {args[1] .. "um"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["u-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	make_Latn(args, {1})

	local data = {forms = {}, info = "neuter u-stem", categories = {lang:getCanonicalName() .. " neuter u-stem nouns"}}
	
	args["n"] = "sg"  -- No plural forms attested
	data.forms["nom_sg"] = {args[1] .. "u"}
	data.forms["voc_sg"] = {args[1] .. "u"}
	data.forms["acc_sg"] = {args[1] .. "u"}
	data.forms["gen_sg"] = {args[1] .. "aus"}
	data.forms["dat_sg"] = {args[1] .. "au"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


-- Irregular nouns

export["aba"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine an-stem, contraction in plural", categories = {lang:getCanonicalName() .. " an-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"aba"}
	data.forms["voc_sg"] = {"*aba"}
	data.forms["acc_sg"] = {"aban"}
	data.forms["gen_sg"] = {"abins"}
	data.forms["dat_sg"] = {"abin"}
	
	data.forms["nom_pl"] = {"abans"}
	data.forms["voc_pl"] = {"*abans"}
	data.forms["acc_pl"] = {"*abans"}
	data.forms["gen_pl"] = {"abnē"}
	data.forms["dat_pl"] = {"abnam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["aiws"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine mixed i/a-stem", categories = {lang:getCanonicalName() .. " i-stem nouns", lang:getCanonicalName() .. " a-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"aiws"}
	data.forms["voc_sg"] = {"aiw"}
	data.forms["acc_sg"] = {"aiw"}
	data.forms["gen_sg"] = {"aiwis"}
	data.forms["dat_sg"] = {"aiwa"}
	
	data.forms["nom_pl"] = {"*aiweis", "*aiwōs"}
	data.forms["voc_pl"] = {"*aiweis", "*aiwōs"}
	data.forms["acc_pl"] = {"aiwins"}
	data.forms["gen_pl"] = {"aiwē"}
	data.forms["dat_pl"] = {"aiwam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["andeis"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine mixed i/a-stem", categories = {lang:getCanonicalName() .. " i-stem nouns", lang:getCanonicalName() .. " a-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"andeis"}
	data.forms["voc_sg"] = {"*andi"}
	data.forms["acc_sg"] = {"andi"}
	data.forms["gen_sg"] = {"*andeis"}
	data.forms["dat_sg"] = {"andja"}
	
	data.forms["nom_pl"] = {"*andjōs"}
	data.forms["voc_pl"] = {"*andjōs"}
	data.forms["acc_pl"] = {"andins"}
	data.forms["gen_pl"] = {"*andjē", "*andē"}
	data.forms["dat_pl"] = {"andjam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["auhsa"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine mixed an/u stem, contraction in the plural", categories = {lang:getCanonicalName() .. " an-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"auhsa"}
	data.forms["voc_sg"] = {"*auhsa"}
	data.forms["acc_sg"] = {"auhsau"}
	data.forms["gen_sg"] = {"*auhsaus?"}
	data.forms["dat_sg"] = {"auhsau"}
	
	data.forms["nom_pl"] = {"*auhsnjus"}
	data.forms["voc_pl"] = {"*auhsnjus"}
	data.forms["acc_pl"] = {"auhsnuns"}
	data.forms["gen_pl"] = {"auhsne"}
	data.forms["dat_pl"] = {"*auhsum"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["fon"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "neuter an-stem, irregular nominative", categories = {lang:getCanonicalName() .. " an-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	args["n"] = "sg"
	data.forms["nom_sg"] = {"fōn"}
	data.forms["voc_sg"] = {"fōn"}
	data.forms["acc_sg"] = {"fōn"}
	data.forms["gen_sg"] = {"funins"}
	data.forms["dat_sg"] = {"funin"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["haims"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "feminine mixed i/ō-stem", categories = {lang:getCanonicalName() .. " i/ō-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"*haims"}
	data.forms["voc_sg"] = {"*haim"}
	data.forms["acc_sg"] = {"haim"}
	data.forms["gen_sg"] = {"*haimais"}
	data.forms["dat_sg"] = {"haimai"}
	
	data.forms["nom_pl"] = {"*haimōs"}
	data.forms["voc_pl"] = {"*haimōs"}
	data.forms["acc_pl"] = {"haimōs"}
	data.forms["gen_pl"] = {"haimō"}
	data.forms["dat_pl"] = {"haimōm"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["manna"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine mixed an/consonant stem", categories = {lang:getCanonicalName() .. " an-stem nouns", lang:getCanonicalName() .. " consonant stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"manna"}
	data.forms["voc_sg"] = {"manna"}
	data.forms["acc_sg"] = {"mannan"}
	data.forms["gen_sg"] = {"mans"}
	data.forms["dat_sg"] = {"mann"}
	
	data.forms["nom_pl"] = {"mans", "mannans"}
	data.forms["voc_pl"] = {"mans", "mannans"}
	data.forms["acc_pl"] = {"mans", "mannans"}
	data.forms["gen_pl"] = {"mannē"}
	data.forms["dat_pl"] = {"mannam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["namo"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "neuter an-stem, contraction in plural", categories = {lang:getCanonicalName() .. " an-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"namō"}
	data.forms["voc_sg"] = {"namō"}
	data.forms["acc_sg"] = {"namō"}
	data.forms["gen_sg"] = {"namins"}
	data.forms["dat_sg"] = {"namin"}
	
	data.forms["nom_pl"] = {"namna"}
	data.forms["voc_pl"] = {"namna"}
	data.forms["acc_pl"] = {"namna"}
	data.forms["gen_pl"] = {"namnē"}
	data.forms["dat_pl"] = {"namnam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["reiks"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine mixed consonant/a-stem", categories = {lang:getCanonicalName() .. " consonant stem nouns", lang:getCanonicalName() .. " a-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"reiks"}
	data.forms["voc_sg"] = {"*reik"}
	data.forms["acc_sg"] = {"*reik"}
	data.forms["gen_sg"] = {"reikis"}
	data.forms["dat_sg"] = {"reik"}
	
	data.forms["nom_pl"] = {"reiks"}
	data.forms["voc_pl"] = {"*reiks"}
	data.forms["acc_pl"] = {"*reiks"}
	data.forms["gen_pl"] = {"reikē"}
	data.forms["dat_pl"] = {"reikam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["wato"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "neuter an-stem, contraction in plural", categories = {lang:getCanonicalName() .. " an-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"watō"}
	data.forms["voc_sg"] = {"watō"}
	data.forms["acc_sg"] = {"watō"}
	data.forms["gen_sg"] = {"watins"}
	data.forms["dat_sg"] = {"watin"}
	
	data.forms["nom_pl"] = {"*watna"}
	data.forms["voc_pl"] = {"*watna"}
	data.forms["acc_pl"] = {"*watna"}
	data.forms["gen_pl"] = {"*watnē"}
	data.forms["dat_pl"] = {"watnam"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["wegs"] = function(frame)
	local params = {
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine mixed i/a-stem", categories = {lang:getCanonicalName() .. " i-stem nouns", lang:getCanonicalName() .. " a-stem nouns", lang:getCanonicalName() .. " irregular nouns"}}
	
	data.forms["nom_sg"] = {"wēgs"}
	data.forms["voc_sg"] = {"*wēg"}
	data.forms["acc_sg"] = {"*wēg"}
	data.forms["gen_sg"] = {"*wēgis"}
	data.forms["dat_sg"] = {"wēga"}
	
	data.forms["nom_pl"] = {"wēgōs"}
	data.forms["voc_pl"] = {"wēgōs"}
	data.forms["acc_pl"] = {"*wēgans", "*wēgins"}
	data.forms["gen_pl"] = {"*wēgē"}
	data.forms["dat_pl"] = {"wēgim"}
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
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_Goth = {}
		local ret_Latn = {}
		
		for key, subform in ipairs(form) do
			table.insert(ret_Latn, m_links.full_link({lang = lang, term = subform, tr = "-"}))
			table.insert(ret_Goth, m_links.full_link({lang = lang, term = m_translit.tr(subform), tr = "-"}))
		end
		
		return table.concat(ret_Goth, ", ") .. "<br/>" ..  table.concat(ret_Latn, ", ")
	end
	
	local names = {
		["nom"] = "กรรตุการก",
		["voc"] = "สัมโพธนการก",
		["acc"] = "กรรมการก",
		["gen"] = "สัมพันธการก",
		["dat"] = "สัมปทานการก",
		
		["sg"] = "เอกพจน์",
		["pl"] = "พหูพจน์",
	}
	
	local numbers = {"sg", "pl"}
	local cases = {"nom", "voc", "acc", "gen", "dat"}
	
	local wikicode = {}
	
	table.insert(wikicode, "{| class=\"wikitable inflection-table vsSwitcher\" style=\"border-style: double; border-width: 3px;\" cellspacing=\"1\"")
	table.insert(wikicode, "|-")
	table.insert(wikicode, "! class=\"vsToggleElement\" style=\"background: #CCC; min-width: 32em; text-align: left;\" colspan=\"3\" | {{{info}}}")
	
	table.insert(wikicode, "|- class=\"vsHide\"")
	table.insert(wikicode, "| style=\"min-width: 8em;\" |")
	
	for _, number in ipairs(numbers) do
		table.insert(wikicode, "! style=\"min-width: 12em;\" | " .. mw.getContentLanguage():ucfirst(names[number]))
	end
	
	for _, case in ipairs(cases) do
		table.insert(wikicode, "|- class=\"vsHide\"")
		table.insert(wikicode, "! " .. mw.getContentLanguage():ucfirst(names[case]))
		
		for _, number in ipairs(numbers) do
			table.insert(wikicode, "| {{{" .. case .. "_" .. number .. "}}}")
		end
	end
	
	table.insert(wikicode, "|}")
	
	wikicode = table.concat(wikicode, "\n")
	
	return mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl)
end

return export