/* ============================================================
   Shared components — icons, pizza illustration, header, cards
   ============================================================ */
const { useState, useEffect, useRef, useLayoutEffect } = React;

/* ---------- Icon set (simple line icons) ---------- */
const PATHS = {
  cart: "M3 4h2l2.4 12.3a1 1 0 0 0 1 .8h9.2a1 1 0 0 0 1-.8L21 8H6.2 M9 21a1 1 0 1 0 0-2 1 1 0 0 0 0 2Zm9 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z",
  user: "M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8Zm-7 8a7 7 0 0 1 14 0",
  search: "M11 19a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm10 2-4.3-4.3",
  chevron: "M9 6l6 6-6 6",
  chevronDown: "M6 9l6 6 6-6",
  plus: "M12 5v14M5 12h14",
  minus: "M5 12h14",
  check: "M5 12l4.5 4.5L19 7",
  moped: "M5 16a3 3 0 1 0 0 0Zm12 0a3 3 0 1 0 0 0ZM5 16h9l-1-7H8 M14 16l3-9h3 M9 9h6",
  store: "M4 9l1.2-4h13.6L20 9M4 9v10h16V9M4 9h16M9 19v-5h6v5",
  clock: "M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Zm0-13v5l3 2",
  pin: "M12 21s7-6.5 7-12a7 7 0 1 0-14 0c0 5.5 7 12 7 12Zm0-9a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z",
  tag: "M3 12V4h8l9 9-8 8-9-9Zm5-4a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z",
  pizza: "M12 3a16 16 0 0 1 9 9l-9 9-9-9a16 16 0 0 1 9-9Zm0 5a1 1 0 1 0 0 2 1 1 0 0 0 0-2Zm-3 5a1 1 0 1 0 0 2 1 1 0 0 0 0-2Zm6 0a1 1 0 1 0 0 2 1 1 0 0 0 0-2Z",
  melt: "M4 11c0-3 3.5-5 8-5s8 2 8 5M4 11h16M4 11l2 7h12l2-7",
  wing: "M7 14c-2 0-4 1.5-4 4 3 0 4-1 4-1M7 14l8-8c2-2 5-1 5-1s1 3-1 5l-8 8M7 14l3 3",
  cup: "M6 7h12l-1 13H7L6 7Zm0 0-1-3h14l-1 3M9 11v6m6-6v6",
  ice: "M8 9a4 4 0 1 1 8 0M7 9h10l-5 12L7 9Z",
  back: "M15 6l-6 6 6 6",
  trash: "M5 7h14M9 7V5h6v2m-7 0 1 13h6l1-13",
  edit: "M4 20h4L18 10l-4-4L4 16v4ZM14 6l4 4",
  card: "M3 7h18v10H3V7Zm0 4h18",
  cash: "M3 6h18v12H3V6Zm9 8a2 2 0 1 0 0-4 2 2 0 0 0 0 4Z",
  star: "M12 4l2.4 5 5.6.8-4 4 1 5.6L12 17l-5 2.4 1-5.6-4-4 5.6-.8L12 4Z",
  bag: "M6 8h12l1 12H5L6 8Zm3 0a3 3 0 0 1 6 0",
  spark: "M12 3v4m0 10v4M3 12h4m10 0h4M6 6l2.5 2.5M15.5 15.5 18 18M18 6l-2.5 2.5M8.5 15.5 6 18",
  leaf: "M5 19c0-8 6-13 14-13 0 8-6 13-14 13Zm0 0c2-5 5-8 9-10",
};

function Icon({ name, size = 22, sw = 1.8, fill = "none", style }) {
  const d = PATHS[name] || "";
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={fill === "solid" ? "currentColor" : "none"}
      stroke={fill === "solid" ? "none" : "currentColor"} strokeWidth={sw}
      strokeLinecap="round" strokeLinejoin="round" style={style} aria-hidden="true">
      <path d={d} />
    </svg>
  );
}

/* ---------- Official Pizza Hut wordmark (inline → themeable via currentColor) ---------- */
const PH_WORDMARK_PATHS = [
  "M114.33,71.71c-7.6-.66-14.6-5.09-19.65-10.64-1.38-1.52-2.48-3.31-4.03-4.68-3.22-2.84-7.62-4.14-11.89-3.81-.81.06-1.91.51-2.59.52-.53,0-.57-.56-.21-.85,6.72-6.87,13.61-13.59,20.21-20.57.17-.21.28-.68.02-.84-.04-.02-.8-.21-.84-.21h-12.81c-.05,0-.18-.18-.17-.27l3.99-12.26c.1-.13.22-.22.37-.27.33-.13,1.51-.19,1.94-.21,9.46-.44,19.26.34,28.75,0,.66-.04.8.39.65.98-1.19,2.34-1.93,5.21-3.16,7.49-.48.88-1.66,1.64-2.45,2.29-6.56,5.36-13.38,10.42-19.97,15.75-.67,1.3.28.93,1.02.91,7.84-.18,14.05,3.97,18.95,9.68,1.25,1.46,2.28,3.09,3.5,4.57,2.62,3.18,6.34,6.49,10.8,5.89.2.04.38.48.38.68-.01.57-1.71,2.02-2.2,2.41-2.45,1.98-5.62,3.24-8.77,3.44h-1.83Z",
  "M216.12,57.91c-.19.18-.5.33-.77.36-1.35.16-3.21-.04-4.64,0-3.81.07-8.65.46-12.38.12-.61-.06-1.05-.26-1.13-.92l8.03-25.64-.05-1.25c-2.13-.03-4.06.29-6.17-.29-5.22-1.45-6.97-5.93-6.66-10.99.02-.37.27-2.28.43-2.48.64-.82.83.98.95,1.33,2.28,6.54,9.82,3.51,14.35,1.58,1.48-.63,1.38-1.36,1.86-2.77,1.22-3.59,2.13-7.54,3.47-11.04.33-.87.58-1.32,1.61-1.41,5.4.26,11.11-.33,16.48,0,.71.04,1.65.08,1.47,1.03-1.38,5.01-3.5,10.04-4.72,15.08-.31,1.28-.71,2.94-.3,4.21.78,2.4,5.1,4.7,7.09,2.47,2.5-6.66,4.24-13.64,6.49-20.4.5-1.5.41-2.27,2.23-2.4,1.16-.08,2.33.11,3.44.12,4.56.05,9.12-.04,13.68-.01.35-.01.6.36.58.69l-16.11,51.54c-.32.81-.72,1.52-1.69,1.53l-17.39-.06c-.7-.23-.71-.7-.59-1.35,1.39-4.69,3.15-9.3,4.5-14,.98-3.41,1.66-7.17-2.72-8.2-2.08-.49-3.45-.44-4.33,1.7-2.03,6.39-3.93,12.84-6.09,19.19-.2.6-.5,1.84-.93,2.25Z",
  "M17.85,4.64c1.84-.29,3.94.08,5.78.09,6.45.01,12.93-.27,19.38,0,6.82.27,14.57,3.1,16.57,10.33,3.79,13.66-9.18,31.34-24.04,27.67-3.39-.84-5.55-3.4-5.3-6.97.02-.32.21-1.58.43-1.72s1.46.38,1.98.37c4.94-.09,8.28-7.89,9.17-11.93.85-3.87,1.62-12.52-4.72-11.49-3.28.53-3.96,3.35-4.91,6.06-4.56,12.88-7.82,26.53-12.24,39.49-.36.99-.49,1.64-1.69,1.75l-17.61-.05c-.35-.16-.63-.48-.65-.87L16.6,5.44c.28-.43.73-.72,1.25-.8Z",
  "M189.12,17.21l.12.37c-3.67,11.05-6.9,22.3-10.62,33.35-.42,1.25-.59,2.58-2.24,2.61-.26,0-.49-.12-.74-.12-3.61,0-7.22.06-10.82-.05-.28-.18-.22-.44-.19-.72.17-1.4,1.35-3.57,1.36-4.88.01-.85-.58-.86-1.12-.36-2.33,2.15-4.17,4.35-7.05,5.97-5.73,3.21-11,.97-12.48-5.39-3.37-14.58,10.01-34.24,26.06-31.51,2.42.41,4.26,1.31,6.26,2.67,1.14.78,1.32,1.35,3.01,1.03,2.33-.44,5.35-2.3,7.74-2.91.24-.06.47-.12.72-.06ZM168.87,19.91c-2.01.41-3.75,4.31-4.51,6.03-1.27,2.86-2.23,5.91-2.84,8.99-.42,2.15-1.46,7.58.28,9.18,1.56,1.45,3.25-1.43,3.88-2.55,2.79-4.97,4.94-11.95,5.25-17.65.08-1.44.23-4.48-2.07-4.01Z",
  "M295.38,17.63l-.05.43c-2.73,7.16-4.98,14.51-7.7,21.67-1.78,4.68-4.09,9.04-8.58,11.64-6.61,3.83-19.85,3.56-22.96-4.76-1.35-3.62.39-8.98,1.48-12.61,1.42-4.75,3.19-10.2,4.98-14.81.38-.97.36-1.48,1.56-1.56,4.39-.31,9.14.24,13.57,0,.34-.03.47.3.48.6.02.57-1.24,3.39-1.52,4.19-1.42,4.11-2.87,8.39-4.12,12.55-.81,2.69-3.2,9.05.43,10.6,2.89,1.23,4.67-1.33,5.52-3.69,1.27-3.52,2.13-8.19,3.05-11.9s1.6-7.68,2.67-11.21c.14-.45.19-.93.67-1.16l10.52.03Z",
  "M117.93,28.7c-.08-.02-.17-.17-.17-.27,1.02-2.75,2.04-5.53,3.15-8.25.45-1.11.67-2.67,2.18-2.56l24.81.06c.29.19.42.47.27.8-1.01,2.29-1.8,4.69-3.01,6.89l-19.81,15.69c-.37,1.27,1.35.78,2.15.86,4.14.39,8.13,2.41,11.14,5.21,4.68,4.34,6.56,10.61,12.92,13.33,2.13.91,4.17,1.09,6.46.92.33.34-.77,1.19-1.03,1.39-6.09,4.99-15.28,2.93-21.42-.85-4.66-2.88-6.94-5.82-10.44-9.79-3.2-3.62-6.93-4.85-11.74-4.63-.76.04-2.56.73-1.72-.75l17.72-17.35v-.45c-.06-.07-.53-.26-.6-.26h-10.87Z",
  "M322.89,9.03l.14.24-2.33,7.47c-.07.63.09.73.67.78,1.91.16,4.17-.22,6.03,0,.18.02.34.05.49.16l.1.31-2.15,6.58c-.49.98-1.32.64-2.21.69-.21.01-.41.1-.64.11-1.3.05-3.1-.19-4.3,0-.44.07-.65.43-.82.8-.61,1.32-1.18,3.47-1.64,4.92-1.01,3.18-3.05,8.95-2.05,12.1.77,2.42,3.11,3.01,5.36,2.28.31-.01-.61,1.66-.68,1.79-3.34,5.98-11.8,8.85-17.77,5.03-8.28-5.3-1.31-18.69.18-25.93.17-.44-.05-1.1-.56-1.1h-3.5c-.11-.3,0-.54.08-.83.59-2.27,1.61-4.47,2.33-6.7.97-.19,2.01-.17,3.01-.33,4.03-.66,7.3-3.08,9.41-6.51.21-.35.84-1.86,1.15-1.86h9.69Z",
  "M54.48,53.43c-.18-.45.06-.91.19-1.37,3.1-11.11,7.36-21.94,10.53-33.03.29-.92.69-1.46,1.72-1.49,5.02-.14,10.14.29,15.18.08.23,0,.46.13.47.38l-11.35,35c-.07.11-.45.43-.53.43h-16.2Z",
  "M75.87.35c9.2-.94,11.42,9.38,3.32,12.89-3.62,1.57-8.98.99-10.67-3.03-2.04-4.83,2.76-9.39,7.35-9.86Z",
];
function PHWordmark({ height = 26, style, title = "Pizza Hut" }) {
  return (
    <svg viewBox="0 0 328 72" height={height} fill="currentColor"
      style={{ display: "block", width: "auto", ...style }} role="img" aria-label={title}>
      {PH_WORDMARK_PATHS.map((d, i) => <path key={i} d={d} />)}
    </svg>
  );
}

/* ---------- Logo mark (simple geometric pizza slice) ---------- */
function LogoMark() {
  return (
    <svg className="logo-mark" viewBox="0 0 40 40" fill="none" aria-hidden="true">
      <path d="M20 3 L36 33 A18 18 0 0 1 4 33 Z" fill="currentColor" opacity="0.14" />
      <path d="M20 3 L36 33 A18 18 0 0 1 4 33 Z" stroke="currentColor" strokeWidth="2.4" strokeLinejoin="round" />
      <circle cx="20" cy="16" r="2.1" fill="currentColor" />
      <circle cx="14.5" cy="25" r="2.1" fill="currentColor" />
      <circle cx="25.5" cy="25" r="2.1" fill="currentColor" />
    </svg>
  );
}

/* ---------- Pizza illustration ---------- */
function Pizza({ kind = "pepperoni", size = "100%", style }) {
  const toppings = (window.PHData.T[kind] || window.PHData.T.pepperoni);
  return (
    <div className="pizza" style={{ width: size, height: size, ...style }}>
      {toppings.map((t, i) => (
        <span key={i} className={"topping " + t.c}
          style={{ left: t.x + "%", top: t.y + "%", width: t.s + "%", height: t.s + "%" }} />
      ))}
    </div>
  );
}

/* media wrapper that shows a real photo, a pizza illustration, or a placeholder */
function FoodPhoto({ item, size }) {
  if (item && item.img) return <img className="photo" src={window.__asset(item.img)} alt={item.name || ""} loading="lazy" />;
  if (item && item.toppings) return <Pizza kind={item.toppings} size={size || "80%"} />;
  return <div className="foodph"><div className="fp-disc" /></div>;
}

function Media({ item, label, pad = "14%" }) {
  return (
    <>
      <FoodPhoto item={item} size="78%" />
      {label && !(item && item.img) && <span className="media-label">{label}</span>}
    </>
  );
}

/* ---------- Stepper ---------- */
function Stepper({ value, onChange, min = 1 }) {
  return (
    <div className="stepper">
      <button onClick={() => onChange(Math.max(min, value - 1))} aria-label="menos"><Icon name="minus" size={16} /></button>
      <span className="qv">{value}</span>
      <button onClick={() => onChange(value + 1)} aria-label="más"><Icon name="plus" size={16} /></button>
    </div>
  );
}

/* ---------- Collapse-on-scroll hook (with hysteresis to avoid flicker) ---------- */
function useNavCollapse() {
  const [collapsed, setCollapsed] = useState(false);
  useEffect(() => {
    let raf = 0;
    const onScroll = () => {
      cancelAnimationFrame(raf);
      raf = requestAnimationFrame(() => {
        const y = window.scrollY || window.pageYOffset || 0;
        // collapse past 72px; only re-expand once back above 24px
        setCollapsed(prev => (prev ? y > 24 : y > 72));
      });
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => { window.removeEventListener("scroll", onScroll); cancelAnimationFrame(raf); };
  }, []);
  return collapsed;
}

/* ---------- Unified nav: full two-row header that collapses to one slim chip bar ---------- */
function NavBar({ mode, setMode, cartCount, go, onCartClick, active, onPick }) {
  const collapsed = useNavCollapse();
  const cats = window.PHData.CATS;
  return (
    <div className={"ph-nav" + (collapsed ? " collapsed" : "")}>
      {/* ===== FULL (top of page) ===== */}
      <div className="nav-full">
        <div className="nav-full-inner">
        <div className="container ph-header-row">
          <a className="logo" onClick={() => go("home")} role="button" aria-label="Pizza Hut — inicio">
            <PHWordmark height={30} />
          </a>
          <button className="mode-chip hide-sm" onClick={() => setMode(mode === "delivery" ? "pickup" : "delivery")}>
            <span className="dot" />
            {mode === "delivery" ? "A domicilio" : "A recoger"} · Plaza Mayor
            <Icon name="chevronDown" size={15} />
          </button>
          <div className="header-spacer" />
          <div className="header-actions">
            <button className="h-btn icon" aria-label="Buscar"><Icon name="search" size={20} /></button>
            <button className="h-btn hide-sm"><Icon name="user" size={19} /> Iniciar sesión</button>
            <button className="h-btn signup hide-sm">Crear cuenta</button>
            <button className="h-btn icon cart-btn" onClick={onCartClick} aria-label="Carrito">
              <Icon name="bag" size={20} />
              {cartCount > 0 && <span className="cart-count">{cartCount}</span>}
            </button>
          </div>
        </div>
        <div className="cat-shelf">
          <div className="container cat-row">
            {cats.map(c => (
              <button key={c.id} className={"cat" + (active === c.id ? " active" : "")} onClick={() => onPick(c.id)}>
                <span className="cat-ico"><Icon name={c.ico} size={17} /></span>
                {c.name}
              </button>
            ))}
          </div>
        </div>
        </div>
      </div>

      {/* ===== MINI (after scroll) ===== */}
      <div className="nav-mini">
        <div className="container nav-mini-row">
          <a className="logo mini-logo" onClick={() => go("home")} role="button" aria-label="Pizza Hut — inicio">
            <PHWordmark height={20} />
          </a>
          <div className="mini-chips">
            {cats.map(c => (
              <button key={c.id} className={"mini-chip" + (active === c.id ? " active" : "")} onClick={() => onPick(c.id)}>
                <span className="mc-ico"><Icon name={c.ico} size={14} /></span>
                {c.name}
              </button>
            ))}
          </div>
          <button className="iconbtn mini-act" aria-label="Buscar"><Icon name="search" size={18} /></button>
          <button className="iconbtn mini-act cart-btn" onClick={onCartClick} aria-label="Carrito">
            <Icon name="bag" size={18} />
            {cartCount > 0 && <span className="cart-count">{cartCount}</span>}
          </button>
        </div>
      </div>
    </div>
  );
}

/* ---------- Product card ---------- */
function ProductCard({ item, onOpen, onAdd }) {
  const cta = item.cta || "Personalizar";
  return (
    <article className="pcard fade-in">
      {item.tags && item.tags[0] && <span className={"tag" + (item.tags[0] === "Novedad" ? "" : item.tags[0] === "Premium" ? " gold" : "")}>{item.tags[0]}</span>}
      <div className="pcard-media" onClick={() => onOpen(item)} role="button">
        <Media item={item} label={"foto · " + item.name.toLowerCase()} />
      </div>
      <div className="pcard-body">
        <h3 className="pcard-title" onClick={() => onOpen(item)} role="button">{item.name}</h3>
        <p className="pcard-sub">{item.sub}</p>
        <div className="pcard-foot">
          <span className="price">
            {item.from && <span className="from">Desde</span>}
            {item.price.toFixed(2).replace(".", ",")} €
          </span>
          <button className="btn btn-cta" style={{ padding: "11px 16px", fontSize: 13.5 }} onClick={() => onOpen(item)}>{cta}</button>
        </div>
      </div>
    </article>
  );
}

/* ---------- Bestseller compact card ---------- */
function BestsellerCard({ item, onAdd, onOpen }) {
  return (
    <article className="bs-card">
      <div className="bs-media" onClick={() => onOpen(item)} role="button"><FoodPhoto item={item} size="86%" /></div>
      <div className="bs-main" onClick={() => onOpen(item)} role="button">
        <div className="bs-title">{item.name}</div>
        <div className="bs-sub">Mediana · Masa Tradicional</div>
        <div className="bs-price">{item.price.toFixed(2).replace(".", ",")} €</div>
      </div>
      <button className="add-btn" onClick={() => onAdd(item)} aria-label="Añadir"><Icon name="plus" size={20} /></button>
    </article>
  );
}

/* ---------- Offer card ---------- */
function OfferCard({ offer, onAdd }) {
  return (
    <article className="offer-card">
      <div className="offer-art">
        <span className="o-kicker">{offer.kicker}</span>
        <span className="o-big">{offer.big}</span>
      </div>
      <div className="offer-info">
        <h3>{offer.title}</h3>
        <p>{offer.desc}</p>
        <div className="offer-foot">
          <span className="price">
            {offer.old && <span className="price-strike">{offer.old.toFixed(2).replace(".", ",")} €</span>}
            {offer.price.toFixed(2).replace(".", ",")} €
          </span>
          <button className="btn btn-primary" style={{ padding: "11px 18px", fontSize: 13.5 }} onClick={() => onAdd(offer)}>Elegir oferta</button>
        </div>
      </div>
    </article>
  );
}

/* ---------- Footer ---------- */
function Footer() {
  const col = (title, links) => (
    <div>
      <h4>{title}</h4>
      {links.map(l => l === "Nuestra Historia"
        ? <a key={l} href="Nuestra Historia.html">{l}</a>
        : <a key={l} role="button">{l}</a>)}
    </div>
  );
  return (
    <footer className="ph-footer">
      <div className="container">
        <div className="footer-grid">
          <div>
            <div className="foot-brand"><PHWordmark height={34} /></div>
            <p className="muted" style={{ fontSize: 14, marginTop: 14, maxWidth: 280 }}>
              Comida de la que estamos orgullosos, servida rápido y siempre con una sonrisa.
            </p>
            <div style={{ display: "flex", gap: 10, marginTop: 18 }}>
              <span className="iconbtn"><Icon name="star" size={18} /></span>
              <span className="iconbtn"><Icon name="spark" size={18} /></span>
            </div>
          </div>
          {col("Nutrición y Calidad", ["Listado de alérgenos", "Sin gluten", "Valores nutricionales", "Comida de calidad", "Masas"])}
          {col("Aviso Legal", ["Aviso Legal", "Política de privacidad", "Política de cookies", "Condiciones promocionales"])}
          {col("Corporativo", ["Nuestra Historia", "Trabaja con nosotros", "Franquíciate"])}
        </div>
        <div className="footer-bottom">
          <span>© 2026 Pizza Hut España · Concepto de rediseño</span>
          <span>Hecho con cariño · No oficial</span>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { Icon, LogoMark, PHWordmark, Pizza, FoodPhoto, Media, Stepper, NavBar, ProductCard, BestsellerCard, OfferCard, Footer });
