local export = {}

local common = require("Module:akk-common")
local links = require("Module:links")
local lang = require("Module:languages").getByCode("akk")

local NOTE = "This table gives Old Babylonian inflection. For conjugation in other dialects, see [[Appendix:Akkadian dialectal conjugation]]."

local function column(cun, trans, i)
	local number_gender
	if cun.number[i][2] and cun.number[i][2] == "m" then
		number_gender = '\n! style="background-color:#cdcdcd" rowspan="2" |' .. cun.number[i][1] .. '\n! style="background-color:#cdcdcd; width: 1em" rowspan="1" |' .. cun.number[i][2]
	elseif cun.number[i][2] then
		number_gender = '\n! style="background-color:#cdcdcd; width: 1em" rowspan="1" |' .. cun.number[i][2]
	else
		number_gender = '\n! style="background-color:#cdcdcd" rowspan="1" colspan="2" |' .. cun.number[i][1]
	end
	return '\n|-  class="vsHide"' .. number_gender ..
			   '\n| style="background-color:#fdfdfd;" |' .. trans.dur[i] ..
			   '\n| style="background-color:#fdfdfd;" |' .. trans.perf[i] ..
			   '\n| style="background-color:#fdfdfd;" |' .. trans.pret[i] ..
			   '\n| style="background-color:#fdfdfd;" |' .. trans.imp[i]

end

local function top(stem, cun, trans, title)

	return
		'{| class="inflection-table vsSwitcher autocollapsed" data-toggle-category="conjugation" style="background:#fdfdfd; text-align:center; border: 0.5px solid #cdcdcd;"' ..
			'\n|- style="background: #dedede"' ..
			'\n! class="vsToggleElement" style="text-align: center; width:20em" colspan="7" | ' .. title .. '\n|-' .. stem .. '\n|- class="vsHide"' ..
			'\n! style="background-color:#cdcdcd; min-width: 5em" rowspan="1" colspan="2" | Infinitive' ..
			'\n| style="background-color:#fdfdfd; font-size: 100%;" |' .. trans.inf .. '\n|- class="vsHide"' .. '\n' ..
			'\n! style="background-color:#cdcdcd; min-width: 5em" rowspan="1" colspan="2" | Participle' ..
			'\n| style="background-color:#fdfdfd; font-size: 100%;" |' .. trans.part .. '\n|- class="vsHide"' ..
			'\n! style="background-color:#cdcdcd; min-width: 5em" rowspan="1" colspan="2" | Adjective' ..
			'\n| style="background-color:#fdfdfd; font-size: 100%;" |' .. trans.adj .. 
			'\n|- class="vsHide"' ..
			'\n! style="background-color:#cdcdcd; min-width: 3em" colspan="2" |Active' ..
			'\n! style="background-color:#cdcdcd; min-width: 4.5em" |Durative' ..
			'\n! style="background-color:#cdcdcd; min-width: 4.5em" |Perfect' ..
			'\n! style="background-color:#cdcdcd; min-width: 4.5em" |Preterite' ..
			'\n! style="background-color:#cdcdcd; min-width: 4.5em" |Imperative'
end

local function bottom()
	return '\n|- class="vsHide"' .. '\n| style="font-size:85%; text-align:left" colspan="7" |' .. NOTE .. '\n|}'
end

function export.render_cuneiform(stem, cun, trans, title)
	local template = top(stem, cun, trans, title or "Cuneiform")

	for i, _ in ipairs(cun.number) do
		template = template .. column(cun, trans, i)
	end

	template = template .. bottom()
	return template
end

local function map_column(column, func)
	local output = {}
	for i, word in ipairs(column) do output[i] = func(word) end
	return output
end
local function map_table(table, func)
	local output = {
		number = table.number,
		dur = map_column(table.dur, func),
		pret = map_column(table.pret, func),
		perf = map_column(table.perf, func),
		imp = map_column(table.imp, func),
		inf = func(table.inf),
		part = func(table.part),
		adj = func(table.adj)
	}
	return output
end
export.map_table = map_table

function export.render_transcription(stem, trans)
	local dummy = {}
	local empty = {"", "", "", "", "", "", "", ""}
	dummy.inf = ""
	dummy.part = ""
	dummy.adj = ""
	dummy.dur = empty
	dummy.pret = empty
	dummy.perf = empty
	dummy.imp = empty
	return export.render_cuneiform(stem, trans, dummy, "Normalization")
end

function export.render(stem, trans, debug)
	local italicize = function(word) return word end
	local normalisations = map_table(trans, italicize)
	local cun = trans
	cun = map_table(cun, function(term)
		if cun == "-" then return "" end
		return links.full_link({lang = lang, term = term})
	end)
	if debug then
		return export.render_transcription(stem, normalisations)
	else
		return export.render_cuneiform(stem, cun, normalisations, "Conjugation")
	end
end

return export