/* ============================================================
   3DAX — shared UI primitives & hooks  -> window globals
   ============================================================ */
const { useState, useEffect, useRef, useCallback } = React;

/* money */
function eur(n){ return "€" + n.toFixed(2).replace(".", ","); }

/* reveal-on-scroll hook: attach ref, returns inView bool */
function useReveal(opts){
  const ref = useRef(null);
  const [inView, setInView] = useState(false);
  useEffect(() => {
    const el = ref.current; if(!el) return;
    const io = new IntersectionObserver(
      (entries) => { entries.forEach(e => { if(e.isIntersecting){ setInView(true); io.unobserve(e.target); } }); },
      { threshold: opts?.threshold ?? 0.18, rootMargin: opts?.rootMargin ?? "0px 0px -8% 0px" }
    );
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return [ref, inView];
}

/* Reveal wrapper component */
function Reveal({ children, className="", delay=0, as="div", style }){
  const [ref, inView] = useReveal();
  const Tag = as;
  return (
    <Tag ref={ref} className={className + " reveal" + (inView ? " in" : "")}
         style={{ transitionDelay: (inView ? delay : 0) + "ms", ...style }}>
      {children}
    </Tag>
  );
}

/* star rating */
function Stars({ value=5, size=14, gold="var(--gold)", dim="rgba(251,242,225,.22)" }){
  return (
    <span style={{ display:"inline-flex", gap:2 }} aria-label={value + " de 5"}>
      {[0,1,2,3,4].map(i => (
        <svg key={i} width={size} height={size} viewBox="0 0 24 24"
             fill={i < Math.round(value) ? gold : dim} style={{ display:"block" }}>
          <path d="M12 2.5l2.95 6.18 6.8.77-5.05 4.6 1.36 6.7L12 17.9l-6.06 3.65 1.36-6.7L2.25 9.45l6.8-.77z"/>
        </svg>
      ))}
    </span>
  );
}

/* striped product placeholder (for designs without a render yet) */
function ProductPlaceholder({ label="render en camino", from="#cda14e", to="#822614", ratio=1 }){
  return (
    <div className="prod-ph" style={{
      position:"relative", width:"100%", aspectRatio: String(ratio),
      borderRadius:"var(--r-md)", overflow:"hidden",
      display:"flex", alignItems:"center", justifyContent:"center",
      background:
        "repeating-linear-gradient(135deg, rgba(251,242,225,.04) 0 14px, rgba(251,242,225,.015) 14px 28px)",
      border:"1px solid var(--line)",
    }}>
      <div style={{
        position:"absolute", inset:0, opacity:.16,
        background:`radial-gradient(60% 60% at 50% 40%, ${from}, transparent 70%), radial-gradient(50% 50% at 70% 80%, ${to}, transparent 70%)`,
      }} />
      {/* deckbox glyph */}
      <div style={{ position:"relative", textAlign:"center", padding:24 }}>
        <svg width="74" height="92" viewBox="0 0 74 92" style={{ margin:"0 auto 14px", opacity:.6 }}
             fill="none" stroke="var(--gold)" strokeWidth="1.5">
          <rect x="10" y="26" width="54" height="58" rx="4"/>
          <rect x="8"  y="10" width="58" height="18" rx="3"/>
          <rect x="6"  y="70" width="62" height="16" rx="3"/>
          <line x1="22" y1="42" x2="52" y2="42"/>
          <line x1="22" y1="52" x2="52" y2="52"/>
        </svg>
        <div style={{ fontFamily:"var(--mono)", fontSize:11, letterSpacing:".22em",
                      textTransform:"uppercase", color:"var(--cream-faint)" }}>{label}</div>
      </div>
    </div>
  );
}

/* placeholder slot for future animated assets (360° loops, video) */
function AnimSlot({ label="animación 360° · próximamente", sub="suelta aquí tu vídeo/loop", ratio=1, style }){
  return (
    <div className="anim-slot" style={{ aspectRatio:String(ratio), ...style }}>
      <div className="anim-slot__pulse" />
      <div className="anim-slot__play">
        <svg width="26" height="26" viewBox="0 0 24 24" fill="var(--ink)"><path d="M8 5v14l11-7z"/></svg>
      </div>
      <div className="anim-slot__label">{label}</div>
      <div className="anim-slot__sub">{sub}</div>
      <span className="anim-slot__corner anim-slot__corner--tl" />
      <span className="anim-slot__corner anim-slot__corner--tr" />
      <span className="anim-slot__corner anim-slot__corner--bl" />
      <span className="anim-slot__corner anim-slot__corner--br" />
    </div>
  );
}

// expose React hooks globally so every separately-compiled babel file can use them
Object.assign(window, {
  useState: React.useState, useEffect: React.useEffect,
  useRef: React.useRef, useCallback: React.useCallback, useMemo: React.useMemo,
});
Object.assign(window, { eur, useReveal, Reveal, Stars, ProductPlaceholder, AnimSlot });
