/* core.jsx — shared primitives for RaDa Intelligence (Chemeli daylight). */
const { useState, useEffect, useRef, useMemo, useCallback } = React;

const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
const norm = (v, lo, hi) => clamp((v - lo) / (hi - lo + 1e-9), 0, 1);

const STATUS_COLOR = { red: 'var(--red)', amber: 'var(--amber)', green: 'var(--green)' };
const STATUS_SOFT  = { red: 'var(--red-soft)', amber: 'var(--amber-soft)', green: 'var(--green-soft)' };
const STATUS_INK   = { red: 'var(--red-ink)', amber: 'var(--amber-ink)', green: 'var(--green-deep)' };

/* Material-Symbols-ish line icons, 24px grid, ~1.7px stroke */
const PATHS = {
  pin: 'M12 21s7-6 7-11a7 7 0 10-14 0c0 5 7 11 7 11z M12 11a1.6 1.6 0 100-3.2 1.6 1.6 0 000 3.2',
  leaf: 'M5 21c0-9 7-16 16-16 0 9-7 16-16 16z M5 21c4-7 8-10 13-12',
  drop: 'M12 3s6 6.5 6 11a6 6 0 01-12 0c0-4.5 6-11 6-11z',
  thermo: 'M10 14V5a2 2 0 014 0v9a4 4 0 11-4 0z M12 14V9',
  chart: 'M3 17l5-6 4 3 5-8 4 5 M3 21h18',
  search: 'M11 18a7 7 0 100-14 7 7 0 000 14z M20 21l-4.3-4.3',
  bell: 'M18 8a6 6 0 10-12 0c0 7-3 9-3 9h18s-3-2-3-9 M13.7 21a2 2 0 01-3.4 0',
  chevron: 'M9 6l6 6-6 6',
  chevdown: 'M6 9l6 6 6-6',
  arrowR: 'M5 12h14 M13 6l6 6-6 6',
  calendar: 'M7 3v4 M17 3v4 M3 9h18 M5 5h14a1 1 0 011 1v14a1 1 0 01-1 1H5a1 1 0 01-1-1V6a1 1 0 011-1z',
  person: 'M12 12a4 4 0 100-8 4 4 0 000 8z M4 21c0-4 4-6 8-6s8 2 8 6',
  layers: 'M12 3l9 5-9 5-9-5z M3 13l9 5 9-5',
  check: 'M4 12l5 5 11-11',
  plus: 'M12 5v14 M5 12h14',
  camera: 'M4 8h3l2-2h6l2 2h3a1 1 0 011 1v10a1 1 0 01-1 1H4a1 1 0 01-1-1V9a1 1 0 011-1z M12 17a3.5 3.5 0 100-7 3.5 3.5 0 000 7z',
  route: 'M6 19a2 2 0 100-4 2 2 0 000 4z M18 9a2 2 0 100-4 2 2 0 000 4z M8 17h6a3 3 0 003-3V9 M6 15V7',
  download: 'M12 3v12 M7 11l5 5 5-5 M4 20h16',
  globe: 'M12 21a9 9 0 100-18 9 9 0 000 18z M3 12h18 M12 3c3 3 3 15 0 18 M12 3c-3 3-3 15 0 18',
  clock: 'M12 21a9 9 0 100-18 9 9 0 000 18z M12 7v5l3 2',
  alert: 'M12 3l9 16H3z M12 10v4 M12 17h.01',
  sun: 'M12 17a5 5 0 100-10 5 5 0 000 10z M12 2v2 M12 20v2 M4 12H2 M22 12h-2 M5 5l1.5 1.5 M17.5 17.5L19 19 M5 19l1.5-1.5 M17.5 6.5L19 5',
  rain: 'M7 14a5 5 0 011-9.9A6 6 0 0119 9a4 4 0 01-.5 8H7 M8 20l-1 2 M12 20l-1 2 M16 20l-1 2',
  task: 'M9 11l2 2 4-4 M5 4h14a1 1 0 011 1v14a1 1 0 01-1 1H5a1 1 0 01-1-1V5a1 1 0 011-1z',
  home: 'M3 11l9-7 9 7 M5 10v10h14V10',
  chat: 'M4 5h16a1 1 0 011 1v10a1 1 0 01-1 1H9l-5 4V6a1 1 0 011-1z',
  shield: 'M12 3l8 3v6c0 5-3.5 8-8 9-4.5-1-8-4-8-9V6z M9 12l2 2 4-4',
  doc: 'M6 3h8l4 4v14a1 1 0 01-1 1H6a1 1 0 01-1-1V4a1 1 0 011-1z M14 3v4h4 M8 13h8 M8 17h6',
  grid: 'M4 4h7v7H4z M13 4h7v7h-7z M4 13h7v7H4z M13 13h7v7h-7z',
  phone: 'M5 4h4l1 5-2.5 1.5a11 11 0 005 5L13 13l5 1v4a1 1 0 01-1 1A14 14 0 014 5a1 1 0 011-1z',
  sprout: 'M12 21v-7 M12 14c0-3-2-5-5-5-1 0-2 .3-3 .8 0 3 2 5 5 5 1 0 2-.3 3-.8z M12 12c0-3 2-5 5-5 1 0 2 .3 3 .8 0 3-2 5-5 5-1 0-2-.3-3-.8z',
  flag: 'M5 21V4 M5 4h11l-2 4 2 4H5',
  filter: 'M4 5h16l-6 8v6l-4-2v-4z',
  package: 'M12 3l8 4.5v9L12 21l-8-4.5v-9z M4 7.5l8 4.5 8-4.5 M12 12v9',
  target: 'M12 21a9 9 0 100-18 9 9 0 000 18z M12 16a4 4 0 100-8 4 4 0 000 8z M12 12h.01',
  eye: 'M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12z M12 15a3 3 0 100-6 3 3 0 000 6z',
};
function Icon({ d, size = 16, color = 'currentColor', sw = 1.7, fill = 'none', style }) {
  return React.createElement('svg', { width: size, height: size, viewBox: '0 0 24 24', fill, stroke: color, strokeWidth: sw, strokeLinecap: 'round', strokeLinejoin: 'round', style },
    React.createElement('path', { d: PATHS[d] || d }));
}

function StatusDot({ s, size = 9 }) {
  return React.createElement('span', { className: 'sdot', style: { width: size, height: size, background: STATUS_COLOR[s] } });
}

function StatusChip({ s, label }) {
  return React.createElement('span', { className: 'schip', style: { background: STATUS_SOFT[s], color: STATUS_INK[s] } },
    React.createElement(StatusDot, { s }), label);
}

/* sparkline with cohort baseline + optional area fill */
function Sparkline({ series, baseline, w = 240, h = 64, color = 'var(--green)', baseColor = 'var(--ink-mute)', fill = true, dot = true }) {
  const all = baseline ? series.concat(baseline) : series;
  const lo = Math.min(...all) - 0.04, hi = Math.max(...all) + 0.04;
  const px = (arr) => arr.map((v, i) => [8 + (i / (arr.length - 1)) * (w - 16), h - 8 - norm(v, lo, hi) * (h - 16)]);
  const path = (pts) => pts.map((p, i) => (i ? 'L' : 'M') + p[0].toFixed(1) + ' ' + p[1].toFixed(1)).join(' ');
  const sp = px(series), bp = baseline ? px(baseline) : null;
  const area = `${path(sp)} L ${sp[sp.length - 1][0].toFixed(1)} ${h - 4} L ${sp[0][0].toFixed(1)} ${h - 4} Z`;
  const last = sp[sp.length - 1];
  return React.createElement('svg', { width: '100%', height: h, viewBox: `0 0 ${w} ${h}`, preserveAspectRatio: 'none', style: { display: 'block', overflow: 'visible' } },
    bp && React.createElement('path', { d: path(bp), fill: 'none', stroke: baseColor, strokeWidth: 1.5, strokeDasharray: '3 3', opacity: 0.7 }),
    fill && React.createElement('path', { d: area, fill: color, opacity: 0.1 }),
    React.createElement('path', { d: path(sp), fill: 'none', stroke: color, strokeWidth: 2.4, strokeLinecap: 'round', strokeLinejoin: 'round' }),
    dot && React.createElement('circle', { cx: last[0], cy: last[1], r: 3.5, fill: color, stroke: 'var(--bg-paper)', strokeWidth: 2 }));
}

/* thin progress bar with status fill */
function Bar({ pct, s = 'green', track = 'var(--bg-cream-dim)', h = 6 }) {
  return React.createElement('div', { className: 'bar', style: { height: h, background: track } },
    React.createElement('div', { style: { width: clamp(pct, 0, 100) + '%', height: '100%', background: STATUS_COLOR[s] || s, borderRadius: 99 } }));
}

function Eyebrow({ children, dot, style }) {
  return React.createElement('div', { className: 'eyebrow', style },
    dot && React.createElement('span', { className: 'eb-dot', style: { background: dot === true ? 'var(--orange)' : dot } }),
    children);
}

Object.assign(window, {
  clamp, norm, STATUS_COLOR, STATUS_SOFT, STATUS_INK,
  Icon, StatusDot, StatusChip, Sparkline, Bar, Eyebrow,
  useState, useEffect, useRef, useMemo, useCallback,
});
