// components.jsx — shared primitives

const { useState, useEffect, useRef, useCallback, useMemo } = React;

// ─── Inline icon system (simple stroke icons, MUI-ish outlined) ─────────
const Icon = ({ name, size = 22, stroke = 2, style }) => {
  const common = {
    width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
    stroke: 'currentColor', strokeWidth: stroke, strokeLinecap: 'round',
    strokeLinejoin: 'round', style,
  };
  const paths = {
    home:      <><path d="M3 11l9-8 9 8"/><path d="M5 10v10h14V10"/></>,
    camera:    <><rect x="3" y="7" width="18" height="13" rx="2"/><circle cx="12" cy="13" r="3.5"/><path d="M8 7l2-3h4l2 3"/></>,
    upload:    <><path d="M12 3v13"/><path d="M7 8l5-5 5 5"/><path d="M4 17v3h16v-3"/></>,
    library:   <><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/></>,
    user:      <><circle cx="12" cy="8" r="4"/><path d="M4 21c0-4 4-6 8-6s8 2 8 6"/></>,
    settings:  <><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .4 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.4 1.7 1.7 0 0 0-1 1.5V21a2 2 0 0 1-4 0v-.1a1.7 1.7 0 0 0-1-1.5 1.7 1.7 0 0 0-1.8.4l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .4-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 0 1 0-4h.1a1.7 1.7 0 0 0 1.5-1 1.7 1.7 0 0 0-.4-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.4h.1a1.7 1.7 0 0 0 1-1.5V3a2 2 0 0 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.4l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.4 1.8v.1a1.7 1.7 0 0 0 1.5 1H21a2 2 0 0 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></>,
    close:     <><path d="M18 6L6 18"/><path d="M6 6l12 12"/></>,
    check:     <><path d="M20 6L9 17l-5-5"/></>,
    chevRight: <><path d="M9 6l6 6-6 6"/></>,
    chevLeft:  <><path d="M15 6l-6 6 6 6"/></>,
    chevDown:  <><path d="M6 9l6 6 6-6"/></>,
    sparkles:  <><path d="M12 3l1.5 4.5L18 9l-4.5 1.5L12 15l-1.5-4.5L6 9l4.5-1.5z"/><path d="M19 14l.8 2.2L22 17l-2.2.8L19 20l-.8-2.2L16 17l2.2-.8z"/></>,
    wand:      <><path d="M15 4V2"/><path d="M15 14v2"/><path d="M8 9l-5 5 5 5 11-11 1.5-1.5a2.1 2.1 0 0 0-3-3L8 9z"/><path d="M11 11l3 3"/></>,
    download:  <><path d="M12 3v13"/><path d="M17 11l-5 5-5-5"/><path d="M4 17v3h16v-3"/></>,
    share:     <><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="M8.5 13.5l7 4"/><path d="M15.5 6.5l-7 4"/></>,
    flash:     <><path d="M13 2L4 14h7l-1 8 9-12h-7z"/></>,
    flip:      <><path d="M3 7h13a3 3 0 0 1 3 3v1"/><path d="M6 4L3 7l3 3"/><path d="M21 17H8a3 3 0 0 1-3-3v-1"/><path d="M18 20l3-3-3-3"/></>,
    grid:      <><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></>,
    image:     <><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="9" cy="9" r="1.5"/><path d="M21 15l-5-5-9 9"/></>,
    palette:   <><circle cx="12" cy="12" r="9"/><circle cx="8" cy="9" r="1"/><circle cx="12" cy="7" r="1"/><circle cx="16" cy="9" r="1"/><circle cx="17" cy="13" r="1"/><path d="M12 3a9 9 0 0 0 0 18c1.7 0 2-2 1-3.5s-1-2.5 1-2.5h3a4 4 0 0 0 0-8c-4 0-5-3-5-4z"/></>,
    scan:      <><path d="M3 7V5a2 2 0 0 1 2-2h2"/><path d="M17 3h2a2 2 0 0 1 2 2v2"/><path d="M21 17v2a2 2 0 0 1-2 2h-2"/><path d="M7 21H5a2 2 0 0 1-2-2v-2"/><path d="M7 12h10"/></>,
    zap:       <><path d="M13 2L4 14h7l-1 8 9-12h-7z"/></>,
    phone:     <><path d="M5 4h4l2 5-3 2a11 11 0 0 0 5 5l2-3 5 2v4a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2z"/></>,
    cloud:     <><path d="M17 17a4 4 0 0 0 2-7.5 6 6 0 0 0-11-2A5 5 0 0 0 7 17z"/></>,
    mail:      <><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 7 9-7"/></>,
    help:      <><circle cx="12" cy="12" r="9"/><path d="M9.5 9a2.5 2.5 0 0 1 5 .2c0 1.7-2.5 2-2.5 3.8"/><circle cx="12" cy="17" r="1" fill="currentColor" stroke="none"/></>,
    shield:    <><path d="M12 3l8 3v5c0 5-3.5 8-8 10-4.5-2-8-5-8-10V6z"/></>,
    bell:      <><path d="M6 8a6 6 0 0 1 12 0c0 7 3 7 3 9H3c0-2 3-2 3-9z"/><path d="M10 20a2 2 0 0 0 4 0"/></>,
    heart:     <><path d="M20.8 6a5.5 5.5 0 0 0-9-1.7A5.5 5.5 0 0 0 3 9c0 7 9 12 9 12s9-5 9-12c0-1-.4-2-.8-3z"/></>,
    arrowRight:<><path d="M5 12h14"/><path d="M13 6l6 6-6 6"/></>,
    arrowLeft: <><path d="M19 12H5"/><path d="M11 18l-6-6 6-6"/></>,
    plus:      <><path d="M12 5v14"/><path d="M5 12h14"/></>,
    dots:      <><circle cx="5" cy="12" r="1" fill="currentColor" stroke="none"/><circle cx="12" cy="12" r="1" fill="currentColor" stroke="none"/><circle cx="19" cy="12" r="1" fill="currentColor" stroke="none"/></>,
    refresh:   <><path d="M4 11a8 8 0 0 1 14-4"/><path d="M18 3v5h-5"/><path d="M20 13a8 8 0 0 1-14 4"/><path d="M6 21v-5h5"/></>,
    heartFill: <><path d="M20.8 6a5.5 5.5 0 0 0-9-1.7A5.5 5.5 0 0 0 3 9c0 7 9 12 9 12s9-5 9-12c0-1-.4-2-.8-3z" fill="currentColor"/></>,
    link:      <><path d="M10 13a5 5 0 0 0 7 0l3-3a5 5 0 0 0-7-7l-1 1"/><path d="M14 11a5 5 0 0 0-7 0l-3 3a5 5 0 0 0 7 7l1-1"/></>,
    crop:      <><path d="M6 2v16h16"/><path d="M22 18H8V4"/></>,
    sun:       <><circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="M4.9 4.9l1.4 1.4"/><path d="M17.7 17.7l1.4 1.4"/><path d="M4.9 19.1l1.4-1.4"/><path d="M17.7 6.3l1.4-1.4"/></>,
    eye:       <><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8S1 12 1 12z"/><circle cx="12" cy="12" r="3"/></>,
    photo:     <><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="9" cy="9" r="1.5"/><path d="M21 15l-5-5-9 9"/></>,
  };
  return <svg {...common}>{paths[name] || null}</svg>;
};

// ─── StatusBar (fake phone chrome) ───────────────────────────────────
const StatusBar = ({ dark = false }) => (
  <div className={`statusbar ${dark ? 'on-dark' : ''}`}>
    <span>9:41</span>
    <div className="sb-right">
      {/* signal */}
      <svg width="18" height="12" viewBox="0 0 18 12" fill="currentColor"><rect x="0" y="8" width="3" height="4" rx=".5"/><rect x="5" y="5" width="3" height="7" rx=".5"/><rect x="10" y="2" width="3" height="10" rx=".5"/><rect x="15" y="0" width="3" height="12" rx=".5"/></svg>
      {/* wifi */}
      <svg width="16" height="12" viewBox="0 0 16 12" fill="currentColor"><path d="M8 9.5a1.5 1.5 0 100 3 1.5 1.5 0 000-3z"/><path d="M3 7.5 A7 7 0 0 1 13 7.5" stroke="currentColor" strokeWidth="1.5" fill="none"/><path d="M0 4 A11 11 0 0 1 16 4" stroke="currentColor" strokeWidth="1.5" fill="none"/></svg>
      {/* battery */}
      <svg width="26" height="12" viewBox="0 0 26 12" fill="none"><rect x="0.5" y="0.5" width="22" height="11" rx="2.5" stroke="currentColor"/><rect x="2" y="2" width="18" height="8" rx="1.2" fill="currentColor"/><rect x="23" y="4" width="2" height="4" rx="1" fill="currentColor"/></svg>
    </div>
  </div>
);

// ─── AppBar ──────────────────────────────────────────────────────
const AppBar = ({ title, onBack, right, dark = false }) => (
  <div className="appbar" style={dark ? { color: '#fff' } : undefined}>
    {onBack ? (
      <button className="icon-btn" onClick={onBack} aria-label="Back">
        <Icon name="arrowLeft" size={22} />
      </button>
    ) : (
      <div className="wordmark">
        <div className="mark">O</div>
        {title ?? 'Restore Old Photos'}
      </div>
    )}
    {title && onBack ? (
      <div className="title-display" style={{ fontSize: 18, flex: 1, marginLeft: 8 }}>{title}</div>
    ) : null}
    {right ?? <div style={{ width: 44 }} />}
  </div>
);

// ─── TabBar ──────────────────────────────────────────────────────
const TabBar = ({ current, go }) => {
  const tabs = [
    { id: 'home',    label: 'Home',    icon: 'home' },
    { id: 'gallery', label: 'Library', icon: 'library' },
    { id: 'capture', label: 'Restore', icon: 'camera', primary: true },
    { id: 'account', label: 'Account', icon: 'user' },
  ];
  return (
    <nav className="tabbar" role="tablist">
      {tabs.map(t => (
        <button
          key={t.id}
          className={`tab ${current === t.id ? 'active' : ''}`}
          onClick={() => go(t.id)}
          aria-label={t.label}
          aria-current={current === t.id ? 'page' : undefined}
        >
          <span className="ic"><Icon name={t.icon} size={22} stroke={current === t.id ? 2.2 : 1.7} /></span>
          <span>{t.label}</span>
        </button>
      ))}
    </nav>
  );
};

// ─── Before/After components ─────────────────────────────────────
const CompareSlider = ({ before, after }) => {
  const ref = useRef(null);
  const [pos, setPos] = useState(50);
  const dragging = useRef(false);

  const handleMove = (clientX) => {
    const el = ref.current;
    if (!el) return;
    const rect = el.getBoundingClientRect();
    const pct = Math.max(0, Math.min(100, ((clientX - rect.left) / rect.width) * 100));
    setPos(pct);
  };
  const start = (e) => {
    dragging.current = true;
    const x = e.touches ? e.touches[0].clientX : e.clientX;
    handleMove(x);
  };
  const move = (e) => {
    if (!dragging.current) return;
    const x = e.touches ? e.touches[0].clientX : e.clientX;
    handleMove(x);
  };
  const end = () => { dragging.current = false; };

  useEffect(() => {
    window.addEventListener('mousemove', move);
    window.addEventListener('mouseup', end);
    window.addEventListener('touchmove', move, { passive: true });
    window.addEventListener('touchend', end);
    return () => {
      window.removeEventListener('mousemove', move);
      window.removeEventListener('mouseup', end);
      window.removeEventListener('touchmove', move);
      window.removeEventListener('touchend', end);
    };
  }, []);

  return (
    <div className="compare-slider" ref={ref} onMouseDown={start} onTouchStart={start}>
      <img src={before} alt="Before" />
      <div className="cs-after-wrap" style={{ clipPath: `inset(0 0 0 ${pos}%)` }}>
        <img src={after} alt="After" />
      </div>
      <span className="cs-label before">Before</span>
      <span className="cs-label after">After</span>
      <div className="cs-handle" style={{ left: `${pos}%` }} />
    </div>
  );
};

const CompareSideBySide = ({ before, after }) => (
  <div className="compare-sidebyside">
    <div className="sbs-col before">
      <img src={before} alt="Before" />
      <span className="sbs-tag">Before</span>
    </div>
    <div className="sbs-col after">
      <img src={after} alt="After" />
      <span className="sbs-tag">After</span>
    </div>
  </div>
);

const CompareTapReveal = ({ before, after }) => {
  const [pressed, setPressed] = useState(false);
  const on = () => setPressed(true);
  const off = () => setPressed(false);
  return (
    <div
      className={`compare-tap ${pressed ? 'pressed' : ''}`}
      onMouseDown={on} onMouseUp={off} onMouseLeave={off}
      onTouchStart={on} onTouchEnd={off}
    >
      <img src={after} alt="After" />
      <img src={before} alt="Before" style={{ opacity: pressed ? 1 : 0 }} />
      <span className="ct-tag">{pressed ? 'Before' : 'After'}</span>
      <span className="ct-hint">
        <Icon name="eye" size={14} /> Press and hold to see original
      </span>
    </div>
  );
};

Object.assign(window, {
  Icon, StatusBar, AppBar, TabBar,
  CompareSlider, CompareSideBySide, CompareTapReveal,
});
