/* Vito's Wings — shared UI components */

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

// Lookup helper
function t(key, lang) {
  const entry = window.VW_T[key];
  if (!entry) return key;
  return entry[lang] || entry.en || key;
}

// Pretty Lempira formatter
function fmtL(n) {
  return "L " + (Math.round(n)).toLocaleString("en-US");
}

// SAUCE PILL ----------------------------------------------------------------
const HEAT_COLOR = { 1: "#15803D", 2: "#D97706", 3: "#E63426" };

function SaucePill({ sauce, lang, selected, onClick, disabled }) {
  const heatColor = HEAT_COLOR[sauce.heat] || "#0A0A0A";
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      style={{
        appearance: "none",
        border: selected ? `2px solid ${heatColor}` : "1px solid #0A0A0A",
        background: selected ? heatColor : "transparent",
        color: selected ? "#F6F4EF" : "#0A0A0A",
        padding: "10px 14px",
        fontFamily: "'Anton', sans-serif",
        letterSpacing: "0.08em",
        fontSize: 15,
        cursor: disabled && !selected ? "not-allowed" : "pointer",
        opacity: disabled && !selected ? 0.35 : 1,
        display: "inline-flex", alignItems: "center", gap: 10,
        transition: "all 0.15s",
      }}
    >
      <span style={{
        width: 10, height: 10, borderRadius: "50%",
        background: heatColor,
        border: selected ? "2px solid #F6F4EF" : "0",
        flexShrink: 0,
      }} />
      <span>{sauce[lang]}</span>
      {sauce.signature && <span style={{ fontSize: 9, letterSpacing: "0.2em", padding: "2px 6px", border: selected ? "1px solid #F6F4EF" : "1px solid #E63426", color: selected ? "#F6F4EF" : "#E63426", fontFamily: "'DM Sans', sans-serif" }}>{t("signature", lang).toUpperCase()}</span>}
      {sauce.isNew && <span style={{ fontSize: 9, letterSpacing: "0.2em", padding: "2px 6px", background: selected ? "#F6F4EF" : "#0A0A0A", color: selected ? heatColor : "#F6F4EF", fontFamily: "'DM Sans', sans-serif" }}>{t("new_tag", lang).toUpperCase()}</span>}
    </button>
  );
}

// DRINK ART --------------------------------------------------------------
// Stylized product card for drinks — bottle/can/glass silhouette in SVG over a colored block.
function DrinkArt({ item, big }) {
  const Glass = ({ kind }) => {
    const stroke = "rgba(255,255,255,0.85)";
    const fill = "rgba(255,255,255,0.18)";
    const w = 100, h = 120;
    if (kind === "bottle") {
      return (
        <svg viewBox={`0 0 ${w} ${h}`} width="60%" height="80%" style={{ overflow: "visible" }}>
          <path d="M40 8 L60 8 L60 26 Q60 32 64 38 L66 50 L66 108 Q66 116 60 116 L40 116 Q34 116 34 108 L34 50 L36 38 Q40 32 40 26 Z" fill={fill} stroke={stroke} strokeWidth="2" />
          <rect x="36" y="60" width="28" height="22" fill="rgba(255,255,255,0.85)" />
          <rect x="44" y="3" width="12" height="8" fill={stroke} />
        </svg>
      );
    }
    if (kind === "can") {
      return (
        <svg viewBox={`0 0 ${w} ${h}`} width="60%" height="80%">
          <rect x="30" y="14" width="40" height="92" rx="4" fill={fill} stroke={stroke} strokeWidth="2" />
          <rect x="32" y="16" width="36" height="6" fill="rgba(255,255,255,0.25)" />
          <rect x="34" y="46" width="32" height="30" fill="rgba(255,255,255,0.85)" />
          <ellipse cx="50" cy="13" rx="20" ry="3" fill={stroke} />
        </svg>
      );
    }
    if (kind === "cocktail") {
      return (
        <svg viewBox={`0 0 ${w} ${h}`} width="62%" height="82%">
          <path d="M18 22 L82 22 L52 64 Z" fill={fill} stroke={stroke} strokeWidth="2.5" strokeLinejoin="round" />
          <line x1="50" y1="64" x2="50" y2="100" stroke={stroke} strokeWidth="2.5" />
          <ellipse cx="50" cy="104" rx="22" ry="3" fill={stroke} />
          <line x1="60" y1="20" x2="78" y2="6" stroke={stroke} strokeWidth="2.5" strokeLinecap="round" />
          <circle cx="80" cy="6" r="5" fill="rgba(255,255,255,0.85)" />
        </svg>
      );
    }
    if (kind === "wine") {
      return (
        <svg viewBox={`0 0 ${w} ${h}`} width="55%" height="80%">
          <path d="M30 16 Q30 50 50 60 Q70 50 70 16 Z" fill={fill} stroke={stroke} strokeWidth="2.5" />
          <line x1="50" y1="60" x2="50" y2="96" stroke={stroke} strokeWidth="2.5" />
          <ellipse cx="50" cy="100" rx="22" ry="3" fill={stroke} />
        </svg>
      );
    }
    // glass (default — tall)
    return (
      <svg viewBox={`0 0 ${w} ${h}`} width="55%" height="85%">
        <path d="M30 12 L70 12 L65 108 L35 108 Z" fill={fill} stroke={stroke} strokeWidth="2.5" strokeLinejoin="round" />
        <ellipse cx="50" cy="14" rx="20" ry="3" fill={stroke} />
        <path d="M30 12 L70 12 L67 50 L33 50 Z" fill="rgba(255,255,255,0.5)" />
      </svg>
    );
  };

  return (
    <div style={{
      width: "100%", aspectRatio: "4/3",
      background: item.bg,
      border: "1px solid rgba(10,10,10,0.15)",
      position: "relative", overflow: "hidden",
      display: "flex", alignItems: "center", justifyContent: "center",
    }}>
      {/* halftone overlay */}
      <div style={{ position: "absolute", inset: 0, backgroundImage: "radial-gradient(rgba(255,255,255,0.18) 1px, transparent 1.4px)", backgroundSize: "8px 8px", pointerEvents: "none" }} />
      {/* big translucent letter mark in corner */}
      <div style={{
        position: "absolute", top: -8, left: -2,
        fontFamily: "'Anton', sans-serif",
        fontSize: big ? 140 : 110,
        color: "rgba(255,255,255,0.18)",
        lineHeight: 0.8, letterSpacing: "-0.04em",
        pointerEvents: "none",
      }}>{item.glyph || (item.en || "").charAt(0)}</div>
      {/* glass silhouette */}
      <div style={{ display: "flex", alignItems: "center", justifyContent: "center", height: "100%", width: "100%", padding: big ? 24 : 14, position: "relative", zIndex: 1 }}>
        <Glass kind={item.kind || "glass"} />
      </div>
    </div>
  );
}

// ITEM CARD ---------------------------------------------------------------
function ItemCard({ item, lang, onOpen, compact }) {
  const minP = item.sizes ? item.sizes[0].p : item.price;
  return (
    <button
      onClick={onOpen}
      style={{
        appearance: "none",
        border: "1px solid #0A0A0A",
        background: "#F6F4EF",
        textAlign: "left",
        padding: compact ? 16 : 20,
        cursor: "pointer",
        display: "flex",
        flexDirection: "column",
        gap: 8,
        position: "relative",
        transition: "transform 0.15s, box-shadow 0.15s",
        fontFamily: "'DM Sans', sans-serif",
        color: "#0A0A0A",
      }}
      onMouseEnter={e => { e.currentTarget.style.transform = "translate(-3px,-3px)"; e.currentTarget.style.boxShadow = "6px 6px 0 #0A0A0A"; }}
      onMouseLeave={e => { e.currentTarget.style.transform = ""; e.currentTarget.style.boxShadow = ""; }}
    >
      {item.img ? (
        <div style={{
          width: "100%", aspectRatio: "4/3",
          backgroundImage: `url("${item.img}")`,
          backgroundSize: "cover",
          backgroundPosition: "center",
          border: "1px solid rgba(10,10,10,0.15)",
        }} />
      ) : item.bg ? (
        <DrinkArt item={item} />
      ) : (
        <div style={{
          width: "100%", aspectRatio: "4/3",
          background: "repeating-linear-gradient(45deg, #ece6d5 0 10px, #f1ecdd 10px 20px)",
          display: "flex", alignItems: "center", justifyContent: "center",
          border: "1px solid rgba(10,10,10,0.15)",
          fontFamily: "ui-monospace, 'SF Mono', monospace",
          fontSize: 10, letterSpacing: "0.1em", color: "rgba(10,10,10,0.45)",
        }}>
          {`{ ${item.id}.jpg }`}
        </div>
      )}
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: 10 }}>
        <div style={{ fontFamily: "'Anton', sans-serif", fontSize: 22, letterSpacing: "0.02em", lineHeight: 1, flex: 1 }}>{item[lang]}</div>
        <div style={{ fontFamily: "'Anton', sans-serif", fontSize: 20, letterSpacing: "0.02em", color: "#E63426", whiteSpace: "nowrap" }}>
          {item.sizes ? "DESDE " + fmtL(minP) : fmtL(minP)}
        </div>
      </div>
      {item["desc_" + lang] && <div style={{ fontSize: 12, lineHeight: 1.4, color: "rgba(10,10,10,0.7)" }}>{item["desc_" + lang]}</div>}
      {item.saucy && (
        <div style={{ fontSize: 10, letterSpacing: "0.18em", color: "#E63426", fontWeight: 700 }}>
          + {window.VW_SAUCES.length} {lang === "es" ? "SALSAS" : "SAUCES"}
        </div>
      )}
    </button>
  );
}

// MODAL --------------------------------------------------------------------
function Modal({ open, onClose, children, width = 720 }) {
  if (!open) return null;
  return (
    <div
      onClick={onClose}
      style={{
        position: "fixed", inset: 0, zIndex: 200,
        background: "rgba(10,10,10,0.6)",
        display: "flex", alignItems: "center", justifyContent: "center",
        padding: 20, animation: "vwFade 0.2s ease",
      }}
    >
      <div onClick={e => e.stopPropagation()}
        style={{ background: "#F6F4EF", width: "100%", maxWidth: width, maxHeight: "90vh", overflow: "auto", border: "1px solid #0A0A0A", position: "relative", animation: "vwPop 0.22s cubic-bezier(.2,1.3,.4,1)" }}>
        <button onClick={onClose} aria-label="Close" style={{
          position: "sticky", top: 12, marginLeft: "calc(100% - 56px)", zIndex: 1,
          appearance: "none", border: "1px solid #0A0A0A", background: "#F6F4EF",
          width: 40, height: 40, fontSize: 18, cursor: "pointer", fontFamily: "'Anton', sans-serif",
        }}>✕</button>
        {children}
      </div>
    </div>
  );
}

// SECTION HEADER -----------------------------------------------------------
function SectionHeader({ kicker, title, sub, alignment = "left" }) {
  return (
    <div style={{ marginBottom: 32, textAlign: alignment }}>
      {kicker && <div style={{ fontSize: 11, letterSpacing: "0.22em", color: "#E63426", marginBottom: 8, fontWeight: 700 }}>▌ {kicker.toUpperCase()}</div>}
      <h2 style={{ fontFamily: "'Anton', sans-serif", fontSize: "clamp(40px, 6vw, 84px)", lineHeight: 0.88, margin: 0, letterSpacing: "-0.01em" }}>{title}</h2>
      {sub && <p style={{ marginTop: 12, fontSize: 16, lineHeight: 1.5, maxWidth: 640, color: "rgba(10,10,10,0.7)", marginLeft: alignment === "center" ? "auto" : 0, marginRight: alignment === "center" ? "auto" : 0 }}>{sub}</p>}
    </div>
  );
}

// PRIMARY BUTTON -----------------------------------------------------------
function PrimaryBtn({ children, onClick, full, big, ...rest }) {
  return (
    <button onClick={onClick} {...rest} style={{
      appearance: "none", border: 0, background: "#E63426", color: "#F6F4EF",
      fontFamily: "'Anton', sans-serif",
      fontSize: big ? 18 : 15,
      letterSpacing: "0.14em",
      padding: big ? "18px 26px" : "14px 22px",
      cursor: "pointer", width: full ? "100%" : "auto",
      transition: "transform 0.12s, background 0.12s",
      ...(rest.style || {})
    }}
      onMouseDown={e => e.currentTarget.style.transform = "translate(1px,1px)"}
      onMouseUp={e => e.currentTarget.style.transform = ""}
      onMouseLeave={e => e.currentTarget.style.transform = ""}
    >{children}</button>
  );
}

function GhostBtn({ children, onClick, full, dark, ...rest }) {
  return (
    <button onClick={onClick} {...rest} style={{
      appearance: "none",
      border: `1px solid ${dark ? "#F6F4EF" : "#0A0A0A"}`,
      background: "transparent",
      color: dark ? "#F6F4EF" : "#0A0A0A",
      fontFamily: "'Anton', sans-serif",
      fontSize: 14, letterSpacing: "0.14em",
      padding: "13px 20px",
      cursor: "pointer", width: full ? "100%" : "auto",
      transition: "background 0.15s, color 0.15s",
    }}
      onMouseEnter={e => { e.currentTarget.style.background = dark ? "#F6F4EF" : "#0A0A0A"; e.currentTarget.style.color = dark ? "#0A0A0A" : "#F6F4EF"; }}
      onMouseLeave={e => { e.currentTarget.style.background = "transparent"; e.currentTarget.style.color = dark ? "#F6F4EF" : "#0A0A0A"; }}
    >{children}</button>
  );
}

// TICKER -------------------------------------------------------------------
function Ticker({ items, dark }) {
  return (
    <div style={{
      overflow: "hidden",
      background: dark ? "#0A0A0A" : "#F6F4EF",
      color: dark ? "#F6F4EF" : "#0A0A0A",
      borderTop: "1px solid currentColor",
      borderBottom: "1px solid currentColor",
      padding: "14px 0",
      fontFamily: "'Anton', sans-serif",
      letterSpacing: "0.14em",
      fontSize: 16,
      whiteSpace: "nowrap",
      maskImage: "linear-gradient(to right, transparent, black 5%, black 95%, transparent)",
    }}>
      <div style={{ display: "inline-flex", gap: 32, animation: "vwTicker 40s linear infinite", paddingLeft: 0 }}>
        {Array(8).fill(0).map((_, copy) => (
          <span key={copy} style={{ display: "inline-flex", gap: 32 }}>
            {items.map((it, i) => <span key={i}>{it}{i < items.length - 1 ? <span style={{ margin: "0 16px", color: "#E63426" }}>★</span> : null}</span>)}
            <span style={{ margin: "0 16px", color: "#E63426" }}>★</span>
          </span>
        ))}
      </div>
    </div>
  );
}

// Expose
Object.assign(window, { t, fmtL, SaucePill, ItemCard, Modal, SectionHeader, PrimaryBtn, GhostBtn, Ticker, DrinkArt, EmbedMap, HEAT_COLOR });

// EMBED MAP --------------------------------------------------------------
// Google Maps iframe embed — no API key needed. Falls back to a styled link.
function EmbedMap({ query = "Plaza Macuelizo, Tegucigalpa, Honduras", zoom = 16, height = 320, withPinSticker = true }) {
  const src = `https://maps.google.com/maps?q=${encodeURIComponent(query)}&t=&z=${zoom}&ie=UTF8&iwloc=&output=embed`;
  const directionsUrl = `https://www.google.com/maps/dir/?api=1&destination=${encodeURIComponent(query)}`;
  return (
    <div style={{
      position: "relative",
      border: "2px solid #0A0A0A",
      boxShadow: "8px 8px 0 #0A0A0A",
      background: "#F1ECDD",
      width: "100%",
      height,
      overflow: "hidden",
    }}>
      <iframe
        src={src}
        width="100%"
        height="100%"
        style={{ border: 0, display: "block", filter: "contrast(1.05) saturate(0.85)" }}
        loading="lazy"
        referrerPolicy="no-referrer-when-downgrade"
        title="Vito's Wings — Plaza Macuelizo"
      />
      {withPinSticker && (
        <a href={directionsUrl} target="_blank" rel="noopener" style={{
          position: "absolute", top: 12, left: 12,
          background: "#E63426", color: "#F6F4EF",
          padding: "8px 12px",
          fontFamily: "'Anton', sans-serif", fontSize: 13, letterSpacing: "0.14em",
          transform: "rotate(-4deg)",
          boxShadow: "3px 3px 0 #0A0A0A",
          textDecoration: "none",
        }}>📍 VITO&rsquo;S</a>
      )}
      <a href={directionsUrl} target="_blank" rel="noopener" style={{
        position: "absolute", bottom: 10, right: 10,
        background: "#F6F4EF", color: "#0A0A0A",
        padding: "8px 12px",
        fontFamily: "'Anton', sans-serif", fontSize: 12, letterSpacing: "0.16em",
        border: "1px solid #0A0A0A",
        boxShadow: "3px 3px 0 #0A0A0A",
        textDecoration: "none",
      }}>CÓMO LLEGAR →</a>
    </div>
  );
}
