Module:ti-headword


local export = {}

local is_sequence = require "Module:table".isArray

local function parse_args(args)
	local parsed = {}
	for k, v in pairs(args) do
		if type(k) == "string" then
			local word, number = k:match "^(%l+)(%d*)$"
			if not word then
				error("Invalid parameter key " .. k)
			end
			number = tonumber(number) or 1
			parsed[word] = parsed[word] or {}
			if parsed[word][number] then
				error("Two values for " .. k)
			end
			parsed[word][number] = v
		else
			parsed[k] = v
		end
	end
	return parsed
end

local function validate_args(key_info, args)
	local invalid_keys = {}
	local should_not_be_list = {}
	local should_be_sequence = {}
	for key, values in pairs(args) do
		if not key_info[key] then
			table.insert(invalid_keys, key)
		elseif not (key_info[key] == "multiple" or key_info[key] == "sequence")
			and require "Module:table".length(values) > 1 then
			table.insert(should_not_be_list, key)
		end
	end
	
	for key, info in pairs(key_info) do
		if info == "sequence" and args[k] and not is_sequence(args[k]) then
			table.insert(should_be_sequence, key)
		end
	end
	
	local msg = {}
	if #invalid_keys > 0 then
		table.insert(msg, "Invalid parameters: " .. table.concat(invalid_keys, ", "))
	end
	
	if #should_not_be_list > 0 then
		table.insert(msg, "The following arguments don't accept numbered parameters: "
			.. table.concat(should_not_be_list, ", "))
	end
	
	if #should_be_sequence > 0 then
		table.insert(msg, "The following arguments have gaps: "
			.. table.concat(should_be_sequence, ", "))
	end
	
	
	if #msg > 0 then
		error(table.concat(msg, " "))
	end
	
	for key in pairs(key_info) do
		args[key] = args[key] or {}
	end
end

function export.noun(frame)
	local parameters = {
		head = "sequence",
		g = "multiple",
		tr = "multiple",
		pl = "sequence",
		pltr = "multiple",
		sort = true,
	}
	
	local args = frame:getParent().args
	local parsed_args = parse_args(args)
	validate_args(parameters, parsed_args)
	local lang = require "Module:languages".getByCode("ti")
	
	local plurals = { label = "plural" }
	for i, plural in pairs(parsed_args.pl) do
		plurals[i] = {
			term = plural,
			translit = parsed_args.pltr[i],
		}
	end
	
	for i in pairs(parsed_args.pltr) do
		if not parsed_args.pl[i] then
			error("|pltr" .. i .. " with no corresponding |pl" .. i)
		end
	end
	
	local data = {
		lang = lang,
		heads = parsed_args.head or {},
		translits = parsed_args.tr,
		genders = parsed_args.g,
		inflections = #plurals > 0 and { enable_auto_translit = true, plurals } or {},
		pos_category = "nouns",
		categories = {},
		sort_key = parsed_args.sort[1],
	}
	
	return require("Module:headword").full_headword(data)
end

return export