import { useState } from 'react';
import { useEffectOnce, useLatest } from 'react-use';
import { getTargetZIndex } from "../utils/get-target-z-index.js";
const events = new Set([
    'resize',
    'scroll',
    'touchstart',
    'touchmove',
    'touchend',
    'pageshow',
    'load',
    'orientationchange',
]);
export function useTargetZIndex(dataLayoutSelector, offset = 10) {
    const [zIndex, setZIndex] = useState(() => getTargetZIndex(dataLayoutSelector, offset));
    const zIndexRef = useLatest(zIndex);
    useEffectOnce(() => {
        let rafId = null;
        recalculate();
        for (const eventName of events) {
            window.addEventListener(eventName, scheduleRecalculate, true);
        }
        return () => {
            if (rafId !== null) {
                cancelAnimationFrame(rafId);
            }
            for (const eventName of events) {
                window.removeEventListener(eventName, scheduleRecalculate, true);
            }
        };
        function scheduleRecalculate() {
            if (rafId !== null) {
                cancelAnimationFrame(rafId);
            }
            rafId = requestAnimationFrame(recalculate);
        }
        function recalculate() {
            rafId = null;
            const newZIndex = getTargetZIndex(dataLayoutSelector, offset);
            if (newZIndex !== zIndexRef.current) {
                setZIndex(newZIndex);
            }
        }
    });
    return zIndex;
}
//# sourceMappingURL=useTargetZIndex.js.map
