// @wuys/map · data/iso-codes.js
// Map between ISO 3166-1 numeric and alpha-2 codes. Natural Earth uses numeric IDs
// on geometries; the rest of the app uses lowercase alpha-2.
//
// For Phase A we re-use the runtime data that's already loaded by the legacy
// `world-topo-data.js` (sets window.WUYS_WORLD_TOPO with a `numToIso` map). Phase B
// will bake this into a stand-alone `iso-codes.json`.

/**
 * Get the alpha-2 code (lowercase) for a numeric ISO id.
 * @returns {string | null}
 */
export function numToAlpha2(num, table) {
  if (!table) return null;
  return table.get(String(parseInt(num, 10))) || null;
}

/**
 * Build a Map from a plain object dictionary.
 */
export function tableFromDict(dict) {
  const m = new Map();
  for (const [k, v] of Object.entries(dict || {})) m.set(String(parseInt(k, 10)), v);
  return m;
}
