local export = {}

local force_cat = false -- for debugging

function export.categorize(lang, source, roots, ids, sort_key)
	local categories = {}
	
	if lang:getCode() ~= source:getCode() then
		table.insert(categories,
			"Perkataan bahasa " .. lang:getFullName() .. " diterbitkan daripada bahasa " .. source:getCanonicalName())
	end
	
	for index, root in ipairs(roots) do
		if lang:getCode() == source:getCode() then
			table.insert(categories,
				"Perkataan bahasa " .. lang:getFullName() .. " milik akar " .. root .. (ids[index] and " (" .. ids[index] .. ")" or ""))
		else
			table.insert(categories,
				"Perkataan bahasa " .. lang:getFullName() .. " diterbitkan daripada akar bahasa " .. source:getCanonicalName() ..
						" " .. root .. (ids[index] and " (" .. ids[index] .. ")" or ""))
		end
	end
	
	return require("Module:utilities").format_categories(categories, lang, sort_key, nil, force_cat)
end

function export.show(frame)
	local params = {
		[1] = { required = true, type = "language" },
		[2] = { required = true, type = "language" },
		[3] = { required = true, list = true },
		["id"] = { list = true, allow_holes = true },
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	return export.categorize(args[1], args[2], args[3], args.id, args.sort)
end

return export