"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSticky = useSticky;
const react_1 = require("react");
const react_use_1 = require("react-use");
const events = new Set([
    'resize',
    'scroll',
    'touchstart',
    'touchmove',
    'touchend',
    'pageshow',
    'load',
    'orientationchange',
]);
function useSticky(elemRef) {
    const [sticky, setSticky] = (0, react_1.useState)(false);
    const stickyRef = (0, react_use_1.useLatest)(sticky);
    (0, react_use_1.useEffectOnce)(() => {
        observe();
        for (const eventName of events) {
            window.addEventListener(eventName, observe, true);
        }
        return () => {
            for (const eventName of events) {
                window.removeEventListener(eventName, observe, true);
            }
        };
        function observe() {
            if (!elemRef.current)
                return;
            const refPageOffset = elemRef.current.getBoundingClientRect().top;
            const stickyOffset = parseInt(getComputedStyle(elemRef.current).top, 10);
            const stickyActive = refPageOffset <= stickyOffset;
            if (stickyActive && !stickyRef.current)
                setSticky(true);
            else if (!stickyActive && stickyRef.current)
                setSticky(false);
        }
    });
    return sticky;
}
//# sourceMappingURL=useSticky.js.map
