// (C) 2025 GoodData Corporation
import { useEffect } from "react";
import { GOODSTRAP_SCROLLED_EVENT } from "../../utils/scroll.js";
/**
 * Hook to trigger a callback when a parent of the anchor element scrolls.
 * Listens for the custom GOODSTRAP_SCROLLED_EVENT event.
 *
 * @internal
 */
export function useCloseOnParentScroll(isOpen, onClose, anchorRef) {
    useEffect(() => {
        if (!isOpen) {
            return undefined;
        }
        const handleScroll = (event) => {
            const scrollTarget = event?.detail?.node ?? event.target;
            if (anchorRef.current instanceof Element && scrollTarget?.contains?.(anchorRef.current)) {
                onClose();
            }
        };
        window.addEventListener(GOODSTRAP_SCROLLED_EVENT, handleScroll);
        return () => window.removeEventListener(GOODSTRAP_SCROLLED_EVENT, handleScroll);
    }, [isOpen, onClose, anchorRef]);
}
