/* eslint-disable */
/* Shared components: PhoneFrame, Statusbar, Screen, Option, etc. */

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

/* === SVG icons === */
const I = {
  arrow: (p) => (
    <svg width={p.s||16} height={p.s||16} viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 8h10M9 4l4 4-4 4"/>
    </svg>
  ),
  back: (p) => (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M13 8H3M7 4 3 8l4 4"/>
    </svg>
  ),
  check: (p) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="m3 7 3 3 5-6"/>
    </svg>
  ),
  spark: (p) => (
    <svg width={p.s||14} height={p.s||14} viewBox="0 0 14 14" fill="currentColor" {...p}>
      <path d="M7 0l1.5 4.5L13 6 8.5 7.5 7 12 5.5 7.5 1 6l4.5-1.5L7 0z"/>
    </svg>
  ),
  signal: (p) => (
    <svg width="14" height="10" viewBox="0 0 14 10" fill="currentColor" {...p}>
      <rect x="0" y="6" width="2" height="4" rx="0.5"/>
      <rect x="4" y="4" width="2" height="6" rx="0.5"/>
      <rect x="8" y="2" width="2" height="8" rx="0.5"/>
      <rect x="12" y="0" width="2" height="10" rx="0.5"/>
    </svg>
  ),
  battery: (p) => (
    <svg width="22" height="10" viewBox="0 0 22 10" fill="none" stroke="currentColor" strokeWidth="1" {...p}>
      <rect x="0.5" y="0.5" width="18" height="9" rx="2"/>
      <rect x="2" y="2" width="14" height="6" rx="1" fill="currentColor" stroke="none"/>
      <rect x="20" y="3" width="1.5" height="4" rx="0.5" fill="currentColor"/>
    </svg>
  ),
  wifi: (p) => (
    <svg width="14" height="10" viewBox="0 0 14 10" fill="currentColor" {...p}>
      <path d="M7 9l1.5-1.5a2 2 0 0 0-3 0L7 9z"/>
      <path d="M3.5 5.5a5 5 0 0 1 7 0l-1 1a3.5 3.5 0 0 0-5 0l-1-1z"/>
      <path d="M0.5 2.5a9 9 0 0 1 13 0l-1 1a7.5 7.5 0 0 0-11 0l-1-1z"/>
    </svg>
  ),
};

/* === Brand mark — original, hex with slash === */
function Mark({ size = 28, color = "var(--primary)" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" fill="none">
      <path d="M16 2L28 9v14l-12 7L4 23V9l12-7z" stroke={color} strokeWidth="2" fill="none"/>
      <path d="M11 16h10M14 13l4 6" stroke={color} strokeWidth="2" strokeLinecap="round"/>
    </svg>
  );
}

function Statusbar({ time = "9:41" }) {
  return (
    <div className="statusbar">
      <span>{time}</span>
      <span className="statusbar__icons">
        <I.signal/>
        <I.wifi/>
        <I.battery/>
      </span>
    </div>
  );
}

function PhoneFrame({ children, label }) {
  return (
    <div className="phone">
      <div className="phone__notch"/>
      {label && (
        <div className="phone__chrome">
          <span>iPhone 15 Pro</span>
          <span style={{opacity:0.4}}>·</span>
          <b>{label}</b>
        </div>
      )}
      <div className="phone__screen">
        {children}
      </div>
    </div>
  );
}

/* Progress dots */
function Progress({ total, current }) {
  const dots = [];
  for (let i = 0; i < total; i++) {
    let cls = "progress__dot";
    if (i < current) cls += " is-done";
    if (i === current) cls += " is-active";
    dots.push(<span key={i} className={cls}/>);
  }
  return <div className="progress">{dots}</div>;
}

/* Option pill */
function Option({ option, selected, multi, onClick }) {
  return (
    <button
      className="opt"
      data-selected={selected ? "true" : "false"}
      data-multi={multi ? "true" : "false"}
      onClick={onClick}
      type="button"
    >
      <span className="opt__check">
        {multi && selected && <I.check stroke="var(--paper)"/>}
      </span>
      <span style={{flex:1}}>
        <span className="opt__text">{option.label}</span>
        {option.hint && <span className="opt__hint">{option.hint}</span>}
      </span>
    </button>
  );
}

/* Toast — slim ribbon, fades fast, never blocks content */
function Toast({ children, onDone }) {
  useEffect(() => {
    const t = setTimeout(onDone, 1600);
    return () => clearTimeout(t);
  }, [onDone]);
  return <div className="toast"><I.spark s={11}/>{children}</div>;
}

/* Compliance footer (small) — concept demo framing throughout */
function Compliance() {
  const showDemo = (e, label) => {
    e.preventDefault();
    window.dispatchEvent(new CustomEvent("demo-cta", { detail: { label, deeplink: "concept demo · not a live offer" }}));
  };
  return (
    <div className="compliance">
      Concept demo · Illustrative only · Not a Chase offer · <a href="#" onClick={(e) => showDemo(e, "Official rules")}>Official rules</a><br/>
      © 2026 Ape Beverages Inc. · Demo not affiliated with JPMorgan Chase · <a href="#" onClick={(e) => showDemo(e, "Privacy notice")}>Privacy</a> · <a href="#" onClick={(e) => showDemo(e, "Terms")}>Terms</a>
    </div>
  );
}

/* Disclaimer block — small print on regulated screens */
function Disclaimer({ label = "Disclosure", children }) {
  return (
    <div className="disclaimer">
      <div className="disclaimer__label">{label}</div>
      {children}
    </div>
  );
}

/* Demo CTA modal — fired by celebration buttons via custom event */
function DemoCTAModal() {
  const [data, setData] = useState(null);
  useEffect(() => {
    const handler = (e) => setData(e.detail);
    window.addEventListener("demo-cta", handler);
    return () => window.removeEventListener("demo-cta", handler);
  }, []);
  if (!data) return null;
  return (
    <div className="cta-modal-bg" onClick={() => setData(null)}>
      <div className="cta-modal" onClick={(e) => e.stopPropagation()}>
        <div className="cta-modal__eyebrow">Concept Demo · Tap simulated</div>
        <h3 className="cta-modal__title">{data.label}</h3>
        <p className="cta-modal__body">In live deployment this would deeplink to:</p>
        <div className="cta-modal__deeplink">{data.deeplink || "chase.com/[matched-product]"}</div>
        <p className="cta-modal__body" style={{fontSize:12, color:"var(--ink-3)", marginBottom:18}}>No actual transaction, account opening, or Chase API call has been initiated.</p>
        <button className="cta-modal__close" onClick={() => setData(null)}>Got it · close</button>
      </div>
    </div>
  );
}

Object.assign(window, {
  I, Mark, Statusbar, PhoneFrame, Progress, Option, Toast, Compliance, Disclaimer, DemoCTAModal,
});
