<?php
// includes/flags.php — Flag helper and country list

/**
 * Extract 2-letter ISO country code from emoji flag, ISO code, or country name
 */
function get_country_code_from_flag(?string $flag): string {
    if (!$flag) return '';
    $flag = trim($flag);

    // If already 2 letters (e.g. "ID", "jp", "kr")
    if (preg_match('/^[a-zA-Z]{2}$/', $flag)) {
        return strtolower($flag);
    }

    // Known common names/slugs
    $nameMap = [
        'indonesia' => 'id', 'japan' => 'jp', 'jepang' => 'jp',
        'korea' => 'kr', 'south korea' => 'kr', 'korea selatan' => 'kr',
        'china' => 'cn', 'tiongkok' => 'cn', 'turkey' => 'tr', 'turki' => 'tr',
        'switzerland' => 'ch', 'swiss' => 'ch', 'france' => 'fr', 'prancis' => 'fr',
        'australia' => 'au', 'maldives' => 'mv', 'maladewa' => 'mv',
        'saudi arabia' => 'sa', 'arab saudi' => 'sa', 'uae' => 'ae', 'dubai' => 'ae',
        'singapore' => 'sg', 'singapura' => 'sg', 'malaysia' => 'my',
        'thailand' => 'th', 'vietnam' => 'vn', 'united kingdom' => 'gb', 'uk' => 'gb',
        'inggris' => 'gb', 'usa' => 'us', 'amerika' => 'us', 'germany' => 'de',
        'jerman' => 'de', 'italy' => 'it', 'italia' => 'it', 'spain' => 'es',
        'spanyol' => 'es', 'netherlands' => 'nl', 'belanda' => 'nl',
        'egypt' => 'eg', 'mesir' => 'eg', 'qatar' => 'qa', 'jordan' => 'jo',
        'yordania' => 'jo', 'hong kong' => 'hk', 'taiwan' => 'tw',
        'philippines' => 'ph', 'filipina' => 'ph', 'new zealand' => 'nz',
        'selandia baru' => 'nz', 'canada' => 'ca', 'kanada' => 'ca',
        'brazil' => 'br', 'brasil' => 'br', 'austria' => 'at', 'greece' => 'gr',
        'yunani' => 'gr', 'portugal' => 'pt', 'russia' => 'ru', 'rusia' => 'ru',
        'india' => 'in', 'morocco' => 'ma', 'maroko' => 'ma', 'south africa' => 'za',
    ];
    $lower = strtolower($flag);
    if (isset($nameMap[$lower])) {
        return $nameMap[$lower];
    }

    // Check for regional indicator pairs (emoji flag like 🇯🇵, 🇮🇩)
    if (function_exists('mb_str_split')) {
        $chars = mb_str_split($flag);
        if (count($chars) >= 2) {
            $code = '';
            foreach ($chars as $ch) {
                $cp = mb_ord($ch, 'UTF-8');
                if ($cp >= 0x1F1E6 && $cp <= 0x1F1FF) {
                    $code .= chr(ord('a') + ($cp - 0x1F1E6));
                }
            }
            if (strlen($code) === 2) {
                return $code;
            }
        }
    }

    return '';
}

/**
 * Render a real, high-resolution flag image across all platforms (including Windows Chrome)
 */
function render_flag(?string $flag, string $extraClass = '', string $style = ''): string {
    if (!$flag) return '';
    $code = get_country_code_from_flag($flag);
    if ($code) {
        $alt = htmlspecialchars($flag);
        $cls = 'flag-img ' . htmlspecialchars($extraClass);
        $defaultStyle = 'width:24px;height:16px;object-fit:cover;border-radius:2.5px;vertical-align:middle;display:inline-block;box-shadow:0 1px 3px rgba(0,0,0,0.18);';
        if ($style) {
            $defaultStyle .= ' ' . $style;
        }
        return '<img src="https://flagcdn.com/w40/' . $code . '.png" srcset="https://flagcdn.com/w80/' . $code . '.png 2x" alt="' . $alt . '" class="' . trim($cls) . '" style="' . $defaultStyle . '" loading="lazy" onerror="this.outerHTML=\'' . addslashes(htmlspecialchars($flag)) . '\'" />';
    }
    return htmlspecialchars($flag);
}

/**
 * Get comprehensive list of popular countries & flags
 */
function get_country_flags_list(): array {
    return [
        ['code' => 'id', 'name' => 'Indonesia', 'flag' => '🇮🇩'],
        ['code' => 'jp', 'name' => 'Jepang (Japan)', 'flag' => '🇯🇵'],
        ['code' => 'kr', 'name' => 'Korea Selatan (South Korea)', 'flag' => '🇰🇷'],
        ['code' => 'cn', 'name' => 'China (Tiongkok)', 'flag' => '🇨🇳'],
        ['code' => 'tr', 'name' => 'Turki (Turkey)', 'flag' => '🇹🇷'],
        ['code' => 'sa', 'name' => 'Arab Saudi (Saudi Arabia)', 'flag' => '🇸🇦'],
        ['code' => 'ae', 'name' => 'Uni Emirat Arab (Dubai / UAE)', 'flag' => '🇦🇪'],
        ['code' => 'sg', 'name' => 'Singapura (Singapore)', 'flag' => '🇸🇬'],
        ['code' => 'my', 'name' => 'Malaysia', 'flag' => '🇲🇾'],
        ['code' => 'th', 'name' => 'Thailand', 'flag' => '🇹🇭'],
        ['code' => 'vn', 'name' => 'Vietnam', 'flag' => '🇻🇳'],
        ['code' => 'ch', 'name' => 'Swiss (Switzerland)', 'flag' => '🇨🇭'],
        ['code' => 'fr', 'name' => 'Prancis (France)', 'flag' => '🇫🇷'],
        ['code' => 'au', 'name' => 'Australia', 'flag' => '🇦🇺'],
        ['code' => 'mv', 'name' => 'Maladewa (Maldives)', 'flag' => '🇲🇻'],
        ['code' => 'gb', 'name' => 'Inggris (United Kingdom)', 'flag' => '🇬🇧'],
        ['code' => 'us', 'name' => 'Amerika Serikat (USA)', 'flag' => '🇺🇸'],
        ['code' => 'it', 'name' => 'Italia (Italy)', 'flag' => '🇮🇹'],
        ['code' => 'de', 'name' => 'Jerman (Germany)', 'flag' => '🇩🇪'],
        ['code' => 'es', 'name' => 'Spanyol (Spain)', 'flag' => '🇪🇸'],
        ['code' => 'nl', 'name' => 'Belanda (Netherlands)', 'flag' => '🇳🇱'],
        ['code' => 'eg', 'name' => 'Mesir (Egypt)', 'flag' => '🇪🇬'],
        ['code' => 'nz', 'name' => 'Selandia Baru (New Zealand)', 'flag' => '🇳🇿'],
        ['code' => 'qa', 'name' => 'Qatar', 'flag' => '🇶🇦'],
        ['code' => 'jo', 'name' => 'Yordania (Jordan)', 'flag' => '🇯🇴'],
        ['code' => 'hk', 'name' => 'Hong Kong', 'flag' => '🇭🇰'],
        ['code' => 'tw', 'name' => 'Taiwan', 'flag' => '🇹🇼'],
        ['code' => 'ph', 'name' => 'Filipina (Philippines)', 'flag' => '🇵🇭'],
        ['code' => 'is', 'name' => 'Islandia (Iceland)', 'flag' => '🇮🇸'],
        ['code' => 'no', 'name' => 'Norwegia (Norway)', 'flag' => '🇳🇴'],
        ['code' => 'ca', 'name' => 'Kanada (Canada)', 'flag' => '🇨🇦'],
        ['code' => 'br', 'name' => 'Brasil (Brazil)', 'flag' => '🇧🇷'],
        ['code' => 'at', 'name' => 'Austria', 'flag' => '🇦🇹'],
        ['code' => 'gr', 'name' => 'Yunani (Greece)', 'flag' => '🇬🇷'],
        ['code' => 'pt', 'name' => 'Portugal', 'flag' => '🇵🇹'],
        ['code' => 'ru', 'name' => 'Rusia (Russia)', 'flag' => '🇷🇺'],
        ['code' => 'in', 'name' => 'India', 'flag' => '🇮🇳'],
        ['code' => 'za', 'name' => 'Afrika Selatan (South Africa)', 'flag' => '🇿🇦'],
        ['code' => 'ma', 'name' => 'Maroko (Morocco)', 'flag' => '🇲🇦'],
    ];
}
