local export = {}

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

-- Within this module, inflections are the functions that do the actual inflecting
-- by creating the forms of an adjective. They are defined further down.
local inflections = {}

-- 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
	FULLPAGENAME = mw.title.getCurrentTitle().prefixedText
	NAMESPACE = mw.title.getCurrentTitle().nsText
	local infl_type = frame.args[1] or error("Inflection type has not been specified.")
	local args = frame:getParent().args
	
	local data = {forms = {}, info = nil, categories = {}}
	
	if inflections[infl_type] then
		inflections[infl_type](args, data)
	else
		error("Unknown inflection type '" .. infl_type .. "'")
	end
	
	return make_table(data)
end

-- Hard stem adjective
inflections["regular"] = function(args, data)
	local stem = args[1]; if not stem or stem == "" then if NAMESPACE == "Template" then stem = "-" else error("1st parameter (stem) has not been specified.") end end
	local final = args[2]; if final == "" then final = nil end
	local def = args["def"]
	
	-- Create the full stem, which is used when endings are added
	local full_stem = stem
	
	if final then
		full_stem = full_stem .. final
	else
		full_stem = make_long(full_stem)
	end
	
	-- If the given stem doesn't contain any accent marks, flag the entry for attention
	if not require("Module:sl-common").has_accents(full_stem) then
		table.insert(data.categories, "缺少音調的斯洛文尼亞語形容詞")
	end

	-- Is this a hard stem or a soft stem?
	local oe = "o"
	data.info = "hard"
	
	if require("Module:sl-common").is_soft(full_stem) then
		oe = "e"
		data.info = "soft"
	end
	
	-- Masculine singular
	if def == "1" then
		data.forms["nom_sg_m"] = {full_stem .. "i"}
	elseif def == "0" then
		data.forms["nom_sg_m"] = {stem .. (final and "e" .. final or "")}
	else
		data.forms["nom_sg_m"] = {stem .. (final and "e" .. final or "")}
		data.forms["nom_sg_m_def"] = {full_stem .. "i"}
	end
	-- acc_sg_m depends on animacy
	data.forms["gen_sg_m"] = {full_stem .. "ega"}
	data.forms["dat_sg_m"] = {full_stem .. "emu"}
	data.forms["loc_sg_m"] = {full_stem .. "em"}
	data.forms["ins_sg_m"] = {full_stem .. "im"}
	
	-- Feminine singular
	data.forms["nom_sg_f"] = {full_stem .. "a"}
	data.forms["acc_sg_f"] = {full_stem .. "o"}
	data.forms["gen_sg_f"] = {full_stem .. "e"}
	data.forms["dat_sg_f"] = {full_stem .. "i"}
	data.forms["loc_sg_f"] = {full_stem .. "i"}
	data.forms["ins_sg_f"] = {full_stem .. "o"}
	
	-- Neuter singular
	data.forms["nom_sg_n"] = {full_stem .. oe}
	data.forms["acc_sg_n"] = {full_stem .. oe}
	data.forms["gen_sg_n"] = {full_stem .. "ega"}
	data.forms["dat_sg_n"] = {full_stem .. "emu"}
	data.forms["loc_sg_n"] = {full_stem .. "em"}
	data.forms["ins_sg_n"] = {full_stem .. "im"}
	
	-- Masculine dual
	data.forms["nom_du_m"] = {full_stem .. "a"}
	data.forms["acc_du_m"] = {full_stem .. "a"}
	data.forms["gen_du_m"] = {full_stem .. "ih"}
	data.forms["dat_du_m"] = {full_stem .. "ima"}
	data.forms["loc_du_m"] = {full_stem .. "ih"}
	data.forms["ins_du_m"] = {full_stem .. "ima"}
	
	-- Feminine dual
	data.forms["nom_du_f"] = {full_stem .. "i"}
	data.forms["acc_du_f"] = {full_stem .. "i"}
	data.forms["gen_du_f"] = {full_stem .. "ih"}
	data.forms["dat_du_f"] = {full_stem .. "ima"}
	data.forms["loc_du_f"] = {full_stem .. "ih"}
	data.forms["ins_du_f"] = {full_stem .. "ima"}
	
	-- Neuter dual
	data.forms["nom_du_n"] = {full_stem .. "i"}
	data.forms["acc_du_n"] = {full_stem .. "i"}
	data.forms["gen_du_n"] = {full_stem .. "ih"}
	data.forms["dat_du_n"] = {full_stem .. "ima"}
	data.forms["loc_du_n"] = {full_stem .. "ih"}
	data.forms["ins_du_n"] = {full_stem .. "ima"}
	
	-- Masculine plural
	data.forms["nom_pl_m"] = {full_stem .. "i"}
	data.forms["acc_pl_m"] = {full_stem .. "e"}
	data.forms["gen_pl_m"] = {full_stem .. "ih"}
	data.forms["dat_pl_m"] = {full_stem .. "im"}
	data.forms["loc_pl_m"] = {full_stem .. "ih"}
	data.forms["ins_pl_m"] = {full_stem .. "imi"}
	
	-- Feminine plural
	data.forms["nom_pl_f"] = {full_stem .. "e"}
	data.forms["acc_pl_f"] = {full_stem .. "e"}
	data.forms["gen_pl_f"] = {full_stem .. "ih"}
	data.forms["dat_pl_f"] = {full_stem .. "im"}
	data.forms["loc_pl_f"] = {full_stem .. "ih"}
	data.forms["ins_pl_f"] = {full_stem .. "imi"}
	
	-- Neuter plural
	data.forms["nom_pl_n"] = {full_stem .. "a"}
	data.forms["acc_pl_n"] = {full_stem .. "a"}
	data.forms["gen_pl_n"] = {full_stem .. "ih"}
	data.forms["dat_pl_n"] = {full_stem .. "im"}
	data.forms["loc_pl_n"] = {full_stem .. "ih"}
	data.forms["ins_pl_n"] = {full_stem .. "imi"}
end

function make_long(stem)
	local vowel_repl = {
		["à"] = "á",
		["è"] = "ê",
		["ì"] = "í",
		["ò"] = "ô",
		["ù"] = "ú"}
	
	function repl(vowel, rest)
		return (vowel_repl[vowel] or vowel) .. rest
	end
	
	return mw.ustring.gsub(stem, "([àèìòù])([^aeiouàèìòùáéíóúêô]+)$", repl)
end

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 = [=[
{| class="inflection-table vsSwitcher" data-toggle-category="inflection" style="background: #F9F9F9; border: 1px solid #aaaaaa;"
|- style="background: #d9ebff; text-align: left;"
! class="vsToggleElement" colspan="4" | {{{info}}}
|- class="vsShow"
! style="background: #d9ebff; width: 9em;" | 
! style="background: #d9ebff; width: 10em;" | 陽性
! style="background: #d9ebff; width: 10em;" | 陰性
! style="background: #d9ebff; width: 10em;" | 中性
|- class="vsShow"
! style="width: 11em; background: #eff7ff;" | 主格單數
| style="width: 11em;" | {{{!nom_sg_m}}}
| style="width: 11em;" | {{{!nom_sg_f}}}
| style="width: 11em;" | {{{!nom_sg_n}}}
|- class="vsHide"
! colspan="4" style="background: #b3d7ff;" | 單數
|- class="vsHide"
! style="width: 11em; background: #d9ebff;" | 
! style="width: 11em; background: #d9ebff;" | 陽性
! style="width: 11em; background: #d9ebff;" | 陰性
! style="width: 11em; background: #d9ebff;" | 中性
|- class="vsHide"
! style="background: #eff7ff;" | 主格
| {{{nom_sg_m}}}]=] .. (data.forms["nom_sg_m_def"] and ' <sup style="cursor:help" title="不定">ind</sup><br/>{{{nom_sg_m_def}}} <sup style="cursor:help" title="定">def</sup>' or "") .. [=[

| {{{nom_sg_f}}}
| {{{nom_sg_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 賓格
| 主格<sup style="cursor:help" title="動物">inan</sup>或<br/>屬格<sup style="cursor:help" title="動物">anim</sup>
| {{{acc_sg_f}}}
| {{{acc_sg_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 屬格
| {{{gen_sg_m}}}
| {{{gen_sg_f}}}
| {{{gen_sg_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 與格
| {{{dat_sg_m}}}
| {{{dat_sg_f}}}
| {{{dat_sg_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 方位格
| {{{loc_sg_m}}}
| {{{loc_sg_f}}}
| {{{loc_sg_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 工具格
| {{{ins_sg_m}}}
| {{{ins_sg_f}}}
| {{{ins_sg_n}}}
|- class="vsHide"
! colspan="4" style="background: #b3d7ff;" | 雙數
|- class="vsHide"
! style="background: #d9ebff;" | 
! style="background: #d9ebff;" | 陽性
! style="background: #d9ebff;" | 陰性
! style="background: #d9ebff;" | 中性
|- class="vsHide"
! style="background: #eff7ff;" | 主格
| {{{nom_du_m}}}
| {{{nom_du_f}}}
| {{{nom_du_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 賓格
| {{{acc_du_m}}}
| {{{acc_du_f}}}
| {{{acc_du_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 屬格
| {{{gen_du_m}}}
| {{{gen_du_f}}}
| {{{gen_du_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 與格
| {{{dat_du_m}}}
| {{{dat_du_f}}}
| {{{dat_du_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 方位格
| {{{loc_du_m}}}
| {{{loc_du_f}}}
| {{{loc_du_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 工具格
| {{{ins_du_m}}}
| {{{ins_du_f}}}
| {{{ins_du_n}}}
|- class="vsHide"
! colspan="4" style="background: #b3d7ff;" | 複數
|- class="vsHide"
! style="background: #d9ebff;" | 
! style="background: #d9ebff;" | 陽性
! style="background: #d9ebff;" | 陰性
! style="background: #d9ebff;" | 中性
|- class="vsHide"
! style="background: #eff7ff;" | 主格
| {{{nom_pl_m}}}
| {{{nom_pl_f}}}
| {{{nom_pl_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 賓格
| {{{acc_pl_m}}}
| {{{acc_pl_f}}}
| {{{acc_pl_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 屬格
| {{{gen_pl_m}}}
| {{{gen_pl_f}}}
| {{{gen_pl_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 與格
| {{{dat_pl_m}}}
| {{{dat_pl_f}}}
| {{{dat_pl_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 方位格
| {{{loc_pl_m}}}
| {{{loc_pl_f}}}
| {{{loc_pl_n}}}
|- class="vsHide"
! style="background: #eff7ff;" | 工具格
| {{{ins_pl_m}}}
| {{{ins_pl_f}}}
| {{{ins_pl_n}}}
|}]=]
	
	return mw.ustring.gsub(wikicode, "{{{([^}]+)}}}", repl) .. require("Module:utilities").format_categories(data.categories, lang)
end

return export