local export = {}

local Array = require 'Module:array'

local function add_forms(wikitable, forms)
	if type(wikitable) ~= 'string' then
		error('Expected string, got ' .. type(wikitable))
	end
	
	wikitable = wikitable:gsub('{{{([^}]+)}}}', forms)
	return wikitable
end

local stylesheet = require("Module:TemplateStyles")("Template:av-decl-1st/style.css")

function export.make_table_sg(data)
	local output = Array(data.title, stylesheet)
	output:insert [=[

{| class="wikitable inflection-table inflection-table-av"
|-
! class="corner-header" | Case
! class="number-header" | Singular
|-
! class="case-header" | [[absolutive case|Absolutive]]
| class="form-cell" | {{{abs_sg}}}
|-
! class="case-header" | [[ergative case|Ergative]]
| class="form-cell" | {{{erg_sg}}}
|-
! class="case-header" | [[genitive case|Genitive]]
| class="form-cell" | {{{gen_sg}}}
|-
! class="case-header" | [[dative case|Dative]]
| class="form-cell" | {{{dat_sg}}}
|-
! class="case-header" | [[instrumental case|Instrumental]] 
| class="form-cell" | {{{ins_sg}}}
|-]=]
	
	output:insert('\n|}')
	if data.footnotes and data.footnotes ~= "" then
		output:insert('\n' .. data.footnotes)
	end

	return add_forms(output:concat(), data.forms)
end

function export.make_table_pl(data)
	local output = Array(data.title, stylesheet)
	output:insert [=[

{| class="wikitable inflection-table inflection-table-av"
|-
! class="corner-header" | Case
! class="number-header" | Plural
|-
! class="case-header" | [[absolutive case|Absolutive]]
| class="form-cell" | {{{abs_pl}}}
|-
! class="case-header" | [[ergative case|Ergative]]
| class="form-cell" | {{{erg_pl}}}
|-
! class="case-header" | [[genitive case|Genitive]]
| class="form-cell" | {{{gen_pl}}}
|-
! class="case-header" | [[dative case|Dative]]
| class="form-cell" | {{{dat_pl}}}
! class="case-header" | [[instrumental case|Instrumental]] 
| class="form-cell" | {{{ins_pl}}}
|-]=]
	
	output:insert('\n|}')
	if data.footnotes and data.footnotes ~= "" then
		output:insert('\n' .. data.footnotes)
	end
	
	return add_forms(output:concat(), data.forms)
end

function export.make_table(data)
	local output = Array(data.title, stylesheet)
	output:insert [=[

{| class="wikitable inflection-table inflection-table-la"
|-
! class="corner-header" | Case
! class="number-header" | Singular
! class="number-header" | Plural
|-
! class="case-header" | [[absolutive case|Absolutive]]
| class="form-cell" | {{{abs_sg}}}
| class="form-cell" | {{{abs_pl}}}
|-
! class="case-header" | [[ergative case|Ergative]]
| class="form-cell" | {{{erg_sg}}}
| class="form-cell" | {{{erg_pl}}}
|-
! class="case-header" | [[genitive case|Genitive]]
| class="form-cell" | {{{gen_sg}}}
| class="form-cell" | {{{gen_pl}}}
|-
! class="case-header" | [[dative case|Dative]]
| class="form-cell" | {{{dat_sg}}}
| class="form-cell" | {{{dat_pl}}}
|-
! class="case-header" | [[instrumental case|Instrumental]]
| class="form-cell" | {{{ins_sg}}}
| class="form-cell" | {{{ins_pl}}}
|-]=]
	
	output:insert('\n|}')
	if data.footnotes and data.footnotes ~= "" then
		output:insert('\n' .. data.footnotes)
	end
	
	return add_forms(output:concat(), data.forms)
end

return export