<?php

declare(strict_types=1);

/**
 * Build the self-hosted flag subset used by the admin language pickers.
 *
 * The admin used to pull https://cdn.jsdelivr.net/npm/flag-icons/css/flag-icons.min.css
 * on EVERY admin page: a render-blocking third-party stylesheet (extra DNS + TLS, 28 KB,
 * CORS-opaque) covering ~260 countries, for the ~30 flags this product can actually show.
 *
 * The set of codes is DERIVED from Translator::flagCode() rather than written out here,
 * so adding a language to that map and re-running this script is enough — there is no
 * second list to keep in sync. bin/check-flag-icons.php enforces the same derivation at
 * release time.
 *
 * Usage:
 *   php bin/build-flag-icons.php /path/to/extracted/flag-icons-package
 *     (npm pack flag-icons@7.2.3 && tar xzf flag-icons-7.2.3.tgz  ->  ./package)
 */

$root = dirname(__DIR__);
require_once $root . '/src/Core/Translator.php';

$src = $argv[1] ?? '';
if ($src === '' || !is_dir($src . '/flags/1x1')) {
    fwrite(STDERR, "usage: php bin/build-flag-icons.php <extracted flag-icons package dir>\n");
    exit(1);
}

$sets     = require __DIR__ . '/_flag_codes.php';
$required = $sets['required'];
$optional = $sets['optional'];

$outDir  = $root . '/templates/admin/assets/flag-icons';
$flagDir = $outDir . '/flags/1x1';
if (!is_dir($flagDir) && !mkdir($flagDir, 0755, true) && !is_dir($flagDir)) {
    fwrite(STDERR, "cannot create $flagDir\n");
    exit(1);
}

 
foreach (glob($flagDir . '/*.svg') ?: [] as $old) {
    unlink($old);
}

$css = [];
$css[] = "/* Generated by bin/build-flag-icons.php — do not edit by hand.";
$css[] = " * Subset of flag-icons 7.2.3 (MIT), limited to the codes Translator::flagCode() can emit.";
$css[] = " * Only the 1x1 (square) variant is shipped: every call site uses `fi fi-XX fis`. */";
$css[] = '.fi{background-size:contain;background-position:50%;background-repeat:no-repeat;'
    . 'position:relative;display:inline-block;width:1.333333em;line-height:1em}';
$css[] = '.fi:before{content:"\\00a0"}';
$css[] = '.fis{width:1em;background-size:cover}';

$missing = [];
$shipped = [];
foreach (array_merge($required, $optional) as $code) {
    $from = $src . '/flags/1x1/' . $code . '.svg';
    if (!is_file($from)) {
         
         
        if (in_array($code, $required, true)) {
            $missing[] = $code;
        }
        continue;
    }
    copy($from, $flagDir . '/' . $code . '.svg');
    chmod($flagDir . '/' . $code . '.svg', 0644);
    $shipped[] = $code;
}

if ($missing) {
    fwrite(STDERR, "FLAG_BUILD_FAIL: no SVG upstream for required code(s): " . implode(', ', $missing) . "\n");
    exit(1);
}

sort($shipped);
foreach ($shipped as $code) {
    $css[] = '.fi-' . $code . '{background-image:url(flags/1x1/' . $code . '.svg)}';
}

$cssFile = $outDir . '/flag-icons.css';
file_put_contents($cssFile, implode("\n", $css) . "\n");
chmod($cssFile, 0644);

printf(
    "built %s (%d codes, css %d bytes, svg %d bytes)\n",
    $cssFile,
    count($shipped),
    filesize($cssFile),
    array_sum(array_map('filesize', glob($flagDir . '/*.svg') ?: []))
);
