Module:sk-adjective


local export = {}


--[=[

Authorship: Zhnka

]=]

--[=[

TERMINOLOGY:

-- "slot" = A particular combination of case/gender/number.
	 Example slot names for adjectives are "gen_f" (genitive feminine singular) and
	 "nom_mp_an" (animate nominative masculine plural). Each slot is filled with zero or more forms.

-- "form" = The declined Slovak form representing the value of a given slot.

-- "lemma" = The dictionary form of a given Slovak term. Generally the nominative
     masculine singular, but may occasionally be another form if the nominative
	 masculine singular is missing.
]=]

local lang = require("Module:languages").getByCode("sk")
local m_links = require("Module:links")
local m_table = require("Module:table")
local m_string_utilities = require("Module:string utilities")
local iut = require("Module:inflection utilities")
local com = require("Module:User:Zhnka/sk-common")

local current_title = mw.title.getCurrentTitle()
local NAMESPACE = current_title.nsText
local PAGENAME = current_title.text

local u = mw.ustring.char
local rsplit = mw.text.split
local rfind = mw.ustring.find
local rmatch = mw.ustring.match
local rgmatch = mw.ustring.gmatch
local rsubn = mw.ustring.gsub
local ulen = mw.ustring.len
local uupper = mw.ustring.upper


-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end


-- version of rsubn() that returns a 2nd argument boolean indicating whether
-- a substitution was made.
local function rsubb(term, foo, bar)
	local retval, nsubs = rsubn(term, foo, bar)
	return retval, nsubs > 0
end


-- All slots that are used by any of the different tables. The key is the slot and the value is a list of the tables
-- that use the slot. "" = regular, "plonly" = special=plonly in {{sk-adecl-manual}}, "dva" = special=dva in
-- {{sk-adecl-manual}}.
local input_adjective_slots = {
	nom_m = {""},
	nom_f = {""},
	nom_n = {""},
	nom_mp_an = {"", "plonly", "dva"},
	nom_mp_in = {"dva"},
	nom_fnp = {"", "plonly", "dva"},
	gen_mn = {""},
	gen_f = {""},
	gen_p = {"", "plonly", "dva"},
	dat_mn = {""},
	dat_f = {""},
	dat_p = {"", "plonly", "dva"},
	acc_m_an = {""},
	acc_m_in = {""},
	acc_f = {""},
	acc_n = {""},
	acc_mp_an = {"", "dva"},
	acc_fnp = {"", "dva"},
	ins_mn = {""},
	ins_f = {""},
	ins_p = {"", "plonly", "dva"},
	loc_mn = {""},
	loc_f = {""},
	loc_p = {"", "plonly", "dva"},
	short_m = {""},
	short_f = {""},
	short_n = {""},
	short_mp_an = {""},
	short_fnp = {""},
}


local output_adjective_slots = {
	nom_m = "nom|m|s",
	nom_m_linked = "nom|m|s", -- used in [[Module:sk-noun]]?
	nom_f = "nom|f|s",
	nom_n = "nom|n|s",
	nom_mp_an = "an|nom|m|p",
	nom_mp_in = "in|nom|m|p",
	nom_mp = "nom|m|p",
	nom_fnp = "in|nom|m|p|;|nom|f//n|p",
	gen_mn = "gen|m//n|s",
	gen_f = "gen|f|s",
	gen_p = "gen|p",
	dat_mn = "dat|m//n|s",
	dat_f = "dat|f|s",
	dat_p = "dat|p",
	acc_m_an = "an|acc|m|s",
	acc_m_in = "in|acc|m|s",
	acc_f = "acc|f|s",
	acc_n = "acc|n|s",
	acc_mp_an = "an|acc|m|p",
	acc_mp_in = "in|acc|m|p",
	acc_fnp = "in|acc|m|p|;|acc|f//n|p",
	ins_mn = "ins|m//n|s",
	ins_f = "ins|f|s",
	ins_p = "ins|p",
	loc_mn = "loc|m//n|s",
	loc_f = "loc|f|s",
	loc_p = "loc|p",
	short_m = "short|m|s",
	short_f = "short|f|s",
	short_n = "short|n|s",
	short_mp_an = "short|an|m|p",
	short_fnp = "short|in|m|p|;|short|f//n|p",
}


local function get_output_adjective_slots(alternant_multiword_spec)
	return output_adjective_slots
end


local function combine_stem_ending(stem, ending)
	if stem == "?" then
		return "?"
	else
		return stem .. ending
	end
end


local function add(base, slot, stems, endings, footnote)
	if stems then
		stems = iut.combine_form_and_footnotes(stems, footnote)
	end
	iut.add_forms(base.forms, slot, stems, endings, combine_stem_ending)
end


local function add_normal_decl(base, stems,
	nom_m, nom_f, nom_n, nom_mp_an, nom_fnp,
	gen_mn, gen_f, gen_p,
	dat_mn, dat_f, dat_p,
	acc_f,
	loc_mn, loc_f, loc_p,
	ins_mn, ins_f, ins_p,
	footnote)
	if stems then
		stems = iut.combine_form_and_footnotes(stems, footnote)
	end
	add(base, "nom_m", stems, nom_m)
	add(base, "nom_f", stems, nom_f)
	add(base, "nom_n", stems, nom_n)
	add(base, "nom_mp_an", stems, nom_mp_an)
	add(base, "nom_fnp", stems, nom_fnp)
	add(base, "gen_mn", stems, gen_mn)
	add(base, "gen_f", stems, gen_f)
	add(base, "gen_p", stems, gen_p)
	add(base, "dat_mn", stems, dat_mn)
	add(base, "dat_f", stems, dat_f)
	add(base, "dat_p", stems, dat_p)
	add(base, "acc_f", stems, acc_f)
	add(base, "loc_mn", stems, loc_mn)
	add(base, "loc_f", stems, loc_f)
	add(base, "loc_p", stems, loc_p)
	add(base, "ins_mn", stems, ins_mn)
	add(base, "ins_f", stems, ins_f)
	add(base, "ins_p", stems, ins_p)
end


local function add_short_decl(base, stems, m, f, n, mp_an, fnp, footnote)
	if stems then
		stems = iut.combine_form_and_footnotes(stems, footnote)
	end
	add(base, "short_m", stems, m)
	add(base, "short_f", stems, f)
	add(base, "short_n", stems, n)
	add(base, "short_mp_an", stems, mp_an)
	add(base, "short_fnp", stems, fnp)
end


local decls = {}

decls["normal"] = function(base)
	local stem, suffix
	-- hard-long in -ý
	stem, suffix = rmatch(base.lemma, "^(.*)(ý)$")
	if stem then
		add_normal_decl(base, stem,
			"ý", "á", "é", "í", "é",
			"ého", "ej", "ých",
			"ému", "ej", "ým",
			"ú",
			"om", "ej", "ých",
			"ým", "ou", "ými"
		)

if base.short then
for _, short_stem_obj in ipairs(base.short) do
                                add_short_decl(base, short_stem_obj.base, "")
                                add_short_decl(base, short_stem_obj.stem, nil, "a", "o", "i", "y")
                        end
                end
		return
	end

	-- hard-short in -y
	stem, suffix = rmatch(base.lemma, "^(.*)(y)$")
	if stem then
		add_normal_decl(base, stem,
			"y", "a", "e", "i", "e",
			"eho", "ej", "ych",
			"emu", "ej", "ym",
			"u",
			"om", "ej", "ych",
			"ym", "ou", "ymi"
		)
		return
	end

	-- soft-long in -í
	stem, suffix = rmatch(base.lemma, "^(.*)(í)$")
	if stem then
		add_normal_decl(base, stem,
			"í", "ia", "ie", "í", "ie",
			"ieho", "ej", "ích",
			"iemu", "ej", "ím",
			"iu",
			{}, "ej", "ích",
			"ím", {}, "ími"
		)
		add_normal_decl(base, com.convert_paired_plain_to_palatal(stem, ending), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "om", nil, nil, nil, "ou")
		return
	end
			
	-- soft-short in -i
	stem, suffix = rmatch(base.lemma, "^(.*)(i)$")
	if stem then
		add_normal_decl(base, stem,
			"i", {}, "e", "i", "e",
			"eho", "ej", "ich",
			"emu", "ej", "im",
			{},
			{}, "ej", "ich",
			"im", {}, "imi"
		)
		add_normal_decl(base, com.convert_paired_plain_to_palatal(stem, ending), nil, "a", nil, nil, nil, nil, nil, nil, nil, nil, nil, "u", "om", nil, nil, nil, "ou")
		return
	end

	-- possessive in -ov
	stem, suffix = rmatch(base.lemma, "^(.*)(ov)$")
	if stem then
		add_normal_decl(base, stem,
			"ov", "ova", "ovo", "ovi", "ove",
			"ovho", "ovej", "ových",
			"ovmu", "ovej", "ovým",
			"ovu",
			"ovom", "ovej", "ových",
			"ovým", "ovou", "ovými"
		)
		return
	end

	-- possessive in -in
	stem, suffix = rmatch(base.lemma, "^(.*)(in)$")
	if stem then
		add_normal_decl(base, stem,
			"in", "ina", "ino", "ini", "ine",
			"inho", "inej", "iných",
			"inmu", "inej", "iným",
			"inu",
			"inom", "inej", "iných",
			"iným", "inou", "inými"
		)
		return
	end


	error("Unrecognized adjective lemma, should end in '-ý', '-y', '-í', '-i', '-ov' or '-in': '" .. base.lemma .. "'")
end


decls["irreg"] = function(base)
	local stem, suffix

	if base.lemma == "môj" then
		add_normal_decl(base, "",
			"môj", "moja", "moje", "moji", "moje",
			"môjho", "mojej", "mojich",
			"môjmu", "mojej", "mojim",
			"moju",
			"mojom", "mojej", "mojich",
			"mojím", "mojou", "mojimi"
		)
		return
	end

	-- determiner like tvoj
	stem, suffix = rmatch(base.lemma, "^(.*)(oj)$")
	if stem then
		add_normal_decl(base, stem,
			"oj", "oja", "oje", "oji", "oje",
			"ojho", "ojej", "ojich",
			"ojmu", "ojej", "ojim",
			"oju",
			"ojom", "ojej", "ojich",
			"ojím", "ojou", "ojimi"
		)
		return
	end


	if base.lemma == "jeden" then
		add_normal_decl(base, "",
			"jeden", "jedna", "jedno", "jedni", "jedny",
			"jedného", "jednej", "jedných",
			"jednému", "jednej", "jedným",
			"jednu",
			"jednom", "jednej", "jedných",
			"jedným", "jednou", "jednými"
		)
		return
	end
	
		if base.lemma == "všetok" then
		add_normal_decl(base, "",
			"všetok", "všetka", "všetko", "všetci", "všetky",
			"všetkého", "všetkej", "všetkých",
			"všetkému", "všetkej", "všetkým",
			"všetku",
			"všetkom", "všetkej", "všetkých",
			"všetkým", "všetkou", "všetkými"
		)
		return
	end

	-- determiner like [[ten]], [[tamten]], [[onen]]
	stem, suffix = rmatch(base.lemma, "^(.*)(en)$")
	if stem then
		local nom_stem = stem .. suffix
		add_normal_decl(base, nom_stem, "")
		add_normal_decl(base, stem,
			nil, "á", "o", "í", "ie",
			"oho", "ej", "ých",
			"omu", "ej", "ým",
			"ú",
			"om", "ej", "ých",
			"ým", "ou", "ými"
		)
		return
	end
		
		if base.lemma == "tento" then
		add_normal_decl(base, "",
			"tento", "táto", "toto", "títo", "tieto",
			"tohto", "tejto", "týchto",
			"tomuto", "tejto", "týmto",
			"tuto",
			"tomto", "tejto", "týchto",
			"týmto", "touto", "týmito"
		)
		return
	end
	
		
		if base.lemma == "všetok" then 
		add_normal_decl(base, nom_stem, "")
		add_normal_decl(base, stem,
			nil, "á", "o", "í", "ie",
			"oh", "ej", "ých",
			"omu", "ej", "ým",
			"ú",
			"om", "ej", "ých",
			"ým", "ou", "ými"
		)
		return
	end
	
	-- [[náš]], [[váš]]
	stem, suffix = rmatch(base.lemma, "^(.*)(áš)$")
	if stem then
		add_normal_decl(base, stem,
			"áš", "aša", "aše", "aši", "aše",
			"ášho", "ašej", "ašich",
			"ášmu", "ašej", "ašim",
			"ašu",
			"ašom", "ašej", "ašich",
			"aším", "ašou", "ašimi"
		)
		return
	end

	if base.lemma == "sám" then
		add_normal_decl(base, "sám", "")
		add_normal_decl(base, "sam",
			nil, "a", "o", "i", "y",
			"ého", "ej", "ých",
			"ému", "ej", "ým",
			"u",
			"om", "ej", "ých",
			"ým", "ou", "ými"
		)
		return
	end


	error("Unrecognized irregular lemma '" .. base.lemma .. "'")
end


local function fetch_footnotes(separated_group)
	local footnotes
	for j = 2, #separated_group - 1, 2 do
		if separated_group[j + 1] ~= "" then
			error("Extraneous text after bracketed footnotes: '" .. table.concat(separated_group) .. "'")
		end
		if not footnotes then
			footnotes = {}
		end
		table.insert(footnotes, separated_group[j])
	end
	return footnotes
end


local function parse_indicator_spec(angle_bracket_spec)
	local inside = rmatch(angle_bracket_spec, "^<(.*)>$")
	assert(inside)
	local base = {forms = {}}
	if inside ~= "" then
		local parts = rsplit(inside, ".", true)
		for _, part in ipairs(parts) do
			if part == "irreg" then
				base.irreg = true
			elseif part == "short" then
				base.short = {{
					base = {
						form = "+",
					},
					stem = {
						form = "+",
					}
				}}
			elseif rfind(part, "^short:") then
				part = rsub(part, "^short:%s*", "")
				base.short = {}
				local segments = iut.parse_balanced_segment_run(part, "[", "]")
				local comma_separated_groups = iut.split_alternating_runs(segments, "%s*,%s*")
				for _, comma_separated_group in ipairs(comma_separated_groups) do
					if comma_separated_group[1] == "*" then
						-- reducible
						table.insert(base.short, {
							base = {
								form = "*",
								footnotes = fetch_footnotes(comma_separated_group),
							},
							stem = {
								form = "*",
								footnotes = fetch_footnotes(comma_separated_group),
							}
						})
					else
						local slash_separated_groups = iut.split_alternating_runs(comma_separated_group, "%s*/%s*")
						if #slash_separated_groups > 2 then
							error("Too many slash-separated stems: '" .. inside .. "'")
						end
						local short_base = slash_separated_groups[1]
						local short_stem = slash_separated_groups[2]
						local short_base_obj = {
							form = short_base[1],
							footnotes = fetch_footnotes(short_base),
						}
						local short_stem_obj
						if short_stem then
							short_stem_obj = {
								form = short_stem[1],
								footnotes = fetch_footnotes(short_stem),
							}
						end
						table.insert(base.short, {
							base = short_base_obj,
							stem = short_stem_obj,
						})
					end
					iut.insert_form(forms, slot, formobj)
				end
			else
				error("Unrecognized indicator '" .. part .. "': '" .. inside .. "'")
			end
		end
	end
	return base
end


local function normalize_all_lemmas(alternant_multiword_spec, pagename)
	iut.map_word_specs(alternant_multiword_spec, function(base)
		if base.lemma == "" then
			base.lemma = pagename
		end
		base.orig_lemma = base.lemma
		base.orig_lemma_no_links = m_links.remove_links(base.lemma)
		base.lemma = base.orig_lemma_no_links
	end)
end


local function detect_indicator_spec(base)
	if base.short then
		if not base.lemma:find("ý$") then
			error("Short forms can only be specified for lemmas ending in -ý, but saw '" .. base.lemma .. "'")
		end
		local stem = rmatch(base.lemma, "^(.*)ý$")
		for _, short_spec in ipairs(base.short) do
			if short_spec.base.form == "+" then
				short_spec.base.form = stem
			elseif short_spec.base.form == "*" then
				short_spec.base.form = com.dereduce(base, stem)
				if not short_spec.base.form then
					error("Unable to construct non-reduced variant of stem '" .. stem .. "'")
				end
			end
			if not short_spec.stem then
				short_spec.stem = {
					form = short_spec.base.form,
					footnotes = short_spec.base.footnotes
				}
			end
			if short_spec.stem.form == "+" or short_spec.stem.form == "*" then
				short_spec.stem.form = stem
			end
		end
	end

	if base.irreg then
		base.decl = "irreg"
	else
		base.decl = "normal"
	end
end


local function detect_all_indicator_specs(alternant_multiword_spec)
	iut.map_word_specs(alternant_multiword_spec, function(base)
		detect_indicator_spec(base)
	end)
end


local function decline_adjective(base)
	if not decls[base.decl] then
		error("Internal error: Unrecognized declension type '" .. base.decl .. "'")
	end
	decls[base.decl](base)
	-- handle_derived_slots_and_overrides(base)
end


-- Process override for the arguments in `args`, storing the results into `forms`. If `do_acc`, only do accusative
-- slots; otherwise, don't do accusative slots.
local function process_overrides(forms, args, do_acc)
	for slot, _ in pairs(input_adjective_slots) do
		if args[slot] and not not do_acc == not not slot:find("^acc") then
			forms[slot] = nil
			if args[slot] ~= "-" and args[slot] ~= "—" then
				local segments = iut.parse_balanced_segment_run(args[slot], "[", "]")
				local comma_separated_groups = iut.split_alternating_runs(segments, "%s*,%s*")
				for _, comma_separated_group in ipairs(comma_separated_groups) do
					local formobj = {
						form = comma_separated_group[1],
						footnotes = fetch_footnotes(comma_separated_group),
					}
					iut.insert_form(forms, slot, formobj)
				end
			end
		end
	end
end


local function check_allowed_overrides(alternant_multiword_spec, args)
	local special = alternant_multiword_spec.special or alternant_multiword_spec.surname and "surname" or ""
	for slot, types in pairs(input_adjective_slots) do
		if args[slot] then
			local allowed = false
			for _, typ in ipairs(types) do
				if typ == special then
					allowed = true
					break
				end
			end
			if not allowed then
				error(("Override %s= not allowed for %s"):format(slot, special == "" and "regular declension" or
					"special=" .. special))
			end
		end
	end
end


local function set_accusative(alternant_multiword_spec)
	local forms = alternant_multiword_spec.forms
	local function copy_if(from_slot, to_slot)
		if not forms[to_slot] then
			iut.insert_forms(forms, to_slot, forms[from_slot])
		end
	end

	copy_if("nom_n", "acc_n")
	copy_if("gen_mn", "acc_m_an")
	copy_if("nom_m", "acc_m_in")
	copy_if("gen_p", "acc_mp_an")
	copy_if("nom_mp_in", "acc_mp_in")
	copy_if("nom_fnp", "acc_fnp")
end


local function add_categories(alternant_multiword_spec)
	local cats = {}
	local plpos = m_string_utilities.pluralize(alternant_multiword_spec.pos or "adjective")
	local function insert(cattype)
		m_table.insertIfNot(cats, "Slovak " .. cattype .. " " .. plpos)
	end
	if not alternant_multiword_spec.manual then
		iut.map_word_specs(alternant_multiword_spec, function(base)
			if base.decl == "irreg" then
				insert("irregular")
			elseif rfind(base.lemma, "ý$") then
				insert("hard-long")
			elseif rfind(base.lemma, "y$") then
				insert("hard-short")
			elseif rfind(base.lemma, "í$") then
				insert("soft-long")
			elseif rfind(base.lemma, "i$") then
				insert("soft-short")
			else
				insert("possessive")
			end
			if base.short then
				table.insert(cats, "Slovak " .. plpos .. " with short forms")
			end
		end)
	end
	alternant_multiword_spec.categories = cats
end


local function show_forms(alternant_multiword_spec)
	local lemmas = {}
	local lemmaform = alternant_multiword_spec.forms.nom_m or alternant_multiword_spec.forms.nom_mp or
		alternant_multiword_spec.forms.nom_mp_an
	if lemmaform then
		for _, form in ipairs(lemmaform) do
			table.insert(lemmas, form.form)
		end
	end
	local props = {
		lemmas = lemmas,
		slot_table = get_output_adjective_slots(alternant_multiword_spec),
		lang = lang,
	}
	iut.show_forms(alternant_multiword_spec.forms, props)
end


local function make_table(alternant_multiword_spec)
	local forms = alternant_multiword_spec.forms

	local function template_prelude(min_width)
		return rsub([===[
<div>
<div class="NavFrame" style="display: inline-block; min-width: MINWIDTHem">
<div class="NavHead" style="background:#eff7ff">{title}{annotation}</div>
<div class="NavContent">
{\op}| border="1px solid #000000" style="border-collapse:collapse;background:#F9F9F9;text-align:center; min-width:MINWIDTHem" class="inflection-table"
|-
]===], "MINWIDTH", min_width)
	end

	local function template_postlude()
		return [=[
|{\cl}{notes_clause}</div></div></div>]=]
	end
	local table_spec_sg = [=[
! style="background:#d9ebff" colspan=5 | singular
|-
! style="background:#d9ebff" |
! style="background:#d9ebff" | masculine animate
! style="background:#d9ebff" | masculine inanimate
! style="background:#d9ebff" | feminine
! style="background:#d9ebff" | neuter
|-
! style="background:#eff7ff" | nominative
| colspan=2 | {nom_m}
| {nom_f}
| {nom_n}
|-
! style="background:#eff7ff" | genitive
| colspan=2 | {gen_mn}
| {gen_f}
| {gen_mn}
|-
! style="background:#eff7ff" | dative
| colspan=2 | {dat_mn}
| {dat_f}
| {dat_mn}
|-
! style="background:#eff7ff" | accusative
| {acc_m_an}
| {acc_m_in}
| {acc_f}
| {acc_n}
|-
! style="background:#eff7ff" | locative
| colspan=2 | {loc_mn}
| {loc_f}
| {loc_mn}
|-
! style="background:#eff7ff" | instrumental
| colspan=2 | {ins_mn}
| {ins_f}
| {ins_mn}{short_sg_clause}
]=]

	local table_spec_pl = [=[
! style="background:#d9ebff" colspan=5 | plural
|-
! style="background:#d9ebff" | 
! style="background:#d9ebff" | masculine animate
! style="background:#d9ebff" | masculine inanimate
! style="background:#d9ebff" | feminine
! style="background:#d9ebff" | neuter
|-
! style="background:#eff7ff" | nominative
| {nom_mp_an}
| colspan=3 | {nom_fnp}
|-
! style="background:#eff7ff" | genitive
| colspan=4 | {gen_p}
|-
! style="background:#eff7ff" | dative
| colspan=4 | {dat_p}
|-
! style="background:#eff7ff" | accusative
| {acc_mp_an}
| colspan=3 | {acc_fnp}
|-
! style="background:#eff7ff" | locative
| colspan=4 | {loc_p}
|-
! style="background:#eff7ff" | instrumental
| colspan=4 | {ins_p}{short_pl_clause}
]=]

	local table_spec = template_prelude("55") .. table_spec_sg .. "|-\n" .. table_spec_pl .. template_postlude()

	local table_spec_plonly = template_prelude("55") .. table_spec_pl .. template_postlude()
	local table_spec_dva = template_prelude("40") .. [=[
! style="width:40%;background:#d9ebff" colspan="2" | 
! style="background:#d9ebff" colspan="3" | plural
|-
! style="width:40%;background:#d9ebff" colspan="2" | 
! style="background:#d9ebff" | masculine animate
! style="background:#d9ebff" | masculine inanimate
! style="background:#d9ebff" | feminine/neuter
|-
! style="background:#eff7ff" colspan="2" | nominative
| {nom_mp_an}
| {nom_mp_in}
| {nom_fnp}
|-
! style="background:#eff7ff" colspan="2" | genitive
| colspan="3" | {gen_p} 
|-
! style="background:#eff7ff" colspan="2" | dative
| colspan="3" | {dat_p} 
|-
! style="background:#eff7ff" colspan="2" | accusative
| {acc_mp_an}
| {acc_mp_in}
| {acc_fnp}
|-
! style="background:#eff7ff" colspan="2" | locative
| colspan="3" | {loc_p} 
|-
! style="background:#eff7ff" colspan="2" | instrumental
| colspan="3" | {ins_p} 

]=] .. template_postlude()

	local short_sg_template = [=[

|-
! style="background:#eff7ff" | short
| colspan=2 | {short_m}
| {short_f}
| {short_n}
]=]

	local short_pl_template = [=[

|-
! style="background:#eff7ff" | short
| {short_mp_an}
| colspan=3 | {short_fnp}]=]

	local notes_template = [===[
<div style="width:100%;text-align:left;background:#d9ebff">
<div style="display:inline-block;text-align:left;padding-left:1em;padding-right:1em">
{footnote}
</div></div>
]===]

	if alternant_multiword_spec.title then
		forms.title = alternant_multiword_spec.title
	else
		forms.title = 'Declension of <i lang="cs">' .. forms.lemma .. '</i>'
	end

	if alternant_multiword_spec.manual then
		forms.annotation = ""
	else
		local ann_parts = {}
		local decls = {}
		iut.map_word_specs(alternant_multiword_spec, function(base)
			if base.decl == "irreg" then
				m_table.insertIfNot(decls, "irregular")
			elseif rfind(base.lemma, "ý$") then
				m_table.insertIfNot(decls, "hard-long")
			elseif rfind(base.lemma, "y$") then
				m_table.insertIfNot(decls, "hard-short")
			elseif rfind(base.lemma, "í$") then
				m_table.insertIfNot(decls, "soft-long")
			elseif rfind(base.lemma, "i$") then
				m_table.insertIfNot(decls, "soft-short") 
			else
				m_table.insertIfNot(decls, "possessive")
			end
		end)
		table.insert(ann_parts, table.concat(decls, " // "))
		forms.annotation = " (" .. table.concat(ann_parts, ", ") .. ")"
	end

	forms.notes_clause = forms.footnote ~= "" and
		m_string_utilities.format(notes_template, forms) or ""
	forms.short_sg_clause = forms.short_m and forms.short_m ~= "—" and
		m_string_utilities.format(short_sg_template, forms) or ""
	forms.short_pl_clause = forms.short_mp_an and forms.short_mp_an ~= "—" and
		m_string_utilities.format(short_pl_template, forms) or ""
	return m_string_utilities.format(
		alternant_multiword_spec.special == "plonly" and table_spec_plonly or
		alternant_multiword_spec.special == "dva" and table_spec_dva or
		table_spec, forms
	)
end

-- Externally callable function to parse and decline an adjective given
-- user-specified arguments. Return value is WORD_SPEC, an object where the
-- declined forms are in `WORD_SPEC.forms` for each slot. If there are no values
-- for a slot, the slot key will be missing. The value for a given slot is a
-- list of objects {form=FORM, footnotes=FOOTNOTES}.
function export.do_generate_forms(parent_args, pos, from_headword, def)
	local params = {
		[1] = {},
		pos = {},
		json = {type = "boolean"}, -- for use with bots
		title = {},
		pagename = {},
	}
	for slot, _ in pairs(input_adjective_slots) do
		params[slot] = {}
	end

	-- Only default param 1 when displaying the template.
	local args = require("Module:parameters").process(parent_args, params)
	local SUBPAGE = mw.title.getCurrentTitle().subpageText
	local pagename = args.pagename or SUBPAGE
	if not args[1] then
		if SUBPAGE == "sk-adecl" then
			args[1] = "pekný"
		else
			args[1] = pagename
		end
	end		
	local parse_props = {
		parse_indicator_spec = parse_indicator_spec,
		allow_default_indicator = true,
		allow_blank_lemma = true,
	}
	local alternant_multiword_spec = iut.parse_inflected_text(args[1], parse_props)
	alternant_multiword_spec.pos = args.pos
	alternant_multiword_spec.title = args.title
	alternant_multiword_spec.forms = {}
	normalize_all_lemmas(alternant_multiword_spec, pagename)
	detect_all_indicator_specs(alternant_multiword_spec)
	check_allowed_overrides(alternant_multiword_spec, args)
	local inflect_props = {
		slot_table = get_output_adjective_slots(alternant_multiword_spec),
		inflect_word_spec = decline_adjective,
	}
	iut.inflect_multiword_or_alternant_multiword_spec(alternant_multiword_spec, inflect_props)
	-- Do non-accusative overrides so they get copied to the accusative forms appropriately.
	process_overrides(alternant_multiword_spec.forms, args)
	set_accusative(alternant_multiword_spec)
	-- Do accusative overrides after copying the accusative forms.
	process_overrides(alternant_multiword_spec.forms, args, "do acc")
	add_categories(alternant_multiword_spec)
	if args.json and not from_headword then
		return require("Module:JSON").toJSON(alternant_multiword_spec)
	end
	return alternant_multiword_spec
end


-- Externally callable function to parse and decline an adjective where all
-- forms are given manually. Return value is WORD_SPEC, an object where the
-- declined forms are in `WORD_SPEC.forms` for each slot. If there are no values
-- for a slot, the slot key will be missing. The value for a given slot is a
-- list of objects {form=FORM, footnotes=FOOTNOTES}.
function export.do_generate_forms_manual(parent_args, pos, from_headword, def)
	local params = {
		pos = {},
		special = {},
		json = {type = "boolean"}, -- for use with bots
		title = {},
	}
	for slot, _ in pairs(input_adjective_slots) do
		params[slot] = {}
	end

	local args = require("Module:parameters").process(parent_args, params)
	local alternant_multiword_spec = {
		pos = args.pos,
		special = args.special,
		title = args.title, 
		forms = {},
		manual = true,
	}
	check_allowed_overrides(alternant_multiword_spec, args)
	-- Do non-accusative overrides so they get copied to the accusative forms appropriately.
	process_overrides(alternant_multiword_spec.forms, args)
	set_accusative(alternant_multiword_spec)
	-- Do accusative overrides after copying the accusative forms.
	process_overrides(alternant_multiword_spec.forms, args, "do acc")
	add_categories(alternant_multiword_spec)
	if args.json and not from_headword then
		return require("Module:JSON").toJSON(alternant_multiword_spec)
	end
	return alternant_multiword_spec
end


-- Entry point for {{sk-adecl}}. Template-callable function to parse and decline 
-- an adjective given user-specified arguments and generate a displayable table
-- of the declined forms.
function export.show(frame)
	local parent_args = frame:getParent().args
	local alternant_multiword_spec = export.do_generate_forms(parent_args)
	show_forms(alternant_multiword_spec)
	return make_table(alternant_multiword_spec) .. require("Module:utilities").format_categories(alternant_multiword_spec.categories, lang)
end


-- Entry point for {{sk-adecl-manual}}. Template-callable function to parse and
-- decline an adjective given manually-specified inflections and generate a
-- displayable table of the declined forms.
function export.show_manual(frame)
	local parent_args = frame:getParent().args
	local alternant_multiword_spec = export.do_generate_forms_manual(parent_args)
	show_forms(alternant_multiword_spec)
	return make_table(alternant_multiword_spec) .. require("Module:utilities").format_categories(alternant_multiword_spec.categories, lang)
end


return export