import { useState, useEffect } from "react";
export const useElementScroll = (elementRef) => {
    const [scrollY, setScrollY] = useState(0);
    const [scrollDirection, setScrollDirection] = useState(null);
    useEffect(() => {
        if (!elementRef.current)
            return;
        const element = elementRef.current;
        let lastScrollY = element.scrollTop;
        let lastDirection = null;
        const handleScroll = () => {
            const currentScrollY = element.scrollTop;
            const delta = currentScrollY - lastScrollY;
            let newDirection = lastDirection;
            if (delta > 4 && currentScrollY > 10) {
                newDirection = "down";
            }
            else if (delta < -4 && currentScrollY > 0) {
                newDirection = "up";
            }
            setScrollY(currentScrollY);
            // Solo actualizar la dirección si cambió
            if (newDirection !== lastDirection) {
                setScrollDirection(newDirection);
                lastDirection = newDirection;
            }
            lastScrollY = currentScrollY;
        };
        // Agregar el listener al elemento
        element.addEventListener("scroll", handleScroll, { passive: true });
        return () => {
            element.removeEventListener("scroll", handleScroll);
        };
    }, [elementRef]);
    return { scrollY, scrollDirection };
};
