// Shared UI primitives: icons, pills, etc.
const { useState, useEffect, useRef, useMemo, useCallback } = React;

const Icon = ({ name, size = 16 }) => {
  const paths = {
    dashboard: <><rect x="3" y="3" width="7" height="9" rx="1"/><rect x="14" y="3" width="7" height="5" rx="1"/><rect x="14" y="12" width="7" height="9" rx="1"/><rect x="3" y="16" width="7" height="5" rx="1"/></>,
    calendar: <><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 9h18M8 3v4M16 3v4"/></>,
    users: <><circle cx="9" cy="8" r="3"/><path d="M3 20c0-3.3 2.7-6 6-6s6 2.7 6 6"/><circle cx="17" cy="9" r="2"/><path d="M21 20c0-2.2-1.8-4-4-4"/></>,
    clipboard: <><rect x="5" y="4" width="14" height="17" rx="2"/><rect x="9" y="2" width="6" height="4" rx="1"/><path d="M9 12h6M9 16h6"/></>,
    workflow: <><rect x="3" y="3" width="6" height="6" rx="1"/><rect x="15" y="3" width="6" height="6" rx="1"/><rect x="9" y="15" width="6" height="6" rx="1"/><path d="M6 9v3h12V9M12 12v3"/></>,
    clock: <><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></>,
    box: <><path d="M3 7l9-4 9 4v10l-9 4-9-4V7z"/><path d="M3 7l9 4 9-4M12 11v10"/></>,
    bell: <><path d="M6 10a6 6 0 1112 0v4l2 3H4l2-3v-4z"/><path d="M10 20a2 2 0 004 0"/></>,
    chart: <><path d="M3 3v18h18"/><path d="M7 14l4-4 4 4 5-6"/></>,
    settings: <><circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M4.2 4.2l2.1 2.1M17.7 17.7l2.1 2.1M2 12h3M19 12h3M4.2 19.8l2.1-2.1M17.7 6.3l2.1-2.1"/></>,
    search: <><circle cx="11" cy="11" r="7"/><path d="M21 21l-5-5"/></>,
    plus: <><path d="M12 5v14M5 12h14"/></>,
    x: <><path d="M6 6l12 12M18 6l6 12"/></>,
    check: <><path d="M4 12l5 5 11-11"/></>,
    chevronRight: <><path d="M9 6l6 6-6 6"/></>,
    chevronLeft: <><path d="M15 6l-6 6 6 6"/></>,
    chevronDown: <><path d="M6 9l6 6 6-6"/></>,
    alert: <><path d="M12 9v4M12 17h.01"/><path d="M10.3 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/></>,
    warn: <><circle cx="12" cy="12" r="9"/><path d="M12 8v4M12 16h.01"/></>,
    drag: <><circle cx="9" cy="6" r="1"/><circle cx="15" cy="6" r="1"/><circle cx="9" cy="12" r="1"/><circle cx="15" cy="12" r="1"/><circle cx="9" cy="18" r="1"/><circle cx="15" cy="18" r="1"/></>,
    filter: <><path d="M3 5h18l-7 9v5l-4 2v-7L3 5z"/></>,
    wand: <><path d="M15 4V2M15 14v-2M8 9h2M20 9h2M17 11l-1.5-1.5M17 7l-1.5 1.5M13 7L11.5 8.5M13 11L11.5 9.5"/><path d="M4 22l9-9"/></>,
    stethoscope: <><path d="M6 3v7a5 5 0 0010 0V3"/><circle cx="18" cy="14" r="3"/><path d="M18 17v4"/></>,
    pill: <><rect x="3" y="9" width="18" height="6" rx="3" transform="rotate(-45 12 12)"/><path d="M8 8l8 8" transform="rotate(-45 12 12)"/></>,
    history: <><path d="M3 12a9 9 0 109-9 9 9 0 00-7 3.6L3 8"/><path d="M3 3v5h5"/><path d="M12 8v5l3 2"/></>,
    phone: <><path d="M5 4h4l2 5-2.5 1.5a11 11 0 005 5L15 13l5 2v4a1 1 0 01-1 1A17 17 0 013 6a1 1 0 011-1h1z"/></>,
    edit: <><path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 013 3L7 19l-4 1 1-4 12.5-12.5z"/></>,
    trash: <><path d="M3 6h18M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2M6 6l1 14a2 2 0 002 2h6a2 2 0 002-2l1-14"/></>,
    arrow: <><path d="M5 12h14M13 5l7 7-7 7"/></>,
    zap: <><path d="M13 2L4 14h7l-1 8 9-12h-7l1-8z"/></>,
    menu: <><path d="M4 6h16M4 12h16M4 18h16"/></>,
    moon: <><path d="M21 12.8A9 9 0 1111.2 3 7 7 0 0021 12.8z"/></>,
  };
  return (
    <svg className="icon" width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      {paths[name]}
    </svg>
  );
};

const Pill = ({ tone = 'default', children }) => (
  <span className={`pill ${tone === 'default' ? '' : tone}`}>{children}</span>
);

const StatusPill = ({ status }) => {
  const map = {
    scheduled: ['accent', 'Scheduled'],
    'in-progress': ['warn', 'In room'],
    completed: ['ok', 'Completed'],
    cancelled: ['', 'Cancelled'],
  };
  const [tone, label] = map[status] || ['', status];
  return <Pill tone={tone}><span className={`status-dot ${status}`} /> {label}</Pill>;
};

const PriorityPill = ({ priority }) => {
  const map = {
    urgent: 'err',
    elevated: 'warn',
    standard: 'accent',
    routine: 'default',
  };
  return <Pill tone={map[priority] || 'default'}>{priority}</Pill>;
};

const Avatar = ({ name, size = 30 }) => {
  const initials = name.split(' ').map(n => n[0]).slice(0, 2).join('');
  const hash = name.split('').reduce((a, c) => a + c.charCodeAt(0), 0);
  const hue = (hash * 17) % 360;
  return (
    <div className="avatar" style={{
      width: size, height: size, fontSize: size * 0.4,
      background: `linear-gradient(135deg, oklch(0.6 0.13 ${hue}), oklch(0.5 0.15 ${(hue + 60) % 360}))`
    }}>{initials}</div>
  );
};

// Simple SVG sparkline
const Spark = ({ data, color = 'var(--accent)', height = 40 }) => {
  const W = 200, H = height;
  const min = Math.min(...data), max = Math.max(...data);
  const range = max - min || 1;
  const step = W / (data.length - 1);
  const path = data.map((v, i) => {
    const x = i * step;
    const y = H - ((v - min) / range) * (H - 6) - 3;
    return `${i === 0 ? 'M' : 'L'}${x.toFixed(1)},${y.toFixed(1)}`;
  }).join(' ');
  const area = path + ` L${W},${H} L0,${H} Z`;
  return (
    <svg className="spark" viewBox={`0 0 ${W} ${H}`} preserveAspectRatio="none">
      <path d={area} fill={color} opacity="0.12" />
      <path d={path} fill="none" stroke={color} strokeWidth="1.5" strokeLinecap="round" />
    </svg>
  );
};

Object.assign(window, { Icon, Pill, StatusPill, PriorityPill, Avatar, Spark });
