/* ══ OM Group ERP — Analytics · semantic icon system ══════════════════════
   One registry, one renderer. Every Analytics section, tab and card resolves
   its icon from this file — either explicitly by id, or automatically from
   its own live title text, so a section that renames itself (mode switches
   "Customer Intelligence" → "Vendor Intelligence") re-resolves its icon
   without a single hardcoded mapping at the call site.

   Geometry is 24×24, stroke-only, 1.7 stroke width, round caps/joins —
   identical optical weight across the whole module at every size.       */

const OM_ICONS = {
  sales:        { p: ['M3 3v16a2 2 0 002 2h16', 'M7 14.2l3.6-4.6 3 3L21 6', 'M16 6h5v5'] },
  purchase:     { p: ['M3 4h2l2.4 10.2a2 2 0 002 1.6h7.7a2 2 0 001.95-1.55L21 7.6H6'], c: [[10, 19.4, 1.3], [17, 19.4, 1.3]] },
  brain:        { p: ['M12 5a3 3 0 00-5.7 1.3A2.8 2.8 0 004 8.9a2.9 2.9 0 001 2.2 3 3 0 001.7 5.3A3 3 0 0012 15.6z', 'M12 5a3 3 0 015.7 1.3A2.8 2.8 0 0120 8.9a2.9 2.9 0 01-1 2.2 3 3 0 01-1.7 5.3A3 3 0 0112 15.6z', 'M12 15.6V21'] },
  compass:      { c: [[12, 12, 8.8]], p: ['M15.7 8.3l-2.2 5.2-5.2 2.2 2.2-5.2z'] },
  ai:           { c: [[12, 9.8, 5.8]], p: ['M8.4 17.6h7.2l-1.3 2.6H9.7z', 'M9.9 8.7A2.6 2.6 0 0112.4 7', 'M12 1.8v1.6', 'M3.6 5l1.1 1.1', 'M20.4 5l-1.1 1.1'] },
  telescope:    { p: ['M4.4 13.9l9.6-5.4 1.9 3.4-9.6 5.4z', 'M14.7 8.2l3.1-1.7 1.9 3.4-3.1 1.7', 'M8.6 15.4L6.8 21', 'M11.6 16.9L13.4 21'] },
  calculator:   { r: [[5, 2.6, 14, 18.8, 3]], p: ['M8.6 6.6h6.8', 'M9 12h.01', 'M12 12h.01', 'M15 12h.01', 'M9 16.4h.01', 'M12 16.4h.01', 'M15 16.4h.01'] },
  steering:     { c: [[12, 12, 8.8], [12, 12, 2.6]], p: ['M12 3.2v6.2', 'M4.4 15.6l5.4-2.2', 'M19.6 15.6l-5.4-2.2'] },
  nodes:        { c: [[5.6, 7.2, 2.2], [18.4, 6, 2.2], [12, 18.2, 2.2]], p: ['M7.5 8.6l3.3 7.6', 'M16.8 7.8l-3.5 8.4', 'M7.7 6.7l8.6-.4'] },
  shield:       { p: ['M12 2.8l7.5 2.9v5.6c0 4.6-3.1 8.4-7.5 9.9-4.4-1.5-7.5-5.3-7.5-9.9V5.7z', 'M12 8.6v3.9', 'M12 15.8h.01'] },
  trendUp:      { p: ['M3 17.2l5.6-5.6 3.5 3.5L21 6', 'M15 6h6v6'] },
  bulb:         { p: ['M9.2 17.3a6 6 0 115.6 0', 'M9.6 17.4h4.8', 'M10.1 20.6h3.8'] },
  heart:        { p: ['M3 12.4h3.7l2-5.4 3.3 10.8 2.2-5.4H21'] },
  users:        { c: [[9.2, 8, 3.2]], p: ['M3.4 19.6a5.9 5.9 0 0111.7 0', 'M16.4 5.3a3.2 3.2 0 010 5.4', 'M17.7 19.6a5.7 5.7 0 00-2.1-4.1'] },
  layers:       { p: ['M12 3.2l8.5 4.4-8.5 4.4L3.5 7.6z', 'M3.5 12.2l8.5 4.4 8.5-4.4', 'M3.5 16.6l8.5 4.4 8.5-4.4'] },
  compare:      { p: ['M4 8.4h12', 'M13 5.4l3 3-3 3', 'M20 15.6H8', 'M11 12.6l-3 3 3 3'] },
  calendarBars: { r: [[3.2, 4.8, 17.6, 16, 3]], p: ['M3.2 9.6h17.6', 'M8 3.2v3.2', 'M16 3.2v3.2', 'M8 17.6v-3.4', 'M12 17.6v-5.2', 'M16 17.6v-2.2'] },
  wave:         { p: ['M3 13.6c2.6 0 2.6-4.8 5.2-4.8s2.6 4.8 5.2 4.8 2.6-4.8 5.2-4.8', 'M3 18.8c2.6 0 2.6-3 5.2-3s2.6 3 5.2 3 2.6-3 5.2-3'] },
  boxes:        { r: [[3, 3.8, 8, 6.8, 1.6], [13, 3.8, 8, 6.8, 1.6], [8, 13.4, 8, 6.8, 1.6]] },
  recovery:     { p: ['M20.4 11.6a8.4 8.4 0 10-2 6.2', 'M20.6 17.8v-4.2h-4.2', 'M9.6 8.4h4.6', 'M9.6 11.2h4.6', 'M13.4 15.6L9.6 11.2h1.5c1.7 0 2.8-1 2.8-2.4'] },
  fuel:         { p: ['M5 20.6V5.2A2.2 2.2 0 017.2 3h4.6A2.2 2.2 0 0114 5.2v15.4', 'M3.6 20.6h11.8', 'M7.6 8.3h3.8', 'M14 10.4h2.6a2 2 0 012 2v3.7a1.8 1.8 0 003.6 0V9.3l-2.6-2.6'] },
  truck:        { p: ['M3 6.4h10.6v10.2H3z', 'M13.6 9.8h3.6l2.8 3v3.8h-6.4z'], c: [[7, 18.6, 1.9], [17, 18.6, 1.9]] },
  rupee:        { c: [[12, 12, 8.6]], p: ['M9.2 8h5.6', 'M9.2 11h5.6', 'M13.7 16.8L9.2 11h1.7c1.9 0 3-1.1 3-3'] },
  percent:      { p: ['M19 5L5 19'], c: [[7.6, 7.6, 2.5], [16.4, 16.4, 2.5]] },
  factory:      { p: ['M3 20.6V10.2l5.6 3.4v-3.4l5.6 3.4V6.6l6.8 4.1v9.9z', 'M3 20.6h18', 'M7.2 17.4h1.4', 'M12 17.4h1.4', 'M16.8 17.4h1.4'] },
  cash:         { r: [[2.6, 6, 18.8, 12, 2.6]], c: [[12, 12, 2.8]], p: ['M6 9.6v.01', 'M18 14.4v.01'] },
  tag:          { p: ['M11.4 3.2H4v7.4l9.4 9.4a1.6 1.6 0 002.3 0l5.1-5.1a1.6 1.6 0 000-2.3z', 'M7.7 7.7h.01'] },
  cycle:        { p: ['M20.4 12a8.4 8.4 0 11-2.5-6', 'M20.6 3.4v4.4h-4.4'] },
  pen:          { p: ['M16.6 3.9l3.5 3.5', 'M13.9 6.6L4.6 16v3.4H8L17.4 10z', 'M3.6 21h16.8'] },
  doc:          { p: ['M6 2.8h7.6L19 8.2v13H6z', 'M13.6 2.8v5.4H19', 'M9.2 13h6.6', 'M9.2 16.6h6.6'] },
  pattern:      { r: [[4, 4, 6, 6, 1.4], [14, 4, 6, 6, 1.4], [4, 14, 6, 6, 1.4]], c: [[17, 17, 3.2]] },
  building:     { p: ['M4 21V4.6h9.4V21', 'M13.4 21V9.6H20V21', 'M2.8 21h18.4', 'M7 8.2h3.2', 'M7 12.2h3.2', 'M7 16.2h3.2', 'M16 13h1.6', 'M16 17h1.6'] },
  target:       { c: [[12, 12, 8.6], [12, 12, 4.6], [12, 12, 1.1]] },
  scale:        { p: ['M12 3.4v17.2', 'M6.2 20.6h11.6', 'M4 7.6l16-2.2', 'M4 7.6L1.9 13a3 3 0 004.2 0z', 'M20 5.4L17.9 11a3 3 0 004.2 0z'] },
  search:       { c: [[11, 11, 6.6]], p: ['M15.8 15.8L21 21'] },
  warn:         { p: ['M12 3.6l9 15.8H3z', 'M12 9.6v3.9', 'M12 16.8h.01'] },
  bolt:         { p: ['M13.2 2.4L4.6 13.8h6.4l-.4 7.8 8.8-11.4h-6.6z'] },
  ruler:        { p: ['M3.4 15.2L15.2 3.4l5.4 5.4L8.8 20.6z', 'M7.4 9.4l1.8 1.8', 'M10.4 6.4l1.8 1.8', 'M13.4 3.4l1.8 1.8', 'M4.4 12.4l1.8 1.8'] },
  grid:         { r: [[3.4, 3.4, 7, 7, 1.6], [13.6, 3.4, 7, 7, 1.6], [13.6, 13.6, 7, 7, 1.6], [3.4, 13.6, 7, 7, 1.6]] },
};

/* title → icon. Ordered: specific before generic. Nothing at a call site
   needs to know these — a section only knows its own live title.        */
const OM_ICON_RULES = [
  [/annotation|canvas|drawing|measure/, 'ruler'],
  [/pattern|detection/, 'pattern'],
  [/simulat|scenario|what[- ]if/, 'calculator'],
  [/recommend|insight/, 'bulb'],
  [/forecast lab|the models|bucket-by-bucket|projection/, 'telescope'],
  [/forecast|predict|\bai\b/, 'ai'],
  [/driver|root cause|who moved|attribution/, 'steering'],
  [/correlat|matrix|moves with/, 'nodes'],
  [/risk|exposure|threat/, 'shield'],
  [/concentration/, 'target'],
  [/opportunit|upside/, 'trendUp'],
  [/recommend|insight|action/, 'bulb'],
  [/health|score|diagnostic/, 'heart'],
  [/customer|buyer|client/, 'users'],
  [/vendor|supplier/, 'building'],
  [/material|product mix/, 'layers'],
  [/compar|versus|side-by-side/, 'compare'],
  [/season/, 'calendarBars'],
  [/demand/, 'wave'],
  [/inventory|stock|storage/, 'boxes'],
  [/recovery/, 'recovery'],
  [/diesel|fuel/, 'fuel'],
  [/transport|fleet|trip|logistic/, 'truck'],
  [/working capital|cash|receivab|payab|settlement/, 'cash'],
  [/margin/, 'percent'],
  [/profit|earning/, 'rupee'],
  [/operational|operations|crusher|plant/, 'factory'],
  [/pricing|selling rate|rate behaviour|price/, 'tag'],
  [/cycle|billing/, 'cycle'],
  [/brief|board pack|report|statement|summary/, 'doc'],
  [/market/, 'target'],
  [/explore|explorer|search/, 'search'],
  [/alert|warning/, 'warn'],
  [/velocity|momentum/, 'bolt'],
  [/purchase|procure/, 'purchase'],
  [/performance|revenue|sales|growth|trend/, 'sales'],
  [/intelligence|analytics|business/, 'brain'],
  [/studio|analysis/, 'compass'],
];

function omIconId(title) {
  const t = String(title || '').toLowerCase();
  for (let i = 0; i < OM_ICON_RULES.length; i++) if (OM_ICON_RULES[i][0].test(t)) return OM_ICON_RULES[i][1];
  return 'grid';
}

/* ── optical normalisation ────────────────────────────────────────────────
   Hand-drawn paths never share a common ink box: the telescope sits low, the
   users glyph sits left, the bulb is narrow. Rather than nudge 40 icons by
   hand, every glyph is measured ONCE (hidden metrics svg, real getBBox) and
   its ink is re-centred on 12,12 and fitted to one optical box. Stroke width
   is divided by the fit scale so no icon ever reads heavier than its
   neighbour. Any icon added to OM_ICONS later inherits this automatically. */
const OM_NS = 'http://www.w3.org/2000/svg';
const OM_GEOM = {};
const OM_FIT = 19.2;   /* target optical box inside the 24 viewBox */
const OM_SW = 1.7;

function omGeom(id) {
  if (OM_GEOM[id]) return OM_GEOM[id];
  let out = { dx: 0, dy: 0, s: 1 };
  try {
    const g = OM_ICONS[id];
    if (!g || !document.body) return out;
    let host = document.getElementById('om-icon-metrics');
    if (!host) {
      host = document.createElementNS(OM_NS, 'svg');
      host.id = 'om-icon-metrics';
      host.setAttribute('viewBox', '0 0 24 24');
      host.setAttribute('aria-hidden', 'true');
      host.style.cssText = 'position:absolute;left:-9999px;top:0;width:24px;height:24px;visibility:hidden;pointer-events:none';
      document.body.appendChild(host);
    }
    while (host.firstChild) host.removeChild(host.firstChild);
    const el = (t, a) => { const e = document.createElementNS(OM_NS, t); for (const k in a) e.setAttribute(k, a[k]); host.appendChild(e); };
    (g.p || []).forEach(d => el('path', { d: d, fill: 'none', stroke: '#000' }));
    (g.c || []).forEach(c => el('circle', { cx: c[0], cy: c[1], r: c[2], fill: 'none', stroke: '#000' }));
    (g.r || []).forEach(r => el('rect', { x: r[0], y: r[1], width: r[2], height: r[3], rx: r[4] || 0, fill: 'none', stroke: '#000' }));
    (g.l || []).forEach(l => el('line', { x1: l[0], y1: l[1], x2: l[2], y2: l[3], stroke: '#000' }));
    const b = host.getBBox();
    if (b.width > 0 && b.height > 0) {
      /* ink box grown by the stroke that will be painted around it */
      const w = b.width + OM_SW, h = b.height + OM_SW;
      let s = OM_FIT / Math.max(w, h);
      s = Math.max(0.92, Math.min(1.14, s));
      const cx = b.x + b.width / 2, cy = b.y + b.height / 2;
      out = { dx: +(12 - s * cx).toFixed(3), dy: +(12 - s * cy).toFixed(3), s: +s.toFixed(4) };
    }
  } catch (e) { /* geometry unavailable (SSR / detached) — render unshifted */ }
  OM_GEOM[id] = out;
  return out;
}

function OMIcon({ name, from, size, stroke, className, style }) {
  const id = name && OM_ICONS[name] ? name : omIconId(from || name);
  const g = OM_ICONS[id] || OM_ICONS.grid;
  const S = size || 16;
  const m = omGeom(OM_ICONS[id] ? id : 'grid');
  return (
    <svg className={className} style={style} width={S} height={S} viewBox="0 0 24 24" fill="none"
      stroke="currentColor" strokeWidth={(stroke || OM_SW) / m.s} strokeLinecap="round" strokeLinejoin="round"
      shapeRendering="geometricPrecision" aria-hidden="true" focusable="false">
      <g transform={'translate(' + m.dx + ' ' + m.dy + ') scale(' + m.s + ')'}>
        {(g.p || []).map((d, i) => <path key={'p' + i} d={d} />)}
        {(g.c || []).map((c, i) => <circle key={'c' + i} cx={c[0]} cy={c[1]} r={c[2]} />)}
        {(g.r || []).map((r, i) => <rect key={'r' + i} x={r[0]} y={r[1]} width={r[2]} height={r[3]} rx={r[4] || 0} />)}
        {(g.l || []).map((l, i) => <line key={'l' + i} x1={l[0]} y1={l[1]} x2={l[2]} y2={l[3]} />)}
      </g>
    </svg>
  );
}

/* the badge used in every section header — icon in a tinted tile */
function OMSecIcon({ title, name, tone }) {
  return <span className="i-ico" style={tone ? { '--ic': tone } : null}><OMIcon name={name} from={title} size={15} /></span>;
}

Object.assign(window, { OMIcon, OMSecIcon, OM_ICONS, omIconId, omGeom });
