import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import './index-bbdfb97a.js';
import { useRef, useState, useEffect } from 'react';

function useStickyScroll() {
  var threshold = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  var element = arguments.length > 1 ? arguments[1] : undefined;
  var stickyRef = useRef(null);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isSticky = _useState2[0],
    setIsSticky = _useState2[1];
  var lastUpdateRef = useRef(0);
  useEffect(function () {
    var handleScroll = function handleScroll() {
      var _stickyRef$current$ge, _element$getBoundingC;
      if (!stickyRef.current || !element) return;
      var now = Date.now();
      if (now - lastUpdateRef.current < 300) {
        return; // Skip update if less than 300ms since last update
      }
      var top = ((_stickyRef$current$ge = stickyRef.current.getBoundingClientRect().top) !== null && _stickyRef$current$ge !== void 0 ? _stickyRef$current$ge : 0) - ((_element$getBoundingC = element === null || element === void 0 ? void 0 : element.getBoundingClientRect().top) !== null && _element$getBoundingC !== void 0 ? _element$getBoundingC : 0);
      var newIsSticky = top <= threshold;
      if (newIsSticky !== isSticky) {
        lastUpdateRef.current = now;
        setIsSticky(newIsSticky);
      }
    };
    handleScroll(); // run once on mount

    element === null || element === void 0 || element.addEventListener('scroll', handleScroll, {
      passive: true
    });
    return function () {
      return element === null || element === void 0 ? void 0 : element.removeEventListener('scroll', handleScroll);
    };
  }, [threshold, isSticky, element]);
  return {
    stickyRef: stickyRef,
    isSticky: isSticky
  };
}

export { useStickyScroll as u };
