This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local m_links = require("Module:links")
local m_utilities = require("Module:utilities")

local m_infl =  require("Module:lld-conj/data")

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

local export = {}

-- Shows forms with links, or a dash if empty
local function show_form(subforms)
	if not subforms then
		return "—"
	elseif type(subforms) ~= "table" then
		error("a non-table value was given in the list of inflected forms.")
	elseif #subforms == 0 then
		return "—"
	end
	
	local ret = {}
	
	-- Go over each subform and insert links
	for key, subform in ipairs(subforms) do
		table.insert(ret, m_links.full_link({lang = lang, term = subform}))
	end
	
	return table.concat(ret, ", ")
end

-- Shows the table with the given forms
local function make_table(data)
	local ret = {[=[* Ladin conjugation varies from one region to another. Hence, the following conjugation should be considered as typical, not as exhaustive.
<div class="NavFrame" style="clear:both; margin-top:1em">
<div class="NavHead" style="text-align: left">Conjugation of ]=]}
	table.insert(ret, m_links.full_link({lang = lang, alt = mw.title.getCurrentTitle().text}, "term") .. " (" .. data.conj ..  ")</div>\n")
	table.insert(ret, [=[<div class="NavContent">
{| style="background:#F0F0F0;border-collapse:separate;border-spacing:2px;width:100%" class="inflection-table"
|-
! colspan="1" style="background:#e2e4c0" | infinitive
| colspan="1" | ]=])
	table.insert(ret, show_form(data.forms.inf) .. "\n")
	table.insert(ret, [=[
|-
! colspan="2" style="background:#e2e4c0" | auxiliary verb
| colspan="1" | ]=])
	table.insert(ret, show_form(data.forms.aux) .. "\n")
	table.insert(ret, [=[
! colspan="2" style="background:#e2e4c0" | gerund
| colspan="2" | ]=])
	table.insert(ret, show_form(data.forms.gerund) .. "\n")
	table.insert(ret, [=[
|-
! colspan="2" style="background:#e2e4c0" | past participle
| colspan="2" | ]=])
	table.insert(ret, show_form(data.forms.past_part) .. "\n")
	table.insert(ret, [=[
|-
! colspan="1" rowspan="2" style="background:#C0C0C0" | person
! colspan="3" style="background:#C0C0C0" | singular
! colspan="3" style="background:#C0C0C0" | plural

|-
! style="background:#C0C0C0;width:12.5%" | first
! style="background:#C0C0C0;width:12.5%" | second
! style="background:#C0C0C0;width:12.5%" | third
! style="background:#C0C0C0;width:12.5%" | first
! style="background:#C0C0C0;width:12.5%" | second
! style="background:#C0C0C0;width:12.5%" | third

|-
! style="background:#c0cfe4" colspan="1" | indicative
! style="background:#c0cfe4" | ie
! style="background:#c0cfe4" | tu 
! style="background:#c0cfe4" | el / ela 
! style="background:#c0cfe4" | nos
! style="background:#c0cfe4" | vos
! style="background:#c0cfe4" | ei / eles

|-

! style="height:3em;background:#c0cfe4" colspan="1" | present
]=])
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0cfe4" colspan="1" | imperfect
]=])
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0cfe4" colspan="1" | future
]=])
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="background:#c0e4c0" colspan="1" | subjunctive
! style="background:#c0e4c0" | che ie
! style="background:#c0e4c0" | che tu
! style="background:#c0e4c0" | che el / ela
! style="background:#c0e4c0" | che nos
! style="background:#c0e4c0" | che vos
! style="background:#c0e4c0" | che ei / eles
|-
! style="height:3em;background:#c0e4c0" colspan="1" | present
]=])
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0e4c0" colspan="1" | imperfect
]=])
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_3pl) .. "\n")
	table.insert(ret, [=[
|-
! colspan="1" rowspan="2" style="height:3em;background:#e4d4c0" | imperative
! style="background:#e4d4c0" | –
! style="background:#e4d4c0" | tu
! style="background:#e4d4c0" | –
! style="background:#e4d4c0" | nos
! style="background:#e4d4c0" | vos
! style="background:#e4d4c0" | –
|-
| —
]=])
	table.insert(ret, "| " .. show_form(data.forms.impr_2sg) .. "\n")
	table.insert(ret, "| —\n")
	table.insert(ret, "| " .. show_form(data.forms.impr_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impr_2pl) .. "\n")
	table.insert(ret, "| —\n")
	table.insert(ret, [=[
|}
</div></div>]=])

	return table.concat(ret)
end

-- Main entry point
function export.show(frame)
	local args = mw.clone(frame:getParent().args)
	
	-- Create the forms
	local data = {forms = {}, categories = {}}
	
	if mw.title.getCurrentTitle().nsText ~= "" then return end
	
	local word = mw.title.getCurrentTitle().text
	
	if m_infl[word] then
		args[1] = m_infl[word].get_stem(word)
		m_infl[word](args, data)
	elseif mw.ustring.sub(word, -2, -1) == "ir" then
		args[1] = m_infl["ir"].get_stem(word)
		m_infl["ir"](args, data)
	else
		local conj = frame.args["conj"]
		if m_infl[conj] then
			m_infl[conj](args, data)
		else
			error("Inflection for " .. word .. " not found.")
		end
	end
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

return export