// shared.jsx — primitives: TopBar, Footer, Bilingual text, Numpad, Icons

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

// ---- Tiny line icons ---------------------------------------------------------
const Icon = {
  Arrow: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 12h14M13 5l7 7-7 7"/></svg>,
  ArrowL: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M19 12H5M11 5l-7 7 7 7"/></svg>,
  Check: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M20 6L9 17l-5-5"/></svg>,
  Plus: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" {...p}><path d="M12 5v14M5 12h14"/></svg>,
  Clock: (p) => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...p}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>,
  Drop: (p) => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...p}><path d="M12 3l6 9a6 6 0 11-12 0z"/></svg>,
  Pin: (p) => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...p}><path d="M12 22s7-7.5 7-13a7 7 0 10-14 0c0 5.5 7 13 7 13z"/><circle cx="12" cy="9" r="2.5"/></svg>,
  Spark: (p) => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M5.6 18.4l2.1-2.1M16.3 7.7l2.1-2.1"/></svg>,
  Wifi: (p) => <svg width="14" height="10" viewBox="0 0 14 10" fill="currentColor" {...p}><path d="M7 9.5c.55 0 1-.45 1-1s-.45-1-1-1-1 .45-1 1 .45 1 1 1zM4.4 6.5l-.7-.7a4.6 4.6 0 016.6 0l-.7.7a3.6 3.6 0 00-5.2 0zM2 4.1l-.7-.7a8 8 0 0111.4 0l-.7.7a7 7 0 00-10 0z"/></svg>,
  Battery: (p) => <svg width="22" height="10" viewBox="0 0 22 10" fill="none" {...p}><rect x="0.5" y="0.5" width="18" height="9" rx="2" stroke="currentColor"/><rect x="2" y="2" width="14" height="6" rx="1" fill="currentColor"/><rect x="19.5" y="3.5" width="1.5" height="3" rx="0.5" fill="currentColor"/></svg>,
  Upload: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="4" y="3" width="16" height="18" rx="2"/><path d="M8 12l4-4 4 4M12 8v9"/></svg>,
  Camera: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 7h3l1.4-2h5.2L16 7h3a2 2 0 012 2v8a2 2 0 01-2 2H5a2 2 0 01-2-2V9a2 2 0 012-2z"/><circle cx="12" cy="13" r="3.5"/></svg>,
  Cal: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...p}><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 9h18M8 3v4M16 3v4"/></svg>,
};

// ---- Bilingual text helper ---------------------------------------------------
function Bi({ en, tc, sep = " ", className = "" }) {
  return (
    <span className={"bilingual " + className}>
      <span className="en">{en}</span>
      <span className="tc">{tc}</span>
    </span>
  );
}

// ---- Status bar (iPad) -------------------------------------------------------
function StatusBar() {
  return (
    <div className="status-bar">
      <span>9:41 AM</span>
      <span>Mobile Medical · Mong Kok Branch</span>
      <span className="right">
        <Icon.Wifi /> <Icon.Battery />
      </span>
    </div>
  );
}

// ---- Top bar with brand + bilingual progress --------------------------------
function TopBar({ stepIndex }) {
  return (
    <div className="topbar">
      <div className="brand">
        <div className="brand-mark mm-logo-mark"><img src="assets/mm-logo.png" alt="Mobile Medical"/></div>
        <div className="brand-name">Mobile Medical</div>
        <div className="brand-sub">美邦 · Body Check</div>
      </div>
      <div className="progress">
        {COPY.steps.map((s, i) => (
          <React.Fragment key={i}>
            <div className={"progress-step " + (i === stepIndex ? "active" : i < stepIndex ? "done" : "")}>
              <div className="progress-num">{i < stepIndex ? <Icon.Check/> : (i + 1).toString().padStart(2, "0")}</div>
              <div className="progress-label">{s.en}<span className="tc">{s.tc}</span></div>
            </div>
            {i < COPY.steps.length - 1 && <div className="progress-divider" />}
          </React.Fragment>
        ))}
      </div>
    </div>
  );
}

// ---- Footer with prev/next --------------------------------------------------
function PageFoot({ onBack, onNext, nextLabel, nextLabelTc, backLabel = "Back", backLabelTc = "返回", canNext = true, secondary = null, primaryWide = false }) {
  return (
    <div className="page-foot">
      <div style={{ display: "flex", alignItems: "center", gap: 16 }}>
        {onBack ? (
          <button className="btn btn-ghost" onClick={onBack}>
            <Icon.ArrowL/> {backLabel} · {backLabelTc}
          </button>
        ) : <span />}
        {secondary}
      </div>
      {onNext && (
        <button className={"btn btn-primary btn-lg" + (primaryWide ? "" : "")} onClick={onNext} disabled={!canNext}>
          {nextLabel} · {nextLabelTc} <Icon.Arrow/>
        </button>
      )}
    </div>
  );
}

// ---- Numpad ------------------------------------------------------------------
function Numpad({ onKey, hkidMode = false }) {
  const keys = hkidMode
    ? [["A","B","C"],["D","E","F"],["G","H","I"],["J","K","L"],["1","2","3"],["4","5","6"],["7","8","9"],["⌫","0","✓"]]
    : [["1","2","3"],["4","5","6"],["7","8","9"],["⌫","0","✓"]];
  return (
    <div className="numpad" style={hkidMode ? { gridTemplateColumns: "repeat(3, 1fr)" } : null}>
      {keys.flat().map((k, i) => {
        const isFn = k === "⌫" || k === "✓";
        const isAlpha = /[A-Z]/.test(k);
        return (
          <button
            key={i}
            className={"numpad-key " + (isFn ? (k === "⌫" ? "fn fn-del" : "fn") : "") + (isAlpha ? " alpha" : "")}
            onClick={() => onKey(k)}
          >
            {k === "⌫" ? "DEL" : k === "✓" ? "DONE" : k}
          </button>
        );
      })}
    </div>
  );
}

Object.assign(window, { Icon, Bi, StatusBar, TopBar, PageFoot, Numpad });
