/* icons.jsx — iconos de línea, placeholder de imagen y utilidades compartidas */
const { useState, useEffect, useRef } = React;

const ICONS = {
  arrow:   "M5 12h14M13 6l6 6-6 6",
  check:   "M5 12.5l4.5 4.5L19 7",
  clock:   "M12 7v5l3.5 2M12 21a9 9 0 100-18 9 9 0 000 18z",
  shield:  "M12 3l7 3v5c0 4.5-3 7.5-7 9-4-1.5-7-4.5-7-9V6l7-3z|M9 12l2 2 4-4",
  sparkle: "M12 3l1.8 5.2L19 10l-5.2 1.8L12 17l-1.8-5.2L5 10l5.2-1.8L12 3z",
  route:   "M6 19a2 2 0 100-4 2 2 0 000 4zM18 9a2 2 0 100-4 2 2 0 000 4z|M8 17h6a3 3 0 003-3V9M6 15V8a3 3 0 013-3h2",
  star:    "M12 3.5l2.6 5.3 5.9.9-4.3 4.1 1 5.8-5.2-2.7-5.2 2.7 1-5.8L3.5 9.7l5.9-.9L12 3.5z",
  bell:    "M6 9a6 6 0 1112 0c0 5 2.5 6 2.5 6h-17S6 14 6 9zM10 20a2 2 0 004 0",
  pin:     "M12 21s7-5.2 7-11a7 7 0 10-14 0c0 5.8 7 11 7 11z|M12 13a2.5 2.5 0 100-5 2.5 2.5 0 000 5z",
  chef:    "M7 21h10M7 17h10v0a5 5 0 01-1-9.9A4 4 0 0112 3a4 4 0 014 4.1A5 5 0 0117 17z",
  truck:   "M3 7h11v8H3zM14 10h4l3 3v2h-7zM7 18.5a1.5 1.5 0 100-3 1.5 1.5 0 000 3zM17.5 18.5a1.5 1.5 0 100-3 1.5 1.5 0 000 3z",
  pkg:     "M3 8l9-5 9 5v8l-9 5-9-5V8z|M3 8l9 5 9-5M12 13v8",
  flag:    "M5 21V4M5 4h11l-2 3 2 3H5",
  phone:   "M5 4h4l2 5-2.5 1.5a11 11 0 005 5L15 13l5 2v4a2 2 0 01-2 2A16 16 0 013 6a2 2 0 012-2z",
  zap:     "M13 2L4 14h7l-1 8 9-12h-7l1-8z",
  award:   "M12 15a5 5 0 100-10 5 5 0 000 10zM8.5 13.5L7 22l5-3 5 3-1.5-8.5",
  heart:   "M12 20s-7-4.4-7-9.5A4 4 0 0112 6a4 4 0 017 4.5C19 15.6 12 20 12 20z",
  plus:    "M12 6v12M6 12h12",
  camera:  "M4 9a2 2 0 012-2h1.6l1.2-1.8a1 1 0 01.84-.45h4.72a1 1 0 01.84.45L16.4 7H18a2 2 0 012 2v8a2 2 0 01-2 2H6a2 2 0 01-2-2z|M12 16.5a3 3 0 100-6 3 3 0 000 6z",
  minus:   "M6 12h12",
  chart:   "M4 20V4M4 20h16M8 16v-5M12 16V8M16 16v-8",
  headset: "M4 13v-1a8 8 0 0116 0v1M4 13a2 2 0 012 2v2a2 2 0 01-4 0v-2a2 2 0 012-2zM20 13a2 2 0 012 2v2a2 2 0 01-2 2h-1a3 3 0 01-3 3h-2",
  thermo:  "M14 14.8V5a2 2 0 10-4 0v9.8a4 4 0 104 0z",
  gauge:   "M12 14l4-4M5.5 18a9 9 0 1113 0",
  map:     "M9 5L3 7v12l6-2 6 2 6-2V5l-6 2-6-2zM9 5v12M15 7v12",
  user:    "M12 12a4 4 0 100-8 4 4 0 000 8zM4 21a8 8 0 0116 0",
  building:"M4 21V5a2 2 0 012-2h8a2 2 0 012 2v16M9 8h.01M13 8h.01M9 12h.01M13 12h.01M9 16h.01M13 16h.01M18 21V9h2v12",
  home:    "M4 11l8-7 8 7M6 9.5V20h12V9.5M10 20v-5h4v5",
  search:  "M10.5 4a6.5 6.5 0 100 13 6.5 6.5 0 000-13zM20 20l-4.8-4.8",
  logout:  "M9 5H6a2 2 0 00-2 2v10a2 2 0 002 2h3M16 17l5-5-5-5M21 12H9",
  eye:     "M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7-10-7-10-7z|M12 15a3 3 0 100-6 3 3 0 000 6z",
  eyeoff:  "M3 3l18 18M10.6 10.6A3 3 0 0014 14M6.7 6.7A17 17 0 002 12s3.6 7 10 7a9.7 9.7 0 004.3-1M9.9 5.2A9.7 9.7 0 0112 5c6.4 0 10 7 10 7a17 17 0 01-2.2 3",
  grid:    "M4 4h7v7H4zM13 4h7v7h-7zM4 13h7v7H4zM13 13h7v7h-7z",
  cog:     "M12 15a3 3 0 100-6 3 3 0 000 6z|M19.4 13a7.6 7.6 0 000-2l1.9-1.5-1.9-3.3-2.3.9a7.6 7.6 0 00-1.7-1L15 3.5h-3.8L10.9 6a7.6 7.6 0 00-1.7 1l-2.3-.9-1.9 3.3L6.9 11a7.6 7.6 0 000 2l-1.9 1.5 1.9 3.3 2.3-.9a7.6 7.6 0 001.7 1l.3 2.5H15l.3-2.5a7.6 7.6 0 001.7-1l2.3.9 1.9-3.3z",
  bolt:    "M13 2L4 14h7l-1 8 9-12h-7l1-8z",
  bell2:   "M6 9a6 6 0 1112 0c0 5 2.5 6 2.5 6h-17S6 14 6 9zM10 20a2 2 0 004 0",
  lock:    "M5 11h14v10H5z|M8 11V7a4 4 0 018 0v4|M12 15v2.5",
  lockopen:"M5 11h14v10H5z|M8 11V7a4 4 0 017.5-2|M12 15v2.5",
  layers:  "M12 3l9 5-9 5-9-5 9-5zM3 13l9 5 9-5M3 17l9 5 9-5",
  sun:     "M12 17a5 5 0 100-10 5 5 0 000 10z|M12 2v2M12 20v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M2 12h2M20 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4",
  moon:    "M21 12.8A9 9 0 1111.2 3a7 7 0 009.8 9.8z",
  mail:    "M3 6.5h18v11H3z|M3 7.5l9 6 9-6",
};

function Icon({ name, style }) {
  const d = (ICONS[name] || "").split("|");
  return (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9"
         strokeLinecap="round" strokeLinejoin="round"
         style={{ width: 20, height: 20, flex: 'none', display: 'block', ...style }}>
      {d.map((p, i) => <path key={i} d={p} />)}
    </svg>
  );
}

function Star({ filled = true }) {
  return (
    <svg viewBox="0 0 24 24" fill={filled ? "currentColor" : "none"} stroke="currentColor" strokeWidth="1.5">
      <path d="M12 3.5l2.6 5.3 5.9.9-4.3 4.1 1 5.8-5.2-2.7-5.2 2.7 1-5.8L3.5 9.7l5.9-.9L12 3.5z" />
    </svg>
  );
}

/* Placeholder de imagen rayado con etiqueta mono */
function Placeholder({ label, dark, style, radius }) {
  return (
    <div className={"ph-img" + (dark ? " dark" : "")} style={{ borderRadius: radius, ...style }}>
      <span className="ph-lbl">{label}</span>
    </div>
  );
}

/* Avatar de monograma — iniciales sobre fondo tintado de marca.
   shape: "circle" | "tile". watermark dibuja la campana de fondo. */
const AV_SETS = [
  ["#2a211c", "#3a2e26", "#2DD4BF"],
  ["#0D9488", "#0F766E", "#ffffff"],
  ["#115E59", "#134E4A", "#ffffff"],
  ["#4a3f37", "#5e5046", "#F7F1E9"],
];
function Avatar({ name = "", shape = "circle", size, watermark }) {
  /* si el nombre tiene foto registrada (repartidores con foto subida a
     Supabase Storage), se muestra la foto en lugar de las iniciales;
     window.TREEGO_FOTOS se llena al cargar los repartidores */
  const foto = (window.TREEGO_FOTOS || {})[name];
  const initials = name.split(/\s+/).filter(Boolean).slice(0, 2).map((w) => w[0]).join("").toUpperCase();
  const idx = [...name].reduce((a, c) => a + c.charCodeAt(0), 0) % AV_SETS.length;
  const [c1, c2, fg] = AV_SETS[idx];
  return (
    <div style={{
      width: size || "100%", height: size || "100%",
      borderRadius: shape === "circle" ? "50%" : "inherit",
      background: foto ? "var(--bg-2)" : `linear-gradient(140deg, ${c1}, ${c2})`,
      display: "grid", placeItems: "center", position: "relative", overflow: "hidden", color: fg,
    }}>
      {foto ? (
        <img loading="lazy" src={foto} alt={name} style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }} />
      ) : (
        <React.Fragment>
          {watermark && (
            <Icon name="bell" style={{ position: "absolute", width: "62%", height: "62%", right: "-12%", bottom: "-14%", opacity: 0.1, color: fg, strokeWidth: 1.4 }} />
          )}
          <span style={{ fontFamily: "var(--font-display)", fontWeight: 600, fontSize: size ? size * 0.4 : "clamp(28px,5vw,46px)", letterSpacing: "-.02em", lineHeight: 1, zIndex: 1 }}>{initials}</span>
        </React.Fragment>
      )}
    </div>
  );
}

/* Reveal en scroll: añade .in cuando entra al viewport */
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll(".reveal:not(.in)");
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) { e.target.classList.add("in"); io.unobserve(e.target); }
      });
    }, { threshold: 0.12, rootMargin: "0px 0px -8% 0px" });
    els.forEach((el) => io.observe(el));
    return () => io.disconnect();
  });
}

Object.assign(window, { Icon, Star, Placeholder, Avatar, useReveal });
