require('strict')

local p = {}

-- Errors
local err_category = {
	['crh'] = '[[Category:crh template errors]]'
}

local function errorMessage(error)
	return '<span class="error" style="font-size:100%">Error in crh template: {{' .. error .. '}}</span>'
	end

-- Helper function to parse boolean parameters
local function isFalse(value)
	if value == "no" or value == "n" or value == "false" or value == "0" or value == "off" or value == "none" then
		return true
	else 
		return false
	end
end

local function parseBoolean(value, default)
    if value == nil then
        return default
    elseif type(value) == "string" then
        value = value:lower()
        if value == "yes" or value == "y" or value == "true" or value == "1" or value == "on" then
            return true
        elseif isFalse(value) then
            return false
        end
    end
    return default
end

-- Helper function to get parameter value, considering aliases
local function getParamValue(params, paramName, aliases)
    local value = params[paramName]
    if value == nil and aliases then
        for _, alias in ipairs(aliases) do
            value = params[alias]
            if value then break end
        end
    end
    return value
end

-- Main function to render text
function p.renderText(frame)
    local args = require ('Module:Arguments').getArgs (frame);
    
    local latinText = args[1]
    local cyrillicText = args[2]
    local arabicText = args[3]
    local literalTranslation = getParamValue(args, "lit", {"literal", "literally"})
    local link = parseBoolean(args["link"], true)
    local lead = parseBoolean(args["lead"], true)
    local label = args["label"]
    local labelOnly = parseBoolean(args["label-only"], true)

    local output = {}
    local parts = {}

    if label then
    	if not isFalse(label) then
        	table.insert(output, label .. ": ")
        else
        end
    elseif lead then
        if link then
            table.insert(output, "[[Crimean Tatar language|Crimean Tatar]]: ")
        else
            table.insert(output, "Crimean Tatar: ")
        end
    end

	-- if label and not isFalse(label) then
 --   	table.insert(parts, label .. ":")
 --   elseif label and isFalse(label) then
 --   	do return end
 --   elseif lead and link then
 --       table.insert(output, "[[Crimean Tatar language|Crimean Tatar]]: ")
 --   elseif lead and not link then
	-- 	table.insert(output, "Crimean Tatar: ")
 --   end

	if latinText then
		local spannedLatin = "<span title=\"Crimean Tatar-language text\" lang=\"crh-Latn\">" .. latinText .. "</span>"
	    if lead and not labelOnly and link then
	        table.insert(parts, "[[Latin alphabet|Latin]]: ''" .. spannedLatin .. "''")
	    elseif lead and not labelOnly and link then
	        table.insert(parts, "Latin: ''" .. spannedLatin .. "''")
	    else
	        table.insert(parts, "''" .. spannedLatin .. "''")
	    end
	end

    if cyrillicText then
    	local spannedCyrillic = "<span title=\"Crimean Tatar-language text\" lang=\"crh-Cyrl\">" .. cyrillicText .. "</span>"
	    if not labelOnly and link then
	        table.insert(parts, "[[Cyrillic script|Cyrillic]]: " .. spannedCyrillic)
	    elseif cyrillicText and not labelOnly and not link then
	        table.insert(parts, "Cyrillic: " .. spannedCyrillic)
	    elseif cyrillicText and labelOnly then
	        table.insert(parts, spannedCyrillic)
	    end
	end
    
    if arabicText then
    	local spannedArabic = "<span title=\"Crimean Tatar-language text\" lang=\"crh-Arab\" dir=\"rtl\">" .. arabicText .. "</span>"

	    if not labelOnly and link then
	        table.insert(parts, "[[Arabic script|Arabic]]: " .. spannedArabic)
	    elseif not labelOnly and not link then
	        table.insert(parts, "Arabic: " .. spannedArabic)
	    elseif labelOnly then
	        table.insert(parts, spannedArabic)
	    end
	end

    if literalTranslation and link then
        table.insert(parts, "[[Literal translation|lit.]] '" .. literalTranslation .. "'")
    elseif literalTranslation and not link then
        table.insert(parts, "<abbr title=\"literal translation\">lit.</abbr> '" .. literalTranslation .. "'")
    end

    table.insert(output, table.concat(parts, ", "))

    return table.concat(output)
end

return p