const { useState, useEffect, useRef } = React;

const Arrow = ({ className = "" }) => (
  <svg className={`arrow ${className}`} width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
    <path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
  </svg>
);

const Sticker = ({ kind, className = "", style }) => {
  const glyphs = {
    star: (
      <svg viewBox="0 0 40 40" fill="currentColor"><path d="M20 2 L24 16 L38 20 L24 24 L20 38 L16 24 L2 20 L16 16 Z"/></svg>
    ),
    sparkle: (
      <svg viewBox="0 0 40 40" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round">
        <path d="M20 4 L20 16 M20 24 L20 36 M4 20 L16 20 M24 20 L36 20"/>
      </svg>
    ),
    wave: (
      <svg viewBox="0 0 60 20" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round">
        <path d="M2 10 Q 10 2, 18 10 T 34 10 T 58 10"/>
      </svg>
    ),
    dot: (
      <svg viewBox="0 0 40 40" fill="currentColor"><circle cx="20" cy="20" r="10"/></svg>
    ),
    squig: (
      <svg viewBox="0 0 60 60" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round">
        <path d="M5 30 C 15 10, 25 50, 35 30 S 55 10, 55 30"/>
      </svg>
    ),
    burst: (
      <svg viewBox="0 0 40 40" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round">
        <path d="M20 4 L20 12 M20 28 L20 36 M4 20 L12 20 M28 20 L36 20 M8 8 L13 13 M27 27 L32 32 M32 8 L27 13 M13 27 L8 32"/>
      </svg>
    ),
    flower: (
      <svg viewBox="0 0 40 40" fill="currentColor">
        <circle cx="20" cy="8"  r="6"/>
        <circle cx="20" cy="32" r="6"/>
        <circle cx="8"  cy="20" r="6"/>
        <circle cx="32" cy="20" r="6"/>
        <circle cx="20" cy="20" r="5" fill="white"/>
      </svg>
    ),
    plus: (
      <svg viewBox="0 0 40 40" fill="none" stroke="currentColor" strokeWidth="5" strokeLinecap="round">
        <path d="M20 6 L20 34 M6 20 L34 20"/>
      </svg>
    ),
    heart: (
      <svg viewBox="0 0 40 40" fill="currentColor"><path d="M20 36 C 8 28, 2 20, 2 12 C 2 6, 7 2, 12 2 C 16 2, 18 4, 20 8 C 22 4, 24 2, 28 2 C 33 2, 38 6, 38 12 C 38 20, 32 28, 20 36 Z"/></svg>
    ),
  };
  return (
    <span className={`sticker ${kind} ${className}`} style={style} aria-hidden="true">
      {glyphs[kind] || glyphs.star}
    </span>
  );
};

function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const check = () => {
      const r = el.getBoundingClientRect();
      if (r.top < window.innerHeight - 40 && r.bottom > 0) {
        el.classList.add("in");
        window.removeEventListener("scroll", check);
        window.removeEventListener("resize", check);
        return true;
      }
      return false;
    };
    if (check()) return;
    window.addEventListener("scroll", check, { passive: true });
    window.addEventListener("resize", check);
    const t = setTimeout(() => { el.classList.add("in"); }, 2000);
    return () => {
      window.removeEventListener("scroll", check);
      window.removeEventListener("resize", check);
      clearTimeout(t);
    };
  }, []);
  return ref;
}
const Reveal = ({ as = "div", className = "", children, ...rest }) => {
  const Tag = as;
  const r = useReveal();
  return <Tag ref={r} className={`reveal ${className}`} {...rest}>{children}</Tag>;
};

Object.assign(window, { Arrow, Sticker, useReveal, Reveal });
