"use client";

import { ReactNode, useEffect, useRef } from "react";

interface WowProps {
  children: ReactNode;
  className?: string;
  delay?: string;
  duration?: string;
  style?: React.CSSProperties;
}

export default function Wow({
  children,
  className = "",
  delay,
  duration,
  style,
}: WowProps) {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            const animated = el.querySelectorAll(".wow");
            animated.forEach((node) => {
              const a = node as HTMLElement;
              const anim = a.dataset.animation || "fadeInUp";
              const dur = a.dataset.duration || "1000ms";
              const del = a.dataset.delay || "0ms";
              a.classList.add("animated", anim);
              a.style.animationDuration = dur;
              a.style.animationDelay = del;
            });
            observer.disconnect();
          }
        });
      },
      { threshold: 0.05 }
    );
    observer.observe(el);
    return () => observer.disconnect();
  }, []);

  return (
    <div ref={ref} className={className} style={style}>
      {children}
    </div>
  );
}
