Documentation for this module may be created at Modulu:de/dok

local extra = require("Module:extra")
local eu = require("Module:eu")

local export = {}

local t = {
	["label"] = {
		["case"] = {
			["a"] = "akusatibo",
			["d"] = "datibo",
			["g"] = "genitibo",
			["n"] = "nominatibo",
		},
		["declension"] = {
			["m"] = "deklinabide misto",
			["s"] = "deklinabide indartsu",
			["w"] = "deklinabide ahul",
		},
		["degree"] = {
			["c"] = "konparatibo",
			["p"] = "positibo",
			["s"] = "superlatibo",
		},
		["gender"] = {
			["f"] = "femenino",
			["m"] = "maskulino",
			["n"] = "neutro",
		},
		["mood"] = {
			["im"] = "inperatibo",
			["in"] = "indikatibo",
			["su"] = "subjuntibo",
		},
		["number"] = {
			["p"] = "plural",
			["s"] = "singular",
		},
		["tense"] = {
			["fu"] = "geroaldi",
			["fup"] = "geroaldi burutu",
			["pa"] = "iraganaldi",
			["pap"] = "iraganaldi burutu",
			["pr"] = "orainaldi",
			["prp"] = "orainaldi burutu",
		},
		["voice"] = {
			["a"] = "aktibo",
			["p"] = "pasibo",
		},
	},
	["pattern"] = {
		["noun"] = {
			"",
			"(e)n",
			"(e)ns",
			"(e)s",
			"(ses)",
			"en",
			"ens",
			"es",
			"n",
			"ns",
			"s",
			"ses",
		},
	},
	["sequence"] = {
		["case"] = {
			"n",
			"g",
			"d",
			"a",
		},
		["gender"] = {
			"m",
			"f",
			"n",
		},
		["number"] = {
			"s",
			"p",
		},
	},
}

function export.getAdjektive(frame)
	return "aleman"
end

function export.getCase(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.case[tArg[1]] or "")
end

function export.getDegree(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.degree[tArg[1]] or "")
end

function export.getDeclension(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.declension[tArg[1]] or "")
end

function export.getGender(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.gender[tArg[1]] or "")
end

function export.getInflectedNoun(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	tArg["singular"] = (
		tArg[1] or
		tArg["singularra"] or
		tArg["singular"] or
		"‑"
	)
	tArg["plural"] = (
		tArg[2] or
		tArg["plurala"] or
		tArg["plural"] or
		"‑"
	)
	tArg["gender"] = (
		tArg[3] or
		tArg["generoa"] or
		tArg["genero"] or
		tArg["gender"]
	)
	tArg["gender"] = extra.table.find(extra.table.listindex(
		t.label.gender
	), tArg["gender"]) and tArg["gender"] or "m"
	tArg["pattern"] = (
		tArg[4] or
		tArg["eredua"] or
		tArg["eredu"] or
		tArg["pattern"]
	)
	tArg["pattern"] = extra.table.find(
		t.pattern.noun,
		tArg["pattern"]
	) and tArg["pattern"] or ""
	tArg["case"] = (
		tArg[5] or
		tArg["kasua"] or
		tArg["kasu"] or
		tArg["case"]
	)
	tArg["case"] = extra.string.find(
		tArg[5],
		{t.label.case, t.label.number},
		1,
		false,
		true
	)
	tArg["number"] = (
		tArg[6] or
		tArg["numeroa"] or
		tArg["numero"] or
		tArg["number"]
	)
	tArg["number"] = extra.string.find(
		tArg["number"],
		{t.label.number},
		1,
		false,
		true
	)[1]
	tArg["number"] = (
		tArg["number"] and tArg["number"][2] or
		tArg["case"][2] and tArg["case"][2][2] or
		"p"
	)
	tArg["case"] = tArg["case"][1] and tArg["case"][1][2] or "n"
	tArg["selection"] = tonumber(
		tArg[7] or
		tArg["aukera"] or
		tArg["selection"]
	) or 1
	if tArg["number"] == "p" then
		if (
			tArg["case"] == "d" and
			string.match(tArg["plural"], "[ns‑]", -1) == nil
		) then
			return ({tArg["plural"] .. "n"})[tArg["selection"]]
		end
		return ({tArg["plural"]})[tArg["selection"]]
	end
	if tArg["case"] == "n" or tArg["gender"] == "f" or tArg["singular"] == "‑" then
		return ({tArg["singular"]})[tArg["selection"]]
	end
	if tArg["case"] == "a" then
		if tArg["gender"] == "n" then
			return ({tArg["singular"]})[tArg["selection"]]
		end
		if tArg["pattern"] == "en" or tArg["pattern"] == "n" then
			return ({tArg["singular"] .. tArg["pattern"]})[tArg["selection"]]
		end
		if tArg["pattern"] == "ens" then
			return ({tArg["singular"] .. "en"})[tArg["selection"]]
		end
		if tArg["pattern"] == "ns" then
			return ({tArg["singular"] .. "n"})[tArg["selection"]]
		end
		if tArg["pattern"] == "(e)n" then
			return ({tArg["singular"] .. "n", tArg["singular"] .. "en"})[tArg["selection"]]
		end
		return ({tArg["singular"]})[tArg["selection"]]
	end
	if tArg["case"] == "d" then
		if tArg["pattern"] == "en" or tArg["pattern"] == "n" then
			return ({tArg["singular"] .. tArg["pattern"]})[tArg["selection"]]
		end
		if tArg["pattern"] == "ens" then
			return ({tArg["singular"] .. "en"})[tArg["selection"]]
		end
		if tArg["pattern"] == "ns" then
			return ({tArg["singular"] .. "n"})[tArg["selection"]]
		end
		if tArg["pattern"] == "(e)n" then
			return ({tArg["singular"] .. "n", tArg["singular"] .. "en"})[tArg["selection"]]
		end
		if tArg["pattern"] == "(e)s" or tArg["pattern"] == "es" then
			return ({tArg["singular"], tArg["singular"] .. "e"})[tArg["selection"]]
		end
		if tArg["pattern"] == "(ses)" or tArg["pattern"] == "ses" then
			return ({tArg["singular"], tArg["singular"] .. "se"})[tArg["selection"]]
		end
		return ({tArg["singular"]})[tArg["selection"]]
	end
	if (
		tArg["pattern"] == "en" or
		tArg["pattern"] == "ens" or
		tArg["pattern"] == "es" or 
		tArg["pattern"] == "n" or
		tArg["pattern"] == "ns" or
		tArg["pattern"] == "s" or
		tArg["pattern"] == "ses" 
	) then
		return ({tArg["singular"] .. tArg["pattern"]})[tArg["selection"]]
	end
	if tArg["pattern"] == "(e)n" then
		return ({tArg["singular"] .. "n", tArg["singular"] .. "en"})[tArg["selection"]]
	end
	if tArg["pattern"] == "(e)s" then
		return ({tArg["singular"] .. "s", tArg["singular"] .. "es"})[tArg["selection"]]
	end
	if tArg["pattern"] == "(ses)" then
		return ({tArg["singular"], tArg["singular"] .. "ses"})[tArg["selection"]]
	end
	return ({tArg["singular"]})[tArg["selection"]]
end

function export.getMood(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.mood[tArg[1]] or "")
end

function export.getName(frame)
	return "Alemana"
end

function export.getNativeAdjektive(frame)
	return "deutsch"
end

function export.getNativeName(frame)
	return "Deutsch"
end

function export.getNumber(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.number[tArg[1]] or "")
end

function export.getSymbol(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (
		"[[Irudi:Flag of Germany.svg|border|" .. tostring(tonumber(
			tArg[1] or tArg["neurria"] or tArg["neurri"] or tArg["size"]
		) or 60) .. "px]]"
	)
end

function export.getTense(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.tense[tArg[1]] or "")
end

function export.getVoice(frame)
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	return (tArg[1] and	t.label.voice[tArg[1]] or "")
end

function export.tabulateInflectedNoun(frame)
	local sRes = [[{| style="background:#cce4fc;text-align:center;width:100%;border-collapse:separate;border-spacing:1px" class="inflection-table"
|-
! style="width:33%;background:#d0d0d0" | ]]
	local tArg = extra.table.cover(frame.getParent and frame:getParent().args, frame.args)
	tArg["ns"] = (
		tArg[1] or
		tArg["ns"] or
		"‑"
	)
	tArg["np"] = (
		tArg[2] or
		tArg["np"] or
		"‑"
	)
	tArg["gs"] = (
		tArg[3] or
		tArg["gs"]
	)
	tArg["gp"] = (
		tArg[4] or
		tArg["gp"]
	)
	tArg["ds"] = (
		tArg[5] or
		tArg["ds"]
	)
	tArg["dp"] = (
		tArg[6] or
		tArg["dp"]
	)
	tArg["as"] = (
		tArg[7] or
		tArg["as"]
	)
	tArg["ap"] = (
		tArg[8] or
		tArg["ap"]
	)
	tArg["gender"] = (
		tArg[9] or
		tArg["generoa"] or
		tArg["genero"] or
		tArg["gender"]
	)
	tArg["pattern"] = (
		tArg[10] or
		tArg["eredua"] or
		tArg["eredu"] or
		tArg["pattern"]
	)
	tArg["note"] = (
		tArg[11] or
		tArg["oharra"] or
		tArg["ohar"] or
		tArg["note"]
	)
	for i = 1, #t.sequence.number do
		sRes = sRes .. "\n" .. [[! style="background:#d0d0d0" | ]] .. eu.getInflectedNounPhrase({args = {
			t.label.number[t.sequence.number[i]],
			"i",
			"i",
			"abs",
			"s",
		}})
	end
	for iCase = 1, #t.sequence.case do
		sRes = sRes .. "\n" .. [[|-
!style="background:#d0d0d0"|]] .. eu.getInflectedNounPhrase({args = {
			t.label.case[t.sequence.case[iCase]],
			"i",
			"i",
			"abs",
			"s",
		}})
		for i = 1, #t.sequence.number do
			local v = tArg[t.sequence.case[iCase] .. t.sequence.number[i]]
			if v then
				local tFind = {(
					string.find(v, "<[Bb][Rr]", 2) or
					string.find(v, "<[Nn][Oo][Ww][Ii][Kk][Ii]", 2) or
					string.find(v, "\n\n", 2)
				)}
				if tFind[1] then
					sRes = sRes .. "\n|" .. v
				else
					sRes = sRes .. "\n|[[" .. v .. "]]"
				end
			else
				local iSelection = 1
				repeat
					local v = export.getInflectedNoun({args = {
						tArg["ns"],
						tArg["np"],
						tArg["gender"],
						tArg["pattern"],
						t.sequence.case[iCase],
						t.sequence.number[i],
						iSelection,
					}})
					if v then
						if v == "‑" then
							sRes = sRes .. "\n|‑"
						else
							sRes = sRes .. (
								iSelection > 1 and "<br />[[" or
								"\n|[["
							) .. v .. "]]"
						end
					end
					iSelection = iSelection + 1
				until v == nil or v == "‑"
			end
		end
	end
	sRes = sRes .. "\n|-"
	if tArg["note"] then
		sRes = sRes .. "\n" .. [[|-
| colspan="4" align="left" style="background:#d9ebff" | ]] .. tArg["note"]
	end
	sRes = sRes .. "\n|}"
	return sRes
end

return export