input (parameter) name (function) code (function) number (function)
[[Khitan]] Khitan large script Kitl 505
Kitl Khitan large script Kitl 505
Duployan Duployan shorthand Dupl 755
Latin Latin Latn 215
Klingon Klingon Piqd 293
Klingon Klingon Piqd 293

local p = {}
local getArgs = require('Module:Arguments').getArgs
local data = mw.loadData('Module:Language/data/iso 15924')

function p.code(frame)                                                          -- to output part 1 code
	frame.args.type = "code"
	return p.main(frame)
end

function p.name(frame)                                                          -- to output name
	frame.args.type = "name"
	return p.main(frame)
end

function p.number(frame)                                                          -- to output name
	frame.args.type = "number"
	return p.main(frame)
end

function p.main(frame)                                                          -- main function (doesn't need to be called directly)
	local args = getArgs(frame)
	
	local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A",                   -- accent list
		["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E",
		["Ê"]="E",["Ë"]="E",["Ì"]="I",["Í"]="I",["Î"]="I",
		["Ï"]="I",["Ñ"]="N",["Ò"]="O",["Ó"]="O",["Ô"]="O",
		["Õ"]="O",["Ö"]="O",["Ø"]="O",["Ù"]="U",["Ú"]="U",
		["Û"]="U",["Ü"]="U",["Ý"]="Y"
	}
		
	args[1] = mw.ustring.gsub(args[1],"[À-Ý]",accents)                          -- Deaccent
	args[1] = mw.ustring.gsub(args[1],"%[","")                                  -- Delink
	args[1] = mw.ustring.gsub(args[1],"%]","")                                  -- Delink
	args[1] = mw.ustring.gsub(args[1],"%{","")                                  -- Remove {
	args[1] = mw.ustring.gsub(args[1],"%}","")                                  -- Remove }
	
	for code,table in pairs(data) do
		for _,value in pairs(table) do
			if string.lower(args[1]) == string.lower(value) then
				args[1] = code
			end
		end
		
		if string.lower(args[1]) == string.lower(code) 
		or string.lower(args[1]) == table.num then
			if args.type == "code" then
				return code or ""
			elseif args.type == "name" then
				return table[1] or ""
			elseif args.type == "number" then
				return table.num or ""
			end
		end
	end
end

return p