local concat = table.concat
local html_create = mw.html.create
local insert = table.insert
local require = require
local upper = string.upper

local format_categories = require("Module:utilities").format_categories
local full_link = require("Module:links").full_link
local language_link = require("Module:links").language_link
local plain_link = require("Module:links").plain_link
local process_params = require("Module:parameters").process
local show_labels = require("Module:labels").show_labels

local ms = require("Module:languages").getByCode("ms")
local mul = require("Module:languages").getByCode("mul")

local export = {}

local function insert_link(main_text, text, wiki)
	if #main_text == 0 then
		text = text:gsub("^.", upper)
	end
	insert(main_text, plain_link{
		lang = mul,
		term = wiki,
		alt = text
	})
end

function export.show(frame)
	local boolean = {type = "boolean"}
	local required_num = {required = true, type = "number"}
	local params = {
		[1] = required_num,
		[2] = {required = true},
		[3] = true,
		["exr"] = boolean,
		["from"] = true,
		["inr"] = boolean,
		["obs"] = boolean,
		["to"] = true
	}
	
	local parent_args = frame:getParent().args
	if parent_args[1] == "1" then
		insert(params, 2, required_num)
	end
	
	local args = process_params(parent_args, params)
	
	local labels = {"piawaian antarabangsa"}
	if args.obs then
		insert(labels, "obsolete")
	end
	local label = show_labels{
		lang = mul,
		labels = labels
	}
	
	local main_text, categories = {}, {}
	
	if args.obs then
		insert(main_text, "Bekas")
	end
	if args.exr then
		insert_link(main_text, "exceptionally reserved", "w:ISO 3166-1 alpha-2#Exceptional reservations")
	end
	if args.inr then
		insert_link(main_text, "indeterminately reserved", "w:ISO 3166-1 alpha-2#Indeterminate reservations")
	end
	
	if args[1] == 1 then
		insert_link(main_text, "ISO 3166-1", "w:ISO 3166-1")
		if args[2] == 2 or args[2] == 3 then
			insert_link(main_text, "alpha-" .. args[2], "w:ISO 3166-1 alpha-" .. args[2])
		else
			error("The parameter \"2\" must be 2 or 3.")
		end
		insert(main_text, language_link{
			lang = ms,
			term = "kod negara",
		})
		insert(main_text, "bagi")
		insert(main_text, full_link(
			{
				lang = ms,
				term = args[3],
				alt = args[4] or args[3]
			},
			"term"
		))
		insert(categories, "ISO 3166-1 alpha-" .. args[2])
	elseif args[1] == 2 or args[1] == 3 then
		insert_link(main_text, "ISO 3166-" .. args[1], "w:ISO 3166-" .. args[1])
		insert(main_text, language_link{
			lang = ms,
			term = "kod negara",
		})
		insert(main_text, "bagi")
		insert(main_text, full_link(
			{
				lang = ms,
				term = args[2],
				alt = args[3] or args[2]
			},
			"Perkataan"
		))
	else
		error("The parameter \"1\" must be 1, 2 or 3.")
	end
	
	if args.from then
		if args.to then
			insert(main_text, "dari " .. args.from .. " hingga " .. args.to)
		else
			insert(main_text, "sejak " .. args.from)
		end
	elseif args.to then
		insert(main_text, "hingga " .. args.to)
	end
	
	categories = format_categories(categories, mul)
	
	return label .. " " .. tostring(html_create("span")
		:addClass("use-with-mention")
		:wikitext(concat(main_text, " ") .. ".")) .. categories
end

return export