/* =========================================================
   Utilitários compartilhados (globais — sem bundler)
   ========================================================= */
const { useState, useEffect, useRef, useCallback, useMemo, useId } = React;

const CFG = window.SITE_CONFIG;

/* Animação de entrada: sobe 24px + fade ao entrar 8% na tela, uma vez (727:135) */
function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (!("IntersectionObserver" in window)) { el.classList.add("is-visible"); return; }
    const io = new IntersectionObserver(
      ([entry]) => { if (entry.isIntersecting) { el.classList.add("is-visible"); io.disconnect(); } },
      { threshold: 0.08 }
    );
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

function Reveal({ as: Tag = "div", className = "", children, ...rest }) {
  const ref = useReveal();
  return <Tag ref={ref} className={`reveal ${className}`} {...rest}>{children}</Tag>;
}

/* Todos os "Entre em contato" rolam até o contato e focam o botão do WhatsApp */
function goToContato(origem) {
  const sec = document.getElementById("contato");
  if (!sec) return;
  sec.scrollIntoView({ behavior: "auto" });
  setTimeout(() => { const n = document.getElementById("contato-whatsapp"); n && n.focus({ preventScroll: true }); }, 600);
}

/* WhatsApp sempre em nova aba (731:258) */
function whatsappUrl(texto) {
  const msg = encodeURIComponent(texto || CFG.whatsapp.mensagemPadrao);
  return `https://wa.me/${CFG.whatsapp.numero}?text=${msg}`;
}
function openWhatsApp(origem, texto) {
  window.open(whatsappUrl(texto), "_blank", "noopener");
}

/* Ícones Tabler (webfont) — usar apenas onde o design tem ícone */
function Icon({ name, size, className = "", ...rest }) {
  return <i className={`ti ti-${name} ${className}`} style={size ? { fontSize: size } : undefined} aria-hidden="true" {...rest} />;
}

Object.assign(window, { useReveal, Reveal, goToContato, whatsappUrl, openWhatsApp, Icon, CFG });
