local export = {}
local pos_functions = {}

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

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	PAGENAME = mw.title.getCurrentTitle().text
	
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local params = {
		["head"] = {list = true},
		["suff"] = {type = "boolean"},
	}
	
	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = args["head"], inflections = {}}
	
	if args["suff"] then
		data.pos_category = "後綴"
		
		if poscat == "形容詞" then
			table.insert(data.categories, "構成形容詞的" .. lang:getCanonicalName() .. "後綴")
		elseif poscat == "副詞" then
			table.insert(data.categories, "構成副詞的" .. lang:getCanonicalName() .. "後綴")
		elseif poscat == "名詞" then
			table.insert(data.categories, "構成名詞的" .. lang:getCanonicalName() .. "後綴")
		elseif poscat == "動詞" then
			table.insert(data.categories, "構成動詞的" .. lang:getCanonicalName() .. "後綴")
		else
			error("No category exists for suffixes forming " .. poscat .. ".")
		end
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end
	
	return require("Module:headword").full_headword(data)
end

-- Display additional inflection information for an adjective
pos_functions["形容詞"] = {
	params = {
		[1] = {},
		["attr2"] = {},
		
		[2] = {},
		["comp2"] = {},
		
		[3] = {},
		["sup2"] = {},
		},
	func = function(args, data)
		local attr = args[1]
		local comp = args[2]
		local sup = args[3]
		
		local attr2 = args["attr2"]
		local comp2 = args["comp2"]
		local sup2 = args["sup2"]
		
		-- Generate the missing forms
		if not attr then
			if PAGENAME:find("[^aeiou]u$") then
				attr = PAGENAME .. "we"
			elseif mw.ustring.find(PAGENAME, "[eë]$") then
				attr = PAGENAME
			else
				attr = PAGENAME .. "e"
			end
		end
		
		if not comp then
			if PAGENAME:find("r$") then
				comp = PAGENAME .. "der"
			elseif attr ~= "-" then
				if not attr:find("[eë]$") then
					comp = attr .. "er"
				else
					comp = attr .. "r"
				end
			else
				comp = PAGENAME .. "er"
			end
		end
		
		if not sup then
			if attr == PAGENAME:gsub("nk$", "ng") then
				sup = attr .. "ste"
			elseif PAGENAME:find("[^aeiou]u$") then
				sup = PAGENAME .. "uste"
			else
				sup = PAGENAME .. "ste"
			end
		end
		
		-- Attributive form
		if attr == "-" then
			table.insert(data.inflections, {label = "僅用作[[表語]]"})
			table.insert(data.categories, "僅用作表語的南非語形容詞")
		else
			local infl_parts = {label = "[[定語]]", attr}
			
			if attr2 then
				table.insert(infl_parts, attr2)
		   	end
		   	
		   	table.insert(data.inflections, infl_parts)
		end
		
		-- Comparative and superlative forms
		if comp == "-" then
			table.insert(data.inflections, {label = "無[[Appendix:Glossary#比較級|比較級]]"})
		else
			-- Comparative
			local infl_parts = {label = "[[Appendix:Glossary#比較級|比較級]]", comp}
			
			if comp2 then
				table.insert(infl_parts, comp2)
			end
	 
			table.insert(data.inflections, infl_parts)
			
			-- Superlative
			infl_parts = {label = "[[Appendix:Glossary#最高級|最高級]]", sup}
			
			if sup2 then
				table.insert(infl_parts, sup2)
			end
			
			table.insert(data.inflections, infl_parts)
		end
	end
}

-- List of irregular verbs. See further down.
local irregular_verbs = {}

-- Display additional inflection information for a adverb
pos_functions["動詞"] = {
	params = {
		[1] = {list = "pres", default = ""},
		[2] = {list = "past", default = ""},
		
		["sep"] = {},
		},
	func = function(args, data)
		local past_part = args[2]
		local sep = args["sep"]
		
		local stem = PAGENAME
		
		-- If separable, remove the prefix to create the stem
		if sep then
			stem = PAGENAME:gsub("^" .. sep, "")
			table.insert(data.categories, "南非語可分離動詞")
		end
		
		local forms = {}
		
		-- Is this the stem of an irregular verb?
		-- If so, delegate all the work to its function.
		if irregular_verbs[stem] then
			irregular_verbs[stem](forms)
			table.insert(data.categories, "南非語不規則動詞")
		else
			forms["pres"] = {stem}
			forms["pres|ptcp"] = args[1]; if forms["pres|ptcp"][1] == "" then forms["pres|ptcp"][1] = stem .. "ende" end
			forms["past|ptcp"] = args[2]; if forms["past|ptcp"][1] == "" then forms["past|ptcp"][1] = "ge" .. stem end
		end
		
		for key, form in pairs(forms) do
			form.accel = {form = key}
		end
		
		-- Add the forms to the list
		if forms["pres"] then
			if sep then
				forms["pres"].accel = nil
				
				for i, form in ipairs(forms["pres"]) do
					forms["pres"][i] = "[[" .. form .. "]] [[" .. sep .. "]]"
				end
			end
			
			forms["pres"].label = "現在時"
			table.insert(data.inflections, forms["pres"])
		end
		
		if forms["pres|ptcp"] then
			if sep then
				for i, form in ipairs(forms["pres|ptcp"]) do
					forms["pres|ptcp"][i] = sep .. form
				end
			end
			
			forms["pres|ptcp"].label = "現在分詞"
			table.insert(data.inflections, forms["pres|ptcp"])
		end
		
		if forms["past"] then
			forms["past"].accel = nil
			
			if sep then
				for i, form in ipairs(forms["past"]) do
					forms["past"][i] = "[[" .. form .. "]] [[" .. sep .. "]]"
				end
			end
			
			forms["past"].label = "過去時"
			table.insert(data.inflections, forms["past"])
		end
		
		if forms["past|ptcp"] then
			if sep then
				for i, form in ipairs(forms["past|ptcp"]) do
					forms["past|ptcp"][i] = sep .. form
				end
			end
			
			forms["past|ptcp"].label = "過去分詞"
			table.insert(data.inflections, forms["past|ptcp"])
		end
	end
}

irregular_verbs["dink"] = function(forms)
	forms["pres"] = {"dink"}
	forms["pres|ptcp"] = {"denkende"}
	forms["past"] = {"dag", "dog"}
	forms["past|ptcp"] = {"gedag", "gedog", "gedink"}
end

irregular_verbs["hê"] = function(forms)
	forms["pres"] = {"het"}
	forms["pres|ptcp"] = {"hebbende"}
	forms["past"] = {"had"}
	forms["past|ptcp"] = {"gehad"}
end

irregular_verbs["kan"] = function(forms)
	forms["pres"] = {"kan"}
	forms["pres|ptcp"] = nil
	forms["past"] = {"kon"}
	forms["past|ptcp"] = nil
end

irregular_verbs["mag"] = function(forms)
	forms["pres"] = {"mag"}
	forms["pres|ptcp"] = nil
	forms["past"] = {"mog"}
	forms["past|ptcp"] = nil
end

irregular_verbs["moet"] = function(forms)
	forms["pres"] = {"moet"}
	forms["pres|ptcp"] = nil
	forms["past"] = {"moes"}
	forms["past|ptcp"] = nil
end

irregular_verbs["sal"] = function(forms)
	forms["pres"] = {"sal"}
	forms["pres|ptcp"] = nil
	forms["past"] = {"sou"}
	forms["past|ptcp"] = nil
end

irregular_verbs["wees"] = function(forms)
	forms["pres"] = {"is"}
	forms["pres|ptcp"] = {"synde"}
	forms["past"] = {"was"}
	forms["past|ptcp"] = {"gewees"}
end

irregular_verbs["weet"] = function(forms)
	forms["pres"] = {"weet"}
	forms["pres|ptcp"] = {"wetende"}
	forms["past"] = {"wis"}
	forms["past|ptcp"] = {"geweet"}
end

irregular_verbs["wil"] = function(forms)
	forms["pres"] = {"wil"}
	forms["pres|ptcp"] = nil
	forms["past"] = {"wou"}
	forms["past|ptcp"] = {"gewil"}
end

irregular_verbs["beter"] = function(forms)
	forms["pres"] = {"beter"}
	forms["pres|ptcp"] = nil
	forms["past"] = nil
	forms["past|ptcp"] = nil
end

return export