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" | “{{{lemma}}}”{{{info}}}的曲折变化
|- class="vsShow" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | 未变化
| style="min-width: 12em;" | {{{!indef|n|s}}}
|- class="vsShow" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | 变化后
| {{{!indef|m|and|f|s}}}]=] .. (data.comparitive and [=[

|- class="vsShow" style="background: #F2F2FF;"
! style="background: #CCCCFF;" | 比较级
| {{{!indef|n|s|comd}}}]=] or "") .. [=[

|- class="vsHide" style="background: #CCCCFF;"
| colspan="2" style="background: #E6E6FF;" |
! style="min-width: 12em;" | 原级]=] .. (data.comparable and [=[

! style="min-width: 12em;" | 比较级
! style="min-width: 12em;" | 最高级]=] or "") .. [=[

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

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" rowspan="3" | 不定
! style="background: #CCCCFF;" | 阴性/阳性&nbsp;单数
| {{{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;" | 中性&nbsp;单数
| {{{indef|n|s}}}]=] .. (data.comparable and " || {{{indef|n|s|comd}}} || {{{indef|n|s|supd}}}" or "") .. [=[

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

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

|- class="vsHide" style="background: #F2F2FF;"
! style="background: #CCCCFF;" colspan="2" | 表量
| {{{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