local p = 	{}
local lang_table = require('Module:لغات/بيانات')
local lang_name = {}
local lang_name_with_al = {}
local lang_codes = {}

local scripts = {
	['-latn'] = ' لاتينية' ,
	['-cyrl'] = ' سيريلية' ,
	['-arab'] = ' عربية' ,
	}
for code, v in pairs(lang_table) do
	lang_name[code] = v.name
	lang_codes[v.name] = code
	if v.al_name then
		lang_name_with_al[code] = v.al_name
		lang_codes[v.al_name] = code
	end
	if v.names then
		for i, na in pairs( v.names ) do
			lang_codes[na] = code
		end
	end
	lang_name[code] = v.name
	for i, r in pairs( v.codes ) do
		lang_name[r] = v.name
		if v.al_name then
			lang_name_with_al[r] = v.al_name
		end
	end
end

function gsubname(temp , the , code)
	if lang_name_with_al[code] then
		return lang_name_with_al[code]
	end
	if the and the ~= ''
		then
			s = string.gsub(temp, " ", " ال")
			s = "ال".. s
			fi = s 
		else
			fi = temp 
	end
	return fi
end

function LatnCyrl(code,al,number, returnnil)
	local ar_name = ""
	code = code:lower()
	number = (number or 0) +1
	e = string.sub(code, -5)     -- 5 from the end until the end
	s = string.gsub(code, e, '')
	d = p.getname(s , '' , number)
	if d and d ~='' then
		if scripts[e] then
			ar_name = d ..  scripts[e]
		end
	end
	if returnnil and returnnil ~= '' then
		co = '' 
	else 
		co = code 
	end
	if ar_name == "" then 
		return co
	else 
		return gsubname(ar_name , al , code) 
	end
end

function p.getcode(code)
	s = code
	s = string.gsub(code, ' ', '')
	return lang_name[s]
end
function p.get_name_from_code(code)
	return p.getcode(code)
end

function p.getname(code, al, number, returnnil)
	number = (number or 0) + 1
	if number and number > 3 then return nil end
	if not code or code =='' then return '' end
	code = string.gsub(code , ' ', '')--:lower()
	if code and code~= '' then
		name = p.get_name_from_code(code) 
		if name and name ~= '' 
			then 
				fi = gsubname(name , al , code)
			else
				fi = LatnCyrl(code,al,number, returnnil)
		end
	end
	return fi
end

function get_code_from_name(name)
	if lang_codes[name] then
		return lang_codes[name]
	end
	for ss, v in pairs(lang_table) do
		if (name == p.getname(ss, 't') or name == p.getname(ss , ''))
		then 
			return ss--frame:preprocess(ss)
		else
			for q, codee in pairs( v.codes ) do
				if (name == p.getname(codee, 't') or name == p.getname(codee, ''))
					then return codee
				elseif (name == p.getname(ss .. '-cyrl', 't') or name == p.getname(ss .. '-cyrl', ''))
					then return ss .. '-cyrl'
				elseif (name == p.getname(ss.. '-latn', 't') or  name == p.getname(ss.. '-latn', ''))
					then return ss.. '-latn'
				elseif (name == p.getname(ss.. '-arab', 't') or  name == p.getname(ss.. '-arab', ''))
					then return ss.. '-arab'
				end
			end
		end
	end
	
end

p['اسم لغة'] = function ( frame )
	local na = frame.args[1]
	if not na or na =='' then return '' end
	local code = p.getname(frame.args[1], frame.args[2], 0, frame.args['nil'])
	return frame:preprocess(code)
end

p['قالب رمز لغة'] = function ( frame )
	lange= frame.args[1]
	text = frame.args[2]
	local c = p.getname(frame.args[1], 't')
	if lange and lange ~= '' 
	then
	lange = string.gsub(lange, ' ', '')
	lange = lange:lower()
	textout =  mw.text.tag('span', {lang= lange}, text)
		local p = 'تصنيف:مقالات تحتوي نصا ب'
		if lange =='ar' or lange =='ara'
			then cate = '' 
			else
				cate = '[['.. p .. (c or lange) .. ']]'
		end
		return  textout .. cate
	else
		return ''
	end
end

p['رمز لغة'] = function ( frame )
	local na = frame.args[1]
		if na and na~= '' 
		then
			return get_code_from_name(na) or get_code_from_name( gsubname( na , 't' , "" ) )
		else
			return ''
	end
end


p['قائمة'] = function ( frame )
	lang= require('Module:لغات/بيانات')
	local list = mw.html.create("table")
	list:addClass("wikitable sortable collapsible")
	local head = list:tag("tr")
	head:tag("th"):wikitext("رمز")
	head:tag("th"):wikitext("تضمين قالب")
	head:tag("th"):wikitext("وصلة")
	head:tag("th"):wikitext("اسم")
	head:tag("th"):wikitext("التحويلات")
	for code, nam in pairs(lang_table) do
		local temp = nam.name-- or k
		local row = list:tag("tr")
		local oo = row:tag("td")
		local r = row:tag("td")
		local th = row:tag("td")
		local ss = row:tag("td")
		local rr = row:tag("td")
		local rsr = '{{اسم آيزو 639 ' .. code ..'}}'
		local er = '[[قالب:اسم آيزو 639 ' .. code ..']]'
		local dd = "\n[[لغة ".. temp .."]]"
		local ooo = 'اللغة ' .. gsubname(temp, 'r' ,code )
		oo:tag("span"):wikitext(er)
		r:tag("span"):wikitext(frame:preprocess(rsr))
		th:tag("span"):wikitext(dd)
		ss:tag("span"):wikitext(ooo)
			for k,v in pairs(nam.codes ) do
				local cc =  {}
					if v and v ~= '' then 
				  		u = v
				  	end
				rr:tag("code"):wikitext(u)
				rr:tag("span"):wikitext(' - ') 
			end
			end
	return list
end

return p