/* Country-aware phone formatting — the demo library.

   PRODUCT RULE (Arbi, 2026-08-16): when a country is selected, the phone input
   formats and validates as that country's numbers do. Ethiopia gives an
   Ethiopian format; switch the country and the format follows.

   WHAT THIS FILE IS: enough of a library for the demo to SHOW the behaviour —
   dial code, digit grouping, example and length per country, and a progressive
   formatter that shapes digits as they are typed. WHAT IT IS NOT: a metadata
   authority. Production (isystem) should use a standard phone library
   (libphonenumber or equivalent), not hand-roll this table — the ticket says
   so. Nothing here validates beyond length and nothing claims carrier-level
   truth.

   Every top-level name is phlib-prefixed: in-browser Babel makes each one a
   window global, and load order silently overwrites duplicates. */

const PHLIB_COUNTRIES = [
  /* iso, name, dial, groups (national digits), min (optional: shortest valid) */
  ["ET", "Ethiopia",       "251", [3, 3, 3]],
  ["BR", "Brazil",         "55",  [2, 5, 4]],
  ["AR", "Argentina",      "54",  [2, 4, 4]],
  ["MX", "Mexico",         "52",  [2, 4, 4]],
  ["CO", "Colombia",       "57",  [3, 3, 4]],
  ["PE", "Peru",           "51",  [3, 3, 3]],
  ["CL", "Chile",          "56",  [1, 4, 4]],
  ["UY", "Uruguay",        "598", [2, 3, 3]],
  ["PY", "Paraguay",       "595", [3, 3, 3]],
  ["BO", "Bolivia",        "591", [1, 3, 4]],
  ["EC", "Ecuador",        "593", [2, 3, 4]],
  ["VE", "Venezuela",      "58",  [3, 3, 4]],
  ["KE", "Kenya",          "254", [3, 3, 3]],
  ["NG", "Nigeria",        "234", [3, 3, 4]],
  ["GH", "Ghana",          "233", [2, 3, 4]],
  ["TZ", "Tanzania",       "255", [3, 3, 3]],
  ["UG", "Uganda",         "256", [3, 3, 3]],
  ["ZA", "South Africa",   "27",  [2, 3, 4]],
  ["IT", "Italy",          "39",  [3, 3, 4]],
  ["ES", "Spain",          "34",  [3, 3, 3]],
  ["PT", "Portugal",       "351", [3, 3, 3]],
  ["FR", "France",         "33",  [1, 2, 2, 2, 2]],
  ["DE", "Germany",        "49",  [3, 4, 4], 10],
  ["GB", "United Kingdom", "44",  [4, 6]],
  ["US", "United States",  "1",   [3, 3, 4]],
];

const phlibCountry = (iso) =>
  PHLIB_COUNTRIES.find(c => c[0] === String(iso || "").toUpperCase()) || null;

/* Digits in, shaped string out — progressively, so it works while typing:
   ET, "911234"  -> "911 234"
   ET, "911234567" -> "911 234 567"
   Extra digits beyond the pattern stay attached to the last group rather than
   vanishing: truncating input inside a formatter is how a digit silently goes
   missing between what was typed and what was sent. Length problems are
   phlibValid's job, visibly. */
const phlibFormat = (iso, raw) => {
  const c = phlibCountry(iso);
  const digits = String(raw || "").replace(/\D/g, "");
  if (!c) return digits;
  const groups = c[3];
  const parts = [];
  let i = 0;
  for (let g = 0; g < groups.length && i < digits.length; g++) {
    const isLast = g === groups.length - 1;
    parts.push(isLast ? digits.slice(i) : digits.slice(i, i + groups[g]));
    i += groups[g];
  }
  return parts.join(" ");
};

const phlibExample = (iso) => {
  const c = phlibCountry(iso);
  if (!c) return "";
  /* A deterministic dummy: first digit 9 (mobile-shaped in most of the table),
     then ascending — recognisably an example, never a real number. */
  let d = "9"; while (d.length < c[3].reduce((a, b) => a + b, 0)) d += String(d.length % 10);
  return `+${c[2]} ${phlibFormat(iso, d)}`;
};

const phlibValid = (iso, raw) => {
  const c = phlibCountry(iso);
  const n = String(raw || "").replace(/\D/g, "").length;
  if (!c) return n >= 6;
  const full = c[3].reduce((a, b) => a + b, 0);
  return n >= (c[4] || full) && n <= full;
};
