local aliases = {
    width = {'العرض', 'عرض'},
    title = {'عنوان', 'العنوان'},
    shaer = {'الشاعر', 'شاعر'},
    bahar = {'البحر', 'بحر'},
    nass = {1,'النص', 'نص'},

}
local string = mw.ustring
local function argument_wrapper(arg)
	local nilargs = {}
	return setmetatable({},
	{
		__index = function ( tbl, k )
			local v = rawget(tbl,k)
			if v then
				return v
			elseif nilargs[k] then
				return nil
			end
			local list = aliases[k];
			for _,arglist in ipairs(arg) do
				if type( list ) == 'table' then
					for _, alias_key in ipairs( list ) do
						if arglist[alias_key] then
							v = arglist[alias_key]
							break;
						end
					end
				elseif list ~= nil then
					v = arglist[list]
				end

				if v then
					break;
				end
			end
			if v == nil then
				nilargs[k] = true
			else
				rawset( tbl, k, v )
			end
			return v
		end,
	});
end

local function abyat(frame)
	local A = argument_wrapper({frame:getParent().args, frame.args})
	local mashtoor = false
	local header = ''
	local sdrdiv = '<div class="abyat-sdr">'
	if not string.find(A.nass,"\\\\") then
		mashtoor=true
		sdrdiv = '<div>'
	end
	local ret = '<div class="abyat-wrapper' .. (mashtoor and ' mashtoor' or '') .. '" style="width:' .. (A.width or 'fit-content') .. ';">'
	
    if A.title then
        header = header .. "<center>'''" .. A.title .. "'''</center>"
    end
    if A.shaer then
        header = header .. "<center>" .. A.shaer .. "</center>"
    end
    if A.bahar then
        header = header .. "<center>''[من " .. A.bahar .. "]''</center>"
    end
    A.nass = mw.text.trim(A.nass)
    if string.sub(A.nass,1,2) == '\\\\' then
        ret = ret .. '<div class="abyat-single_bayt">'
        A.nass = string.sub(A.nass,3)
    else
        ret = ret .. sdrdiv
    end

    A.nass=string.gsub(A.nass,"\n\\\\",'</div><div class="abyat-single_bayt">')
    A.nass=string.gsub(A.nass,"\n([^|])",'</div>' .. sdrdiv ..'%1')
    A.nass=string.gsub(A.nass,"\\\\",'</div><div class="abyat-ajz">')
    return header .. ret .. A.nass .. "</div></div>"    
end

return {abyat=abyat}