/* ============================================================
   UI primitives — Lavender Eventos
   ============================================================ */
const { useState, useEffect, useRef } = React;

const WA = (msg) =>
  `https://wa.me/${LV.contact.whatsapp}?text=${encodeURIComponent(msg || "Olá! Tenho interesse na Chácara Lavender Eventos.")}`;

/* ---- Rastreamento de conversão (Google Ads / GA4) ----
   Não faz nada até você colar os IDs no data.js (analytics) e o gtag no <head>.
   Seguro: se o gtag não existir, apenas ignora.                                 */
function track(name) {
  try {
    window.dataLayer = window.dataLayer || [];
    const conv = (LV.analytics && LV.analytics.conversions) || {};
    if (typeof window.gtag === "function") {
      window.gtag("event", name + "_click", { event_category: "cta" });
      if (conv[name]) window.gtag("event", "conversion", { send_to: conv[name] });
    } else {
      window.dataLayer.push({ event: name + "_click" });
    }
  } catch (e) {}
}

/* ---- Reveal on scroll ---- */
function Reveal({ children, className = "", delay = 0, as = "div" }) {
  const ref = useRef(null);
  const [vis, setVis] = useState(false);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    // already in view on mount? reveal right away
    const r = el.getBoundingClientRect();
    if (r.top < window.innerHeight * 0.92) { setVis(true); return; }
    const io = new IntersectionObserver(
      ([e]) => { if (e.isIntersecting) { setVis(true); io.disconnect(); } },
      { threshold: 0.12 }
    );
    io.observe(el);
    // safety: never let content stay hidden
    const t = setTimeout(() => setVis(true), 1600);
    return () => { io.disconnect(); clearTimeout(t); };
  }, []);
  const Tag = as;
  return (
    <Tag
      ref={ref}
      className={className + " reveal" + (vis ? " reveal-in" : "")}
      style={{ animationDelay: delay + "ms" }}
    >
      {children}
    </Tag>
  );
}

/* ---- Eyebrow label ---- */
function Eyebrow({ children, light = false }) {
  return (
    <span
      className={
        "inline-flex items-center gap-2 text-[12px] tracking-[0.32em] uppercase font-medium " +
        (light ? "text-cream/70" : "text-forest/70")
      }
    >
      <span className={"inline-block w-6 h-px " + (light ? "bg-gold/70" : "bg-gold")}></span>
      {children}
    </span>
  );
}

/* ---- Buttons ---- */
function Btn({ href, children, variant = "solid", onClick, target }) {
  const base =
    "group inline-flex items-center justify-center gap-2.5 px-7 py-3.5 text-[14px] tracking-wide font-medium rounded-full transition-all duration-300 whitespace-nowrap";
  const styles = {
    solid: "bg-forest text-cream hover:bg-forest-deep shadow-sm hover:shadow-md",
    gold: "bg-gold text-forest-deep hover:brightness-105 shadow-sm",
    outline: "border border-forest/30 text-forest hover:border-forest hover:bg-forest hover:text-cream",
    ghostLight: "border border-cream/35 text-cream hover:bg-cream hover:text-forest-deep",
    wa: "bg-[#1f8a4c] text-white hover:bg-[#19733f] shadow-sm",
  };
  return (
    <a href={href} target={target} rel={target ? "noopener noreferrer" : undefined} onClick={onClick} className={base + " " + styles[variant]}>
      {children}
      <span className="transition-transform duration-300 group-hover:translate-x-1">→</span>
    </a>
  );
}

/* ---- Icons (stroke, 1.4) ---- */
const Ic = {
  wa: (c = "currentColor") => (
    <svg viewBox="0 0 24 24" fill={c} width="18" height="18"><path d="M.06 24l1.69-6.16a11.87 11.87 0 01-1.6-5.95C.16 5.32 5.5 0 12.06 0a11.8 11.8 0 018.41 3.49 11.8 11.8 0 013.48 8.41c0 6.55-5.33 11.89-11.89 11.89a11.9 11.9 0 01-5.69-1.45L.06 24zM6.6 20.13l.36.21a9.86 9.86 0 005.03 1.38 9.88 9.88 0 009.88-9.87c0-2.64-1.03-5.12-2.9-6.99a9.82 9.82 0 00-6.98-2.9 9.88 9.88 0 00-9.88 9.87c0 1.94.57 3.84 1.64 5.46l.24.36-1 3.65 3.65-.96zm11.39-5.55c-.07-.12-.26-.19-.55-.33-.29-.15-1.7-.84-1.96-.94-.26-.1-.46-.14-.65.14-.19.29-.74.94-.91 1.13-.17.19-.34.22-.62.07-.29-.14-1.22-.45-2.32-1.43-.86-.77-1.44-1.71-1.6-2-.17-.29-.02-.44.13-.59.13-.13.29-.34.43-.51.15-.17.19-.29.29-.48.1-.19.05-.36-.02-.51-.07-.14-.65-1.57-.89-2.15-.24-.56-.47-.48-.65-.49l-.55-.01c-.19 0-.5.07-.77.36s-1 .98-1 2.39 1.03 2.78 1.17 2.96c.14.19 2.02 3.09 4.9 4.33.68.3 1.22.47 1.64.6.69.22 1.31.19 1.81.11.55-.08 1.7-.69 1.94-1.37.24-.67.24-1.24.17-1.36z"/></svg>
  ),
  insta: (c = "currentColor") => (
    <svg viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5" width="18" height="18"><rect x="3" y="3" width="18" height="18" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="1" fill={c} stroke="none"/></svg>
  ),
  airbnb: (c = "currentColor") => (
    <svg viewBox="0 0 24 24" fill={c} width="18" height="18"><path d="M12 1.5c-1.4 0-2.5 1-3.6 3.2C6.2 9 3.3 15 2.6 17.2c-.6 1.9.4 3.8 2.3 4.2 1.4.3 2.9-.4 4.2-1.9.9.9 1.9 1.4 2.9 1.4s2-.5 2.9-1.4c1.3 1.5 2.8 2.2 4.2 1.9 1.9-.4 2.9-2.3 2.3-4.2-.7-2.2-3.6-8.2-5.8-12.5C14.5 2.5 13.4 1.5 12 1.5zm0 2c.5 0 1.1.5 1.8 1.9 2.1 4.1 5 10.1 5.6 12.1.3.9-.1 1.7-1 1.9-.8.2-1.8-.5-2.7-1.8 1.3-1.7 2.2-3.6 2.2-5 0-2.8-2.3-5.1-5.1-5.1s-5.1 2.3-5.1 5.1c0 1.4.9 3.3 2.2 5-.9 1.3-1.9 2-2.7 1.8-.9-.2-1.3-1-1-1.9.6-2 3.5-8 5.6-12.1C10.9 4 11.5 3.5 12 3.5zm0 6c1.7 0 3.1 1.4 3.1 3.1 0 .9-.6 2.3-1.7 3.8-.5-.7-1-1.5-1.4-2.2-.4.7-.9 1.5-1.4 2.2-1.1-1.5-1.7-2.9-1.7-3.8 0-1.7 1.4-3.1 3.1-3.1z"/></svg>
  ),
  pin: (c = "currentColor") => (
    <svg viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5" width="18" height="18"><path d="M12 21s7-5.2 7-11a7 7 0 10-14 0c0 5.8 7 11 7 11z"/><circle cx="12" cy="10" r="2.5"/></svg>
  ),
  sun: (c = "currentColor") => (
    <svg viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.4" width="26" height="26"><circle cx="12" cy="12" r="4"/><path d="M12 2v2.5M12 19.5V22M4.2 4.2l1.8 1.8M18 18l1.8 1.8M2 12h2.5M19.5 12H22M4.2 19.8L6 18M18 6l1.8-1.8"/></svg>
  ),
  bulb: (c = "currentColor") => (
    <svg viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.4" width="26" height="26"><path d="M9 18h6M10 21h4M12 3a6 6 0 00-3.5 10.9c.5.4.8.9.9 1.5l.1.6h5l.1-.6c.1-.6.4-1.1.9-1.5A6 6 0 0012 3z"/></svg>
  ),
};

Object.assign(window, { WA, track, Reveal, Eyebrow, Btn, Ic });
