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

/**
 * useStickyCta - Tracks hero CTA visibility for sticky mobile CTA.
 *
 * Uses IntersectionObserver to detect when the hero CTA scrolls out of view,
 * returning a boolean to control showing/hiding the sticky bottom CTA.
 */
export function useStickyCta() {
  const heroCTARef = useRef<HTMLDivElement>(null);
  const [showStickyCTA, setShowStickyCTA] = useState(false);

  useEffect(() => {
    const ctaElement = heroCTARef.current;
    if (!ctaElement) return;

    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry) {
          setShowStickyCTA(!entry.isIntersecting);
        }
      },
      { threshold: 0 }
    );

    observer.observe(ctaElement);
    return () => observer.disconnect();
  }, []);

  return { heroCTARef, showStickyCTA };
}
