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

local export = {}

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

-- 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 "adjective"
	
	if not inflections[infl_type] then
		error("Unknown inflection type '" .. infl_type .. "'")
	end
	
	local data = {forms = {}, categories = {}, comparable = true, comparitive = true}
	local args = require("Module:parameters").process(frame:getParent().args, inflections[infl_type].params)
	
	-- Generate the forms
	inflections[infl_type].func(args, data)
	
	return make_table(data)
end

inflections["adjective"] = {
	params = {
		[1] = {},
		[2] = {},
		[3] = {},
		["pred"] = {},
		["part"] = {},
	},
	func = function(args, data)
		-- Get parameters
		local pred = args["pred"]
		local infl = args[1]
		local part = args["part"]
		local comd = args[2]
		local supd = args[3]
		
		if comd == "-" then
			data.comparable = false
		end
		
		local base = mw.title.getCurrentTitle().nsText == "Template" and "-" or mw.title.getCurrentTitle().text
		
		-- Positive degree
		if pred == "-" then
			pred = nil
		else
			pred = pred or base
		end
		
		infl = infl or export.make_inflected(base)
		
		if part == "-" then
			part = nil
		else
			part = part or export.make_partitive(base)
		end
		
		data.forms["pred"] = {pred}
		data.forms["indef|m|and|f|s"] = {infl}
		data.forms["indef|n|s"] = {base}
		data.forms["indef|p"] = {infl}
		data.forms["def"] = {infl}
		data.forms["par"] = {part}
		
		-- Participles never have comparative and superlative forms.
		-- If they do, then they're probably really adjectives.
		if data.comparable then
			-- Comparative degree
			comd = comd or export.make_comparative(base, infl)
			local infl_comd = export.make_inflected(comd); if base:find("[^eio]en$") or base:find("[aeou]ien$") then infl_comd = comd end
			local part_comd = export.make_partitive(comd)
			
			data.forms["pred|comd"] = {comd}
			data.forms["indef|m|and|f|s|comd"] = {infl_comd}
			data.forms["indef|n|s|comd"] = {comd}
			data.forms["indef|p|comd"] = {infl_comd}
			data.forms["def|comd"] = {infl_comd}
			data.forms["par|comd"] = {part_comd}
			
			-- Superlative degree
			supd = supd or export.make_superlative(base, part)
			local infl_supd = export.make_inflected(supd)
			
			data.forms["pred|supd"] = {"het [[" .. supd .. "]]", "het [[" .. infl_supd .. "]]"}
			data.forms["indef|m|and|f|s|supd"] = {infl_supd}
			data.forms["indef|n|s|supd"] = {infl_supd}
			data.forms["indef|p|supd"] = {infl_supd}
			data.forms["def|supd"] = {infl_supd}
			data.forms["par|supd"] = nil
		end
	end
}

inflections["pres-ptc"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		data.comparable = false
		data.comparitive = false
		inflections["adjective"].func(args, data)
		
		-- The predicative form of the present participle can have -e too,
		-- in sentences like "Hij is stervende".
		table.insert(data.forms["pred"], data.forms["indef|m|and|f|s"][1])
	end
}

inflections["past-ptc"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		data.comparable = false
		data.comparitive = false
		inflections["adjective"].func(args, data)
	end
}

inflections["irregular"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		local base = args[1] or (mw.title.getCurrentTitle().nsText == "Template" and "veel" or mw.title.getCurrentTitle().text)
		
		if irregular[base] then
			irregular[base](data)
		else
			error("Unknown irregular adjective '" .. base .. "'.")
		end
	end
}

irregular["veel"] = function(data)
	data.forms["pred"] = {"veel"}
	data.forms["indef|m|and|f|s"] = {"veel"}
	data.forms["indef|n|s"] = {"veel"}
	data.forms["indef|p"] = {"veel"}
	data.forms["def"] = {"vele"}
	data.forms["par"] = nil
	
	data.forms["pred|comd"] = {"meer"}
	data.forms["indef|m|and|f|s|comd"] = {"meer"}
	data.forms["indef|n|s|comd"] = {"meer"}
	data.forms["indef|p|comd"] = {"meer"}
	data.forms["def|comd"] = nil
	data.forms["par|comd"] = nil
	
	data.forms["pred|supd"] = {"het [[meest]]", "het [[meeste]]"}
	data.forms["indef|m|and|f|s|supd"] = nil
	data.forms["indef|n|s|supd"] = nil
	data.forms["indef|p|supd"] = nil
	data.forms["def|supd"] = {"meeste"}
	data.forms["par|supd"] = nil
end

irregular["weinig"] = function(data)
	data.forms["pred"] = {"weinig"}
	data.forms["mfsg"] = {"weinig"}
	data.forms["nsg"] = {"weinig"}
	data.forms["pl"] = {"weinig"}
	data.forms["def"] = {"weinige"}
	data.forms["part"] = nil
	
	data.forms["pred|comd"] = {"minder"}
	data.forms["indef|m|and|f|s|comd"] = {"minder"}
	data.forms["indef|n|s|comd"] = {"minder"}
	data.forms["indef|p|comd"] = {"minder"}
	data.forms["def|comd"] = nil
	data.forms["par|comd"] = nil
	
	data.forms["pred|supd"] = {"het [[minst]]", "het [[minste]]"}
	data.forms["indef|m|and|f|s|supd"] = {"minste"}
	data.forms["indef|n|s|supd"] = {"minste"}
	data.forms["indef|p|supd"] = {"minste"}
	data.forms["def|supd"] = {"minste"}
	data.forms["par|supd"] = nil
end

-- Four functions to create various forms.
-- These are exported, because Module:nl-headword also uses them to create
-- comparative and superlative forms.

function export.make_inflected(base)
	-- Adjectives ending in unstressed -en or -e get no extra -e.
	if base:find("[^eio]en$") or base:find("[aeou]ien$") or base:find("[^eio]e$") then
		return base
	-- Adjectives ending in certain digraphs get a diaeresis
	elseif base:find("[eio]e$") then
		return base .. "ë"
	else
		return base .. "e"
	end
end

function export.make_partitive(base)
	-- Adjectives ending in a sibilant do not get an extra -s
	if base:find("s$") or base:find("sch$") or base:find("x$") or base:find("sj$") or base:find("sh$") then
		return base
	-- Adjectives ending in a long vowel get an apostrophe before the -s
	elseif base:find("[^aeiou][aiouy]$") then
		return base .. "'s"
	else
		return base .. "s"
	end
end

function export.make_comparative(base, inflected)
	if not inflected then
		inflected = export.make_inflected(base)
	end
	
	-- Adjectives ending in -r get an extra -d- in the comparative,
	-- disregarding the inflected form
	if base:find("r$") then
		return base .. "der"
	-- If the inflected form does not end in -e, add it
	elseif not inflected:find("[eë]$") then
		return inflected .. "er"
	else
		return inflected .. "r"
	end
end

function export.make_superlative(base, partitive)
	if not partitive then
		partitive = export.make_partitive(base)
	end
	
	-- Adjectives in -ide have a superlative -iedst
	if base:find("[iï]de$") then
		return base:gsub("([iï])de$", "%1ed") .. "st"
	elseif partitive then
		return partitive .. "t"
	else
		return base .. "st"
	end
end


-- Make the table
function make_table(data)
	local function repl(param)
		local accel = true
		local no_store = false
		
		if param == "lemma" then
			return require("Module:links").full_link({lang = lang, alt = mw.title.getCurrentTitle().text}, "term")
		elseif param == "info" then
			return data.title and " (" .. data.title .. ")" 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, lemma = data.lemma, no_store = no_store} or nil}))
		end
		
		return table.concat(ret, "<br/>")
	end
	
	local wikicode = [=[
{| style="border: 1px solid #CCCCFF; text-align: center; line-height: 125%;" class="inflection-table vsSwitcher" data-toggle-category="inflection" cellspacing="1" cellpadding="3"
|- style="background: #CCCCFF;"
! colspan="5" class="vsToggleElement" style="text-align: left" | Biến tố của {{{lemma}}}{{{info}}}
|- class="vsShow" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | không biến tố
| style="min-width: 12em;" | {{{!indef|n|s}}}
|- class="vsShow" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | có biến tố
| {{{!indef|m|and|f|s}}}]=] .. (data.comparitive and [=[

|- class="vsShow" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | so sánh hơn
| {{{!indef|n|s|comd}}}]=] or "") .. [=[

|- class="vsHide" style="background: #CCCCFF;"
| colspan="2" style="background: #E6E6FF;" |
! style="min-width: 12em;" | [[positive degree|khẳng định]]]=] .. (data.comparable and [=[

! style="min-width: 12em;" | [[comparative degree|so sánh hơn]]
! style="min-width: 12em;" | [[superlative degree|so sánh nhất]]]=] or "") .. [=[

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" colspan="2" | [[vị ngữ]]/[[trạng ngữ]]
| {{{pred}}}]=] .. (data.comparable and " || {{{pred|comd}}} || {{{pred|supd}}}" or "") .. [=[

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" rowspan="3" | [[bất định]]
! style="background: #CCCCFF;" | [[giống đực|gđ.]]/[[giống cái|gc.]]&nbsp;[[số ít|si.]]
| {{{indef|m|and|f|s}}}]=] .. (data.comparable and " || {{{indef|m|and|f|s|comd}}} || {{{indef|m|and|f|s|supd}}}" or "") .. [=[

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | [[giống trung|gt.]]&nbsp;[[số ít|si.]]
| {{{indef|n|s}}}]=] .. (data.comparable and " || {{{indef|n|s|comd}}} || {{{indef|n|s|supd}}}" or "") .. [=[

|- class="vsHide" style="background: #F2F2FF;"
! style="background:#CCCCFF;" | [[số nhiều]]
| {{{indef|p}}}]=] .. (data.comparable and " || {{{indef|p|comd}}} || {{{indef|p|supd}}}" or "") .. [=[

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" colspan="2" | [[xác định]]
| {{{def}}}]=] .. (data.comparable and " || {{{def|comd}}} || {{{def|supd}}}" or "") .. [=[

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" colspan="2" | [[partitive|par.]]
| {{{par}}}]=] .. (data.comparable and " || {{{par|comd}}} || {{{par|supd}}}" or "") .. [=[

|}]=]
	
	return mw.ustring.gsub(wikicode, "{{{([#!]?[a-z0-9|]+)}}}", repl) .. require("Module:utilities").format_categories(data.categories, lang)
end

return export