Jump to content

Module:MultiCountryDisplay

From ICANNWiki

Documentation for this module may be created at Module:MultiCountryDisplay/doc

-- Module:MultiCountryDisplay
-- Handles multiple country entries consistently across templates, centralizing the logic for formatting country lists (as text or bullet points) and extracting normalized country names for category assignment.

local p = {}

-- Dependencies
local countryNormalization = require('Module:CountryNormalization')
local RegionalMappingICANN = require('Module:RegionalMappingICANN')

-- Split semicolon-separated values into an array
local function splitSemicolonValues(value, delimiter)
    delimiter = delimiter or ";%s*"  -- Default delimiter with flexible whitespace
    if not value or value == "" then return {} end
    
    local items = {}
    for item in string.gmatch(value, "[^;]+") do
        local trimmed = item:match("^%s*(.-)%s*$")
        if trimmed and trimmed ~= "" then
            table.insert(items, trimmed)
        end
    end
    return items
end

-- Format a list of countries from a semicolon-separated string
-- Returns either plain text (single country) or bullet points (multiple countries)
-- Each country gets its own region-specific globe emoji based on RegionalMappingICANN lookup
function p.formatCountries(value)
    if not value or value == "" then return "" end
    
    -- Split and normalize countries
    local countries = splitSemicolonValues(value)
    local normalizedCountries = {}
    
    for i, country in ipairs(countries) do
        normalizedCountries[i] = countryNormalization.formatCountry(country)
    end
    
    -- Function to get the appropriate emoji for a specific region
    local function getEmojiForRegion(region)
        if not region then
            return "\240\159\140\141" -- 🌍 Default to Europe-Africa
        end
        
        if region == "NA" or region == "LAC" then
            return "\240\159\140\142" -- 🌎 Americas
        elseif region == "AP" then
            return "\240\159\140\143" -- 🌏 Asia-Australia/Pacific
        else
            return "\240\159\140\141" -- 🌍 Europe-Africa (AF/EUR) and default
        end
    end
    
    -- Generate output based on number of countries
    if #normalizedCountries > 1 then
        local listItems = {}
        
        for _, country in ipairs(normalizedCountries) do
            -- Get the region for this specific country
            local countryRegion = RegionalMappingICANN.getRegion(country)
            local emoji = getEmojiForRegion(countryRegion)
            
            -- Create a list item that uses list-style-type for the emoji
            table.insert(listItems, string.format("<li style=\"list-style-type: '%s '; padding: 2px 0;\">%s</li>", emoji, country))
        end
        
        return string.format("<ul class=\"template-list template-list-country\" style=\"margin:0; padding-left:1em;\">%s</ul>", 
                             table.concat(listItems, ""))
    elseif #normalizedCountries == 1 then
        -- For a single country, create a similar list with just one item
        local countryRegion = RegionalMappingICANN.getRegion(normalizedCountries[1])
        local emoji = getEmojiForRegion(countryRegion)
        
        -- Single item list with the same styling
        return string.format("<ul class=\"template-list template-list-country\" style=\"margin:0; padding-left:1em;\"><li style=\"list-style-type: '%s '; padding: 2px 0;\">%s</li></ul>", 
                             emoji, normalizedCountries[1])
    end
    
    return ""
end

-- Get a list of normalized countries for category assignment
function p.getCountriesForCategories(value)
    if not value or value == "" then return {} end
    
    local countries = splitSemicolonValues(value)
    local normalizedCountries = {}
    
    for i, country in ipairs(countries) do
        normalizedCountries[i] = countryNormalization.formatCountry(country)
    end
    
    return normalizedCountries
end

return p