local p = {}
local replaces_ = {
	{c = "𐩽",	ar = " ",	file = "OLD SOUTH ARABIAN NUMBER ONE.svg"},
	{c = "𐩱",	ar = "ا",	other = {"أ", "إ", "آ", "ى"},	file = "OLD SOUTH ARABIAN LETTER ALEF.svg"},
	{c = "𐩨",	ar = "ب",	file = "OLD SOUTH ARABIAN LETTER BETH.svg"},
	{c = "𐩩",	ar = "ت",	other = {"ة"},	file = "OLD SOUTH ARABIAN LETTER TAW.svg"},
	{c = "𐩻",	ar = "ث",	file = "OLD SOUTH ARABIAN LETTER THAW.svg"},
	{c = "𐩴",	ar = "ج",	file = "OLD SOUTH ARABIAN LETTER GIMEL.svg"},
	{c = "𐩢",	ar = "ح",	file = "OLD SOUTH ARABIAN LETTER HETH.svg"},
	{c = "𐩭",	ar = "خ",	file = "OLD SOUTH ARABIAN LETTER KHETH.svg"},
	{c = "𐩵",	ar = "د",	file = "OLD SOUTH ARABIAN LETTER DALETH.svg"},
	{c = "𐩹",	ar = "ذ",	file = "OLD SOUTH ARABIAN LETTER DHALETH.svg"},
	{c = "𐩧",	ar = "ر",	file = "OLD SOUTH ARABIAN LETTER RESH.svg"},
	{c = "𐩸",	ar = "ز",	file = "OLD SOUTH ARABIAN LETTER ZAYN.svg"},
	{c = "𐩪",	ar = "س",	file = "OLD SOUTH ARABIAN LETTER SAT.svg"},
	{c = "𐩯",	ar = "x",	file = "OLD SOUTH ARABIAN LETTER SAMEKH.svg"},
	{c = "𐩦",	ar = "ش",	file = "OLD SOUTH ARABIAN LETTER SHIN.svg"},
	{c = "𐩮",	ar = "ص",	file = "OLD SOUTH ARABIAN LETTER SADHE.svg"},
	{c = "𐩳",	ar = "ض",	file = "OLD SOUTH ARABIAN LETTER DHADHE.svg"},
	{c = "𐩷",	ar = "ط",	file = "OLD SOUTH ARABIAN LETTER TETH.svg"},
	{c = "𐩼",	ar = "ظ",	file = "OLD SOUTH ARABIAN LETTER THETH.svg"},
	{c = "𐩲",	ar = "ع",	file = "OLD SOUTH ARABIAN LETTER AYN.svg"},
	{c = "𐩶",	ar = "غ",	file = "OLD SOUTH ARABIAN LETTER GHAYN.svg"},
	{c = "𐩰",	ar = "ف",	file = "OLD SOUTH ARABIAN LETTER FE.svg"},
	{c = "𐩤",	ar = "ق",	file = "OLD SOUTH ARABIAN LETTER QOPH.svg"},
	{c = "𐩫",	ar = "ك",	file = "OLD SOUTH ARABIAN LETTER KAPH.svg"},
	{c = "𐩡",	ar = "ل",	file = "OLD SOUTH ARABIAN LETTER LAMEDH.svg"},
	{c = "𐩣",	ar = "م",	file = "OLD SOUTH ARABIAN LETTER MEM.svg"},
	{c = "𐩬",	ar = "ن",	file = "OLD SOUTH ARABIAN LETTER NUN.svg"},
	{c = "𐩠",	ar = "ه",	file = "OLD SOUTH ARABIAN LETTER HE.svg"},
	{c = "𐩥",	ar = "و",	file = "Himjar wa.svg"},--OLD SOUTH ARABIAN LETTER WAW.svg
	{c = "𐩺",	ar = "ي",	file = "OLD SOUTH ARABIAN LETTER YODH.svg"},
}
local replaces = {}

for _, v in ipairs(replaces_) do
	replaces[v.c] = v.file
	replaces[v.ar] = v.file
	if v.other then
		for _, u in ipairs(v.other) do
			replaces[u] = v.file
		end
	end
end

local function file(a)
	-- local size = a == "OLD SOUTH ARABIAN LETTER WAW.svg" and "35x35px" or "25x25px"
	return "[[ملف:" .. a .. "|25x25px|link=]]"
end

local function replace(text)
	text = mw.text.trim(text)
	-- replace every letter in text with its equivalent using mw.ustring.gmatch
	local result = mw.ustring.gsub(text, ".", function(c)
		if replaces[c] then
			return file(replaces[c])
		else
			return c
		end
	end)
	mw.log(result)
	return result
end

function p.s(frame)
	local text = frame.args[1]
	if not text or text == '' then return '' end
	local newtext = replace(text)
	return newtext
end

function p.tables(frame)

	-- build table
	-- إنشاء جدول
	local root = mw.html.create('table')
		:addClass('wikitable')
		:css('font-size', '30px')
		:css('text-align', 'center')
	
	local style ="position: sticky;top: 0;left: 0;"

	local head = root:tag('tr')
	head:tag('th')
		:wikitext('الحرف العربي')
		:css('font-size', '18px')
		:cssText(style)

	local row1 = root:tag('tr')
	row1:tag('th')
		:wikitext('حرف المسند')
		:css('font-size', '18px')
		:cssText(style)

	local row2 = root:tag('tr')
	row2:tag('th')
		:wikitext('الناتج')
		:css('font-size', '18px')
		:cssText(style)
		
	local sortedTable = {}

	for key, value in pairs(replaces_) do
		table.insert(sortedTable, {key = key, value = value})
	end

	table.sort(sortedTable, function(a, b)
		return a.value.ar < b.value.ar
	end)

	for _, entry in ipairs(sortedTable) do
		local ar = entry.value.ar ~= ' ' and entry.value.ar or 'مسافة'
		-- local size = entry.value.file == "OLD SOUTH ARABIAN LETTER WAW.svg" and "50x50px" or "40x40px"
		head:tag('td')
			:wikitext(ar)
		row1:tag('td')
			:wikitext(entry.value.c)
		row2:tag('td')
			:wikitext("[[ملف:" .. entry.value.file .. "|40x40px]]")
	end

	local table = tostring(root)
	-- <div style="width:100%;overflow-x:auto; overflow-y:auto"></div>
	local div = mw.html.create('div')
		div:css('width', '100%')
		div:css('overflow-x', 'auto')
		div:css('overflow-y', 'auto')
		div:wikitext(table)

	return div
end

return p