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

local m_utilities = require("Module:utilities")
local m_links = require("Module:links")

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

local export = {}


function export.adjective(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "adjective concord", categories = {}}
	
	if args[1] == "L" or args[1] == "H" or args[1] == "LH" or args[1] == "HH" or args[1] == "HHH" then
		data.info = data.info .. ", tone " .. args[1]
		table.insert(data.categories, lang:getCanonicalName() .. " adjectives with tone " .. args[1])
	elseif args[1] then
		error("The tone must be \"L\", \"H\", \"LH\", \"HH\" or \"HHH\".")
	else
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " adjective entries")
	end
	
	local base = mw.title.getCurrentTitle().subpageText
	
	-- This variable needs a better name.
	local x = (mw.ustring.find(base, "^[bfp]") and "m" or mw.ustring.find(base, "^n") and "" or "n")
		.. base:gsub("^([ptk])h", "%1"):gsub("^hl", "tl")
	
	data.forms["mod_1sg_pos"] = {"endim" .. base}
	data.forms["mod_2sg_pos"] = {"om" .. base}
	data.forms["mod_1pl_pos"] = {"esiba" .. base}
	data.forms["mod_2pl_pos"] = {"eniba" .. base}
	data.forms["mod_c1_pos"]  = {"om" .. base}
	data.forms["mod_c2_pos"]  = {"aba" .. base}
	data.forms["mod_c3_pos"]  = {"om" .. base}
	data.forms["mod_c4_pos"]  = {"emi" .. base}
	data.forms["mod_c5_pos"]  = {"eli" .. base}
	data.forms["mod_c6_pos"]  = {"ama" .. base}
	data.forms["mod_c7_pos"]  = {"esi" .. base}
	data.forms["mod_c8_pos"]  = {"ezi" .. x}
	data.forms["mod_c9_pos"]  = {"e"   .. x}
	data.forms["mod_c10_pos"] = {"ezi" .. x}
	data.forms["mod_c11_pos"] = {"olu" .. base}
	data.forms["mod_c14_pos"] = {"obu" .. base}
	data.forms["mod_c15_pos"] = {"oku" .. base}
	data.forms["mod_c17_pos"] = {"oku" .. base}
	
	data.forms["mod_1sg_neg"] = {"endingem" .. base}
	data.forms["mod_2sg_neg"] = {"ongem" .. base}
	data.forms["mod_1pl_neg"] = {"esingeba" .. base}
	data.forms["mod_2pl_neg"] = {"eningeba" .. base}
	data.forms["mod_c1_neg"]  = {"ongem" .. base}
	data.forms["mod_c2_neg"]  = {"abangeba" .. base}
	data.forms["mod_c3_neg"]  = {"ongem" .. base}
	data.forms["mod_c4_neg"]  = {"engemi" .. base}
	data.forms["mod_c5_neg"]  = {"elingeli" .. base}
	data.forms["mod_c6_neg"]  = {"angema" .. base}
	data.forms["mod_c7_neg"]  = {"esingesi" .. base}
	data.forms["mod_c8_neg"]  = {"ezingezi" .. x}
	data.forms["mod_c9_neg"]  = {"enge"   .. x}
	data.forms["mod_c10_neg"] = {"ezingezi" .. x}
	data.forms["mod_c11_neg"] = {"olungelu" .. base}
	data.forms["mod_c14_neg"] = {"obungebu" .. base}
	data.forms["mod_c15_neg"] = {"okungeku" .. base}
	data.forms["mod_c17_neg"] = {"okungeku" .. base}
	
	data.forms["cop_1sg_pos"] = {"ndim" .. base}
	data.forms["cop_2sg_pos"] = {"um" .. base}
	data.forms["cop_1pl_pos"] = {"siba" .. base}
	data.forms["cop_2pl_pos"] = {"niba" .. base}
	data.forms["cop_c1_pos"]  = {"m" .. base}
	data.forms["cop_c2_pos"]  = {"ba" .. base}
	data.forms["cop_c3_pos"]  = {"m" .. base}
	data.forms["cop_c4_pos"]  = {"mi" .. base}
	data.forms["cop_c5_pos"]  = {"li" .. base}
	data.forms["cop_c6_pos"]  = {"ma" .. base}
	data.forms["cop_c7_pos"]  = {"si" .. base}
	data.forms["cop_c8_pos"]  = {"zi" .. x}
	data.forms["cop_c9_pos"]  = {"i"  .. x}
	data.forms["cop_c10_pos"] = {"zi" .. x}
	data.forms["cop_c11_pos"] = {"lu" .. base}
	data.forms["cop_c14_pos"] = {"bu" .. base}
	data.forms["cop_c15_pos"] = {"ku" .. base}
	data.forms["cop_c17_pos"] = {"ku" .. base}
	
	data.forms["cop_1sg_neg"] = {"andim" .. base}
	data.forms["cop_2sg_neg"] = {"awum" .. base}
	data.forms["cop_1pl_neg"] = {"asiba" .. base}
	data.forms["cop_2pl_neg"] = {"aniba" .. base}
	data.forms["cop_c1_neg"]  = {"akam" .. base}
	data.forms["cop_c2_neg"]  = {"ababa" .. base}
	data.forms["cop_c3_neg"]  = {"awum" .. base}
	data.forms["cop_c4_neg"]  = {"ayimi" .. base}
	data.forms["cop_c5_neg"]  = {"alili" .. base}
	data.forms["cop_c6_neg"]  = {"awama" .. base}
	data.forms["cop_c7_neg"]  = {"asisi" .. base}
	data.forms["cop_c8_neg"]  = {"azizi" .. x}
	data.forms["cop_c9_neg"]  = {"ayiyi"  .. x}
	data.forms["cop_c10_neg"] = {"azizi" .. x}
	data.forms["cop_c11_neg"] = {"alulu" .. base}
	data.forms["cop_c14_neg"] = {"abubu" .. base}
	data.forms["cop_c15_neg"] = {"akuku" .. base}
	data.forms["cop_c17_neg"] = {"akuku" .. base}
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


function export.enumerative(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "enumerative concord", categories = {}}
	data.simple = true
	
	if args[1] == "L" or args[1] == "H" or args[1] == "LH" or args[1] == "HH" or args[1] == "HHH" then
		data.info = data.info .. ", tone " .. args[1]
		table.insert(data.categories, lang:getCanonicalName() .. " adjectives with tone " .. args[1])
	elseif args[1] then
		error("The tone must be \"L\", \"H\", \"LH\", \"HH\" or \"HHH\".")
	else
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " enumerative entries")
	end
	
	local base = mw.title.getCurrentTitle().subpageText
	
	data.forms["mod_c1"]  = {"m" .. base}
	data.forms["mod_c2"]  = {"ba" .. base}
	data.forms["mod_c3"]  = {"m" .. base}
	data.forms["mod_c4"]  = {"mi" .. base}
	data.forms["mod_c5"]  = {"li" .. base}
	data.forms["mod_c6"]  = {"ma" .. base}
	data.forms["mod_c7"]  = {"si" .. base}
	data.forms["mod_c8"]  = {"zi" .. base}
	data.forms["mod_c9"]  = {"yi" .. base}
	data.forms["mod_c10"] = {"zi" .. base}
	data.forms["mod_c11"] = {"lu" .. base}
	data.forms["mod_c14"] = {"bu" .. base}
	data.forms["mod_c15"] = {"ku" .. base}
	data.forms["mod_c17"] = {"ku" .. base}
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


function export.relative(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "relative concord", categories = {}}
	
	if args[1] == "L" or args[1] == "H" or args[1] == "LH" or args[1] == "HH" or args[1] == "HHH" then
		data.info = data.info .. ", tone " .. args[1]
		table.insert(data.categories, lang:getCanonicalName() .. " adjectives with tone " .. args[1])
	elseif args[1] then
		error("The tone must be \"L\", \"H\", \"LH\", \"HH\" or \"HHH\".")
	else
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " relative entries")
	end
	
	local base = mw.title.getCurrentTitle().subpageText

	data.forms["mod_1sg_pos"] = {"endi" .. base}
	data.forms["mod_2sg_pos"] = {"o" .. base}
	data.forms["mod_1pl_pos"] = {"esi" .. base}
	data.forms["mod_2pl_pos"] = {"eni" .. base}
	data.forms["mod_c1_pos"]  = {"o" .. base}
	data.forms["mod_c2_pos"]  = {"aba" .. base}
	data.forms["mod_c3_pos"]  = {"o" .. base}
	data.forms["mod_c4_pos"]  = {"e" .. base}
	data.forms["mod_c5_pos"]  = {"eli" .. base}
	data.forms["mod_c6_pos"]  = {"a" .. base}
	data.forms["mod_c7_pos"]  = {"esi" .. base}
	data.forms["mod_c8_pos"]  = {"ezi" .. base}
	data.forms["mod_c9_pos"]  = {"e" .. base}
	data.forms["mod_c10_pos"] = {"ezi" .. base}
	data.forms["mod_c11_pos"] = {"olu" .. base}
	data.forms["mod_c14_pos"] = {"obu" .. base}
	data.forms["mod_c15_pos"] = {"oku" .. base}
	data.forms["mod_c17_pos"] = {"oku" .. base}
	
	data.forms["mod_1sg_neg"] = {"endinge" .. base}
	data.forms["mod_2sg_neg"] = {"onge" .. base}
	data.forms["mod_1pl_neg"] = {"esinge" .. base}
	data.forms["mod_2pl_neg"] = {"eninge" .. base}
	data.forms["mod_c1_neg"]  = {"onge" .. base}
	data.forms["mod_c2_neg"]  = {"abange" .. base}
	data.forms["mod_c3_neg"]  = {"onge" .. base}
	data.forms["mod_c4_neg"]  = {"enge" .. base}
	data.forms["mod_c5_neg"]  = {"elinge" .. base}
	data.forms["mod_c6_neg"]  = {"ange" .. base}
	data.forms["mod_c7_neg"]  = {"esinge" .. base}
	data.forms["mod_c8_neg"]  = {"ezinge" .. base}
	data.forms["mod_c9_neg"]  = {"enge" .. base}
	data.forms["mod_c10_neg"] = {"ezinge" .. base}
	data.forms["mod_c11_neg"] = {"olunge" .. base}
	data.forms["mod_c14_neg"] = {"obunge" .. base}
	data.forms["mod_c15_neg"] = {"okunge" .. base}
	data.forms["mod_c17_neg"] = {"okunge" .. base}
	
	data.forms["cop_1sg_pos"] = {"ndi" .. base}
	data.forms["cop_2sg_pos"] = {"u" .. base}
	data.forms["cop_1pl_pos"] = {"si" .. base}
	data.forms["cop_2pl_pos"] = {"ni" .. base}
	data.forms["cop_c1_pos"]  = {"u" .. base}
	data.forms["cop_c2_pos"]  = {"ba" .. base}
	data.forms["cop_c3_pos"]  = {"u" .. base}
	data.forms["cop_c4_pos"]  = {"i" .. base}
	data.forms["cop_c5_pos"]  = {"li" .. base}
	data.forms["cop_c6_pos"]  = {"a" .. base}
	data.forms["cop_c7_pos"]  = {"si" .. base}
	data.forms["cop_c8_pos"]  = {"zi" .. base}
	data.forms["cop_c9_pos"]  = {"i" .. base}
	data.forms["cop_c10_pos"] = {"zi" .. base}
	data.forms["cop_c11_pos"] = {"lu" .. base}
	data.forms["cop_c14_pos"] = {"bu" .. base}
	data.forms["cop_c15_pos"] = {"ku" .. base}
	data.forms["cop_c17_pos"] = {"ku" .. base}
	
	data.forms["cop_1sg_neg"] = {"andi" .. base}
	data.forms["cop_2sg_neg"] = {"awu" .. base}
	data.forms["cop_1pl_neg"] = {"asi" .. base}
	data.forms["cop_2pl_neg"] = {"ani" .. base}
	data.forms["cop_c1_neg"]  = {"aka" .. base}
	data.forms["cop_c2_neg"]  = {"aba" .. base}
	data.forms["cop_c3_neg"]  = {"awu" .. base}
	data.forms["cop_c4_neg"]  = {"ayi" .. base}
	data.forms["cop_c5_neg"]  = {"ali" .. base}
	data.forms["cop_c6_neg"]  = {"awa" .. base}
	data.forms["cop_c7_neg"]  = {"asi" .. base}
	data.forms["cop_c8_neg"]  = {"azi" .. base}
	data.forms["cop_c9_neg"]  = {"ayi" .. base}
	data.forms["cop_c10_neg"] = {"azi" .. base}
	data.forms["cop_c11_neg"] = {"alu" .. base}
	data.forms["cop_c14_neg"] = {"abu" .. base}
	data.forms["cop_c15_neg"] = {"aku" .. base}
	data.forms["cop_c17_neg"] = {"aku" .. base}
	
	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 = {}
		
		for key, subform in ipairs(form) do
			table.insert(ret, m_links.full_link({lang = lang, term = subform}))
		end
		
		return table.concat(ret, ", ")
	end
	
	local names = {
		["mod"] = "modifier",
		["cop"] = "copulative",
		["pos"] = "positive",
		["neg"] = "negative",
		
		["1sg"] = "1st singular",
		["2sg"] = "2nd singular",
		["1pl"] = "1st plural",
		["2pl"] = "2nd plural",
		
		["c1"] = "class 1",
		["c2"] = "class 2",
		["c3"] = "class 3",
		["c4"] = "class 4",
		["c5"] = "class 5",
		["c6"] = "class 6",
		["c7"] = "class 7",
		["c8"] = "class 8",
		["c9"] = "class 9",
		["c10"] = "class 10",
		["c11"] = "class 11",
		["c14"] = "class 14",
		["c15"] = "class 15",
		["c17"] = "class 17",
	}
	
	local classes = {"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c14", "c15", "c17"}
	local columns = {"mod"}
	local columns2 = {}
	
	if not data.simple then
		table.insert(classes, 1, "1sg")
		table.insert(classes, 2, "2sg")
		table.insert(classes, 3, "1pl")
		table.insert(classes, 4, "2pl")
		
		table.insert(columns, "cop")
		table.insert(columns2, "pos")
		table.insert(columns2, "neg")
	end
	
	local wikicode = {}
	
	table.insert(wikicode, "{| class=\"wikitable inflection-table vsSwitcher\" data-toggle-category=\"inflection\" style=\"border-style: double; border-width: 3px; margin: 0;\"")
	table.insert(wikicode, "|-")
	table.insert(wikicode, "! class=\"vsToggleElement\" style=\"background: #CCC; min-width: 20em; text-align: left;\" colspan=\"5\" | {{{info}}}")
	
	table.insert(wikicode, "|- class=\"vsHide\"")
	table.insert(wikicode, "| style=\"min-width: 8em;\" |")
	
	for _, col in ipairs(columns) do
		table.insert(wikicode, "! style=\"min-width: 12em;\", colspan=\"2\" | " .. mw.getContentLanguage():ucfirst(names[col]))
	end
	
	if not data.simple then
		table.insert(wikicode, "|- class=\"vsHide\"")
		table.insert(wikicode, "! !! " .. names["pos"])
		table.insert(wikicode, "! " .. names["neg"])
		table.insert(wikicode, "! " .. names["pos"])
		table.insert(wikicode, "! " .. names["neg"])
	end
	
	for _, class in ipairs(classes) do
		table.insert(wikicode, "|- class=\"vsHide\"")
		table.insert(wikicode, "! " .. mw.getContentLanguage():ucfirst(names[class]))
		
		for _, col in ipairs(columns) do
			if not data.simple then
				for _, col2 in ipairs(columns2) do
					table.insert(wikicode, "| {{{" .. col .. "_" .. class .. "_" .. col2 .. "}}}")
				end
			else
				table.insert(wikicode, "| {{{" .. col .. "_" .. class .. "}}}")
			end
		end
	end
	
	table.insert(wikicode, "|}")
	
	wikicode = table.concat(wikicode, "\n")
	
	return mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl)
end


return export