"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:cuneiform spellings/doc पर बनाया जा सकता है"

local export = {}

local m_links = require("Module:links")
local lang = require("Module:languages").getByCode("akk")
local m_table = require("Module:table")
local m_strutils = require("Module:string utilities")

local function format_list_items(items)
	local result = {}

	for _, item in ipairs(items) do
		if type(item) == "table" then
			local link = m_links.full_link(item.term)
			if item.q then
				link = link .. " " .. require("Module:qualifier").format_qualifier(item.q)
			end
			item = link
		elseif lang and not string.find(item, "<span") then
			item = m_links.full_link {lang = lang, term = item}
		end

		table.insert(result, '\n* ' .. item)
	end

	return table.concat(result)
end

function export.create_list(args)
	if type(args) ~= "table" then error("expected table, got " .. type(args)) end

	local output = {}

	table.insert(output, [[<div class="term-list ul-column-count" data-column-count="1" >]])

	if args.alphabetize then
		local function keyfunc(item)
			if type(item) == "table" then item = item.term.term end
			item = item:gsub("<.->", "")
			return item
		end
		require("Module:collation").sort(args, lang, keyfunc)
	end
	table.insert(output, format_list_items(args))

	table.insert(output, '</div>')

	return table.concat(output)
end

local param_mods = {"t", "tr", "g", "q"}
local param_mod_set = m_table.listToSet(param_mods)

function export.parse_params(items)
	local iut
	for i, item in ipairs(items) do
		if item:find("<") and not item:find("^[^<]*<[a-z]*[^a-z:]") then
			if not iut then
				iut = require("Module:inflection utilities")
			end
			local run = iut.parse_balanced_segment_run(item, "<", ">")
			local termobj = {term = {}}
			termobj.term.lang = lang
			termobj.term.term = run[1]

			for j = 2, #run - 1, 2 do
				if run[j + 1] ~= "" then
					parse_err("Extraneous text '" .. run[j + 1] .. "' after modifier")
				end
				local modtext = run[j]:match("^<(.*)>$")
				if not modtext then
					parse_err("Internal error: Modifier '" .. modtext .. "' isn't surrounded by angle brackets")
				end
				local prefix, arg = modtext:match("^([a-z]+):(.*)$")
				if not prefix then
					parse_err("Modifier " .. run[j] .. " lacks a prefix, should begin with one of '" .. table.concat(param_mods, ":', '") .. ":'")
				end
				if param_mod_set[prefix] then
					local obj_to_set
					if prefix == "q" then
						obj_to_set = termobj
					else
						obj_to_set = termobj.term
					end
					if obj_to_set[prefix] then
						parse_err("Modifier '" .. prefix .. "' occurs twice, second occurrence " .. run[j])
					end
					if prefix == "t" then
						termobj.term.gloss = arg
					elseif prefix == "g" then
						termobj.term.genders = mw.text.split(arg, ",")
					else
						obj_to_set[prefix] = arg
					end
				else
					parse_err("Unrecognized prefix '" .. prefix .. "' in modifier " .. run[j])
				end
			end
			items[i] = termobj
		end
	end
	return items
end

function export.display(frame)
	local parent_args = frame:getParent().args

	local params = {
		["sum"] = {list = true},
		["phon"] = {list = true},
		["mix"] = {list = true}
	}

	local args = require("Module:parameters").process(parent_args, params)

	local top = [===[
		{| class="wikitable"
        |+ style="text-align:left; white-space: nowrap;"| Cuneiform spellings
		|- style="text-align:center;"
        ]===]

    local headers = ""
    local data = ""

    if next(args["sum"]) ~= nil then
        headers = headers .. [===[
			! Logograms
			]===]
        data = data .. [===[
			| {sum}
			]===]
    end
    if next(args["phon"]) ~= nil then
        headers = headers .. [===[
			! Phonetic
			]===]
        data = data .. [===[
			| {phon}
			]===]
    end
    if next(args["mix"]) ~= nil then
        headers = headers .. [===[
			! Mixed
			]===]
        data = data .. [===[
			| {mix}
			]===]
    end

    local template = top .. headers .. [===[
		|- valign="top"
		]===] .. data .. [===[
			|{\cl}]===]

	local forms = {}

	forms["sum"] = export.create_list(export.parse_params(args["sum"]))
	forms["phon"] = export.create_list(export.parse_params(args["phon"]))
	forms["mix"] = export.create_list(export.parse_params(args["mix"]))

	return m_strutils.format(template, forms)

end

return export