This module implements {{fax-conj}}.


local lang = require("Module:languages").getByCode("fax")
local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
local m_data = require("Module:fax-verb/data")

local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local gsub = mw.ustring.gsub
local PAGENAME = mw.title.getCurrentTitle().text

local export = {}

-- Dialects. Arbitrary order (alphabetical)
local d = {["L"] = 1, ["M"] = 2, ["V"] = 3}
local d_full = {["L"] = "Lagarteiru", ["M"] = "Mañegu", ["V"] = "Valverdeñu"}

-- Inflection functions
function export.show(frame)
	local parent_args = frame:getParent().args
	local dialects = parent_args[1] or nil
	local base = parent_args[2] or PAGENAME
	
	--find the ending of the verb
	local infl_type = ""
	if base:match("[aei]lsi$") then
		infl_type = base:match("[aei]lsi$")
	elseif base:match("[aei]l$") then
		infl_type = base:match("[aei]l$")
	else
		if not m_data[base] then error("This word doesn't have a valid ending.") end
	end

	local data = {forms = {}, categories = {}}
	
	data.head = base
	data.infl_type = infl_type
	data.prefix = parent_args.prefix or ""
	
	-- Generate the forms
	if m_data[base] then
		m_data[base](parent_args, data)
	else
		args = require("Module:parameters").process(parent_args, m_data[infl_type].params, true)
		m_data[infl_type](args, data)
	end
	
	data.info = m_links.full_link({lang = lang, alt = data.prefix .. data.head}, "term")
	
	--generate the table and add categories
	if not dialects then
		error("Please provide the dialects in which this verb is used.")
	elseif not dialects:match("^L?M?V?$") then
		error("Please provide the dialects in which this verb is used in the proper format.")
	else
		local tables = ""
		for dialect in gmatch(dialects, "%u") do
			data.dialect = dialect
			tables = tables .. make_table(data)
		end
		return tables .. (not parent_args.nocat and m_utilities.format_categories(data.categories, lang) or "")
	end
end

-- Make the table
function make_table(data)
	function export.accel_dialect()
		return data.dialect
	end
	
	local function repl(param)
		if param == "info" then
			return mw.getContentLanguage():ucfirst(data.info or "")
		elseif param == "dialect" then
			return mw.getContentLanguage():ucfirst(d_full[data.dialect] or "")
		elseif param == "alternation" then
			return mw.getContentLanguage():ucfirst(data.alternation or "")
		end
		
		local np = data.forms[param]
		local forms = np and (np["LMV"] and np[1] or np[d[data.dialect]]) or nil
		
		if not forms then
			return "—"
		end
		
		local function make_accel(key, partmsg)
			local accel_form, pos, lemma = {}, "verb", PAGENAME
			key = key:gsub("impfsi", "impf")
			if key == "infinitive" or key == "gerund" then
				accel_form = {key:sub(1,3)}
			elseif key == "partmsg" then
				accel_form = {"past", "part"}
				pos = "past participle"
			elseif key:sub(1,4) == "part" then
				accel_form = {key:sub(5,5), key:sub(6,7)}
				pos = "past participle"
				lemma = partmsg or "test"
			elseif key:match("impr") then
				accel_form = {key:sub(5,5), key:sub(6,7), key:sub(1,4)}
			else
				accel_form = {key:sub(8,8), key:sub(9,10), key:sub(1,4), key:sub(5,7)}
			end
			
			return  {form = table.concat(accel_form, "|"), pos = pos, lemma = lemma}
		end
		
		local ret, partmsg = {}, ""
		for _, subform in ipairs(forms) do
			if param == "partmsg" then partmsg = data.prefix .. subform end
			table.insert(ret, m_links.full_link({lang = lang, term = data.prefix .. subform, accel = make_accel(param, partmsg)}))
		end
		return table.concat(ret, "<br/>")
	end
	
	local wikicode = [=[
<div class="NavFrame">
<div class="NavHead" style="text-align: left">Conjugation of {{{info}}} ({{{dialect}}}{{{alternation}}})</div>
<div class="NavContent">
{| style="background:#f9f9f9;width:100%" class="inflection-table"
|-
! colspan="3" style="background:#e2e4c0" | infinitive
| colspan="5" align="center" | {{{infinitive}}}
|-
! colspan="3" style="background:#e2e4c0" | gerund
| colspan="5" align="center" | {{{gerund}}}
|-
! colspan="2" rowspan="3" style="background:#e2e4c0" | past participle
| colspan ="2" style="background:#e2e4c0" | 
! colspan ="2" style="background:#e2e4c0" | masculine
! colspan ="2" style="background:#e2e4c0" | feminine
|-
! colspan = "2" style="background:#e2e4c0" | singular
| colspan = "2" align="center" | {{{partmsg}}}
| colspan = " 2" align="center" | {{{partfsg}}}
|-
! colspan = "2" style="background:#e2e4c0" | plural
| colspan = "2" align="center" | {{{partmpl}}}
| colspan = "2" align="center" | {{{partfpl}}}
|-
! colspan = "2" rowspan= "2" style="background:#DEDEDE" | 
! colspan = "3" style="background:#DEDEDE" | singular
! colspan = "3" style="background:#DEDEDE" | plural
|-
! style="background:#DEDEDE" | 1st person
! style="background:#DEDEDE" | 2nd person
! style="background:#DEDEDE" | 3rd person
! style="background:#DEDEDE" | 1st person
! style="background:#DEDEDE" | 2nd person
! style="background:#DEDEDE" | 3rd person
|-
! rowspan="6" style="background:#c0cfe4" | indicative
! style="background:#ECECEC;width:12.5%" |
! style="background:#ECECEC;width:12.5%" | ei
! style="background:#ECECEC;width:12.5%" | tú
! style="background:#ECECEC;width:12.5%" | el/ela
! style="background:#ECECEC;width:12.5%" | nos
! style="background:#ECECEC;width:12.5%" | vos
! style="background:#ECECEC;width:12.5%" | elis/elas
|-
! style="height:3em;background:#ececec" | present
|{{{presind1sg}}}
|{{{presind2sg}}}
|{{{presind3sg}}}
|{{{presind1pl}}}
|{{{presind2pl}}}
|{{{presind3pl}}}
|-
! style="height:3em;background:#ececec" | imperfect
|{{{impfind1sg}}}
|{{{impfind2sg}}}
|{{{impfind3sg}}}
|{{{impfind1pl}}}
|{{{impfind2pl}}}
|{{{impfind3pl}}}
|-
! style="height:3em;background:#ececec" | preterite
|{{{pretind1sg}}}
|{{{pretind2sg}}}
|{{{pretind3sg}}}
|{{{pretind1pl}}}
|{{{pretind2pl}}}
|{{{pretind3pl}}}
|-
! style="height:3em;background:#ececec" | future
|{{{futrind1sg}}}
|{{{futrind2sg}}}
|{{{futrind3sg}}}
|{{{futrind1pl}}}
|{{{futrind2pl}}}
|{{{futrind3pl}}}
|-
! style="height:3em;background:#ececec" | conditional
|{{{condind1sg}}}
|{{{condind2sg}}}
|{{{condind3sg}}}
|{{{condind1pl}}}
|{{{condind2pl}}}
|{{{condind3pl}}}
|-
! style="background:#DEDEDE;height:.75em" colspan="8" |
|-
! style="background:#c0e4c0" rowspan="4" | subjunctive
! style="background:#ECECEC;width:12.5%" |
! style="background:#ECECEC;width:12.5%" | ei
! style="background:#ECECEC;width:12.5%" | tú
! style="background:#ECECEC;width:12.5%" | el/ela
! style="background:#ECECEC;width:12.5%" | nos
! style="background:#ECECEC;width:12.5%" | vos
! style="background:#ECECEC;width:12.5%" | elis/elas
|-
! style="height:3em;background:#ececec" | present
|{{{pressub1sg}}}
|{{{pressub2sg}}}
|{{{pressub3sg}}}
|{{{pressub1pl}}}
|{{{pressub2pl}}}
|{{{pressub3pl}}}
|-
! style="height:3em;background:#ececec" | imperfect (ra)
|{{{impfsub1sg}}}
|{{{impfsub2sg}}}
|{{{impfsub3sg}}}
|{{{impfsub1pl}}}
|{{{impfsub2pl}}}
|{{{impfsub3pl}}}
|-
! style="height:3em;background:#ececec" | imperfect (si)
|{{{impfsisub1sg}}}
|{{{impfsisub2sg}}}
|{{{impfsisub3sg}}}
|{{{impfsisub1pl}}}
|{{{impfsisub2pl}}}
|{{{impfsisub3pl}}}
|-
! style="background:#DEDEDE;height:.75em" colspan="8" |
|-
!  style="height:3em;background:#e4d4c0" colspan="2" | imperative
| —
|{{{impr2sg}}}
| —
| —
|{{{impr2pl}}}
| —
|}
</div></div>]=]
	return gsub(wikicode, "{{{[#!]?([a-z0-9|]+)}}}", repl)
end

return export