// flags.jsx — inline SVG country flags keyed by source ID.
// Tiny (each ~150 bytes) so we don't pull in a flag library.
// Aspect ratio 4:3 except where noted (CH is 1:1, BA/HR rectangles too).
// Exported as a global window.SourceFlag(sourceId) → JSX node.

// Standard 12px-tall pill. Use width:auto so 4:3 vs 3:2 vs 1:1 all sit nicely.

const F = {
  // Austria — red/white/red horizontal
  at: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="Austria">
      <rect width="9" height="6" fill="#fff" />
      <rect width="9" height="2" fill="#ED2939" />
      <rect y="4" width="9" height="2" fill="#ED2939" />
    </svg>
  ),
  // Germany — black/red/gold
  de: (
    <svg viewBox="0 0 5 3" xmlns="http://www.w3.org/2000/svg" aria-label="Germany">
      <rect width="5" height="1" fill="#000" />
      <rect y="1" width="5" height="1" fill="#DD0000" />
      <rect y="2" width="5" height="1" fill="#FFCE00" />
    </svg>
  ),
  // Switzerland — red square w/ white cross (1:1)
  ch: (
    <svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg" aria-label="Switzerland">
      <rect width="32" height="32" fill="#D52B1E" />
      <rect x="13" y="6" width="6" height="20" fill="#fff" />
      <rect x="6" y="13" width="20" height="6" fill="#fff" />
    </svg>
  ),
  // si — served as static SVG from /flags/si.svg (see FILE_FLAGS below).
  // Italy — green/white/red vertical
  it: (
    <svg viewBox="0 0 3 2" xmlns="http://www.w3.org/2000/svg" aria-label="Italy">
      <rect width="1" height="2" fill="#009246" />
      <rect x="1" width="1" height="2" fill="#fff" />
      <rect x="2" width="1" height="2" fill="#CE2B37" />
    </svg>
  ),
  // ba — served as static SVG from /flags/ba.svg (see FILE_FLAGS below).
  // hr — served as static SVG from /flags/hr.svg (see FILE_FLAGS below).
  // Serbia — red/blue/white horizontal (top-down)
  rs: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="Serbia">
      <rect width="9" height="2" fill="#C6363C" />
      <rect y="2" width="9" height="2" fill="#0C4076" />
      <rect y="4" width="9" height="2" fill="#fff" />
    </svg>
  ),
  // EU — blue w/ ring of 12 stars (simplified — center stars only)
  eu: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="European Union">
      <rect width="9" height="6" fill="#003399" />
      <g fill="#FFCC00" transform="translate(4.5,3)">
        <g>
          {Array.from({length: 12}).map((_, i) => {
            const a = (i * 30 - 90) * Math.PI / 180;
            const r = 1.7;
            const x = (Math.cos(a) * r).toFixed(3);
            const y = (Math.sin(a) * r).toFixed(3);
            return <circle key={i} cx={x} cy={y} r="0.3" />;
          })}
        </g>
      </g>
    </svg>
  ),
  // Hungary — red/white/green horizontal
  hu: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="Hungary">
      <rect width="9" height="2" fill="#CE2939" />
      <rect y="2" width="9" height="2" fill="#fff" />
      <rect y="4" width="9" height="2" fill="#477050" />
    </svg>
  ),
  // Poland — white over red
  pl: (
    <svg viewBox="0 0 8 5" xmlns="http://www.w3.org/2000/svg" aria-label="Poland">
      <rect width="8" height="2.5" fill="#fff" />
      <rect y="2.5" width="8" height="2.5" fill="#DC143C" />
    </svg>
  ),
  // France — blue/white/red vertical
  fr: (
    <svg viewBox="0 0 3 2" xmlns="http://www.w3.org/2000/svg" aria-label="France">
      <rect width="1" height="2" fill="#002395" />
      <rect x="1" width="1" height="2" fill="#fff" />
      <rect x="2" width="1" height="2" fill="#ED2939" />
    </svg>
  ),
  // Spain — red/yellow/red (yellow band is doubled height: 1:2:1).
  // Vivid official colours (rojo #C60B1E / gualda #FFC400) — the previous
  // #AA151B brick-red read muddy/brown next to the brighter reds in the grid.
  es: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="Spain">
      <rect width="9" height="6" fill="#C60B1E" />
      <rect y="1.5" width="9" height="3" fill="#FFC400" />
    </svg>
  ),
  // Netherlands — red/white/blue horizontal
  nl: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="Netherlands">
      <rect width="9" height="2" fill="#AE1C28" />
      <rect y="2" width="9" height="2" fill="#fff" />
      <rect y="4" width="9" height="2" fill="#21468B" />
    </svg>
  ),
  // Belgium — black/yellow/red vertical
  be: (
    <svg viewBox="0 0 3 2" xmlns="http://www.w3.org/2000/svg" aria-label="Belgium">
      <rect width="1" height="2" fill="#000" />
      <rect x="1" width="1" height="2" fill="#FAE042" />
      <rect x="2" width="1" height="2" fill="#ED2939" />
    </svg>
  ),
  // Czech Republic — white/red with blue triangle on the hoist
  cz: (
    <svg viewBox="0 0 9 6" xmlns="http://www.w3.org/2000/svg" aria-label="Czech Republic">
      <rect width="9" height="3" fill="#fff" />
      <rect y="3" width="9" height="3" fill="#D7141A" />
      <polygon points="0,0 4.5,3 0,6" fill="#11457E" />
    </svg>
  ),
  // Romania — blue/yellow/red vertical
  ro: (
    <svg viewBox="0 0 3 2" xmlns="http://www.w3.org/2000/svg" aria-label="Romania">
      <rect width="1" height="2" fill="#002B7F" />
      <rect x="1" width="1" height="2" fill="#FCD116" />
      <rect x="2" width="1" height="2" fill="#CE1126" />
    </svg>
  ),
  // Sweden — blue with yellow Nordic cross
  se: (
    <svg viewBox="0 0 16 10" xmlns="http://www.w3.org/2000/svg" aria-label="Sweden">
      <rect width="16" height="10" fill="#006AA7" />
      <rect y="4" width="16" height="2" fill="#FECC00" />
      <rect x="5" width="2" height="10" fill="#FECC00" />
    </svg>
  ),
};

// Source ID → ISO country code (matches SOURCES in webapp/src/constants/sources.ts)
const SOURCE_COUNTRY = {
  1:  'at',  // Willhaben
  2:  'de',  // Mobile.de
  3:  'ba',  // OLX.ba
  4:  'ch',  // Ricardo.ch
  5:  'ch',  // Tutti.ch
  6:  'rs',  // Polovniautomobili (serves ex-YU; flagged as RS, label "ex-YU" can sit alongside)
  7:  'eu',  // AutoScout24.com (multi: DE/AT/BE/ES/FR/IT/LU/NL)
  8:  'it',  // Subito.it
  9:  'si',  // Avto.net
  10: 'hr',  // Njuskalo.hr
  11: 'de',  // Kleinanzeigen
  12: 'ch',  // AutoScout24.ch
  13: 'ch',  // Anibis.ch
  14: 'at',  // Gebrauchtwagen.at
  15: 'hu',  // Hasznaltauto.hu
  16: 'pl',  // Otomoto.pl
  17: 'it',  // Automoto.it
  18: 'fr',  // Leboncoin.fr
  19: 'es',  // Coches.net
  20: 'nl',  // Marktplaats.nl
  21: 'be',  // 2dehands.be
  22: 'cz',  // Sauto.cz
  23: 'ro',  // Autovit.ro
  24: 'nl',  // AutoTrack.nl
  25: 'es',  // motor.es
  26: 'pl',  // Gratka.pl
  27: 'se',  // Blocket.se
};

const COUNTRY_LABEL = {
  at: 'Austria', de: 'Germany', ch: 'Switzerland', si: 'Slovenia',
  it: 'Italy', ba: 'Bosnia & Herzegovina', hr: 'Croatia', rs: 'ex-Yugoslavia',
  eu: 'EU (multi-country)',
  hu: 'Hungary', pl: 'Poland', fr: 'France', es: 'Spain',
  nl: 'Netherlands', be: 'Belgium', cz: 'Czech Republic', ro: 'Romania',
  se: 'Sweden',
};

// Countries whose flags are served as static SVGs from /flags/{cc}.svg
// instead of being drawn inline. These are the three whose hand-drawn
// versions were either wrong (BA triangle reversed) or indistinguishable
// from other countries' flags without their coats of arms (SI vs RU,
// HR vs NL). Loaded via <img> so the browser caches the file once.
const FILE_FLAGS = new Set(['ba', 'si', 'hr']);

// SourceFlag — small chip-sized flag, rendered inline beside a source name.
function SourceFlag({ sourceId, size = 16 }) {
  const cc = SOURCE_COUNTRY[sourceId];
  if (!cc) return null;
  // CH is 1:1, others ~3:2 — width auto keeps the proportions natural.
  const isSquare = cc === 'ch';
  const style = {
    height: size,
    width: isSquare ? size : size * 1.5,
    display: 'inline-block',
    verticalAlign: 'middle',
    borderRadius: 2,
    boxShadow: '0 0 0 1px rgba(255,255,255,0.08)',
    overflow: 'hidden',
    flexShrink: 0,
  };
  const label = COUNTRY_LABEL[cc];

  if (FILE_FLAGS.has(cc)) {
    return (
      <img
        className="src-flag"
        src={`/flags/${cc}.svg`}
        alt={label}
        title={label}
        style={{ ...style, objectFit: 'cover' }}
      />
    );
  }

  const svg = F[cc];
  if (!svg) return null;
  return (
    <span className="src-flag" style={style} title={label} aria-label={label}>
      {svg}
    </span>
  );
}

Object.assign(window, { SourceFlag, SOURCE_COUNTRY, COUNTRY_LABEL });
