// 공용 훅: 스크롤 reveal, 카운트다운, 언어/테마 컨텍스트

const { useState, useEffect, useRef, useMemo, createContext, useContext } = React;

// ---------- Reveal observer ----------
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll(".reveal:not(.is-visible)");
    if (!("IntersectionObserver" in window) || els.length === 0) {
      els.forEach(el => el.classList.add("is-visible"));
      return;
    }
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add("is-visible");
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: "0px 0px -40px 0px" });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  });
}

// ---------- Countdown ----------
// target: Date object
function useCountdown(target) {
  const compute = () => {
    const now = new Date();
    const diff = target - now;
    if (diff <= 0) return { d: 0, h: 0, m: 0, s: 0, past: true };
    const d = Math.floor(diff / 86400000);
    const h = Math.floor((diff % 86400000) / 3600000);
    const m = Math.floor((diff % 3600000) / 60000);
    const s = Math.floor((diff % 60000) / 1000);
    return { d, h, m, s, past: false };
  };
  const [t, setT] = useState(compute);
  useEffect(() => {
    const id = setInterval(() => setT(compute()), 1000);
    return () => clearInterval(id);
  }, [target]);
  return t;
}

// ---------- App Context ----------
const AppCtx = createContext(null);
function useApp() { return useContext(AppCtx); }

Object.assign(window, { useReveal, useCountdown, AppCtx, useApp });
