"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _utils = require("../../../utils");
// This property enables the intersection observer to be run once the
// HTML element being observed is within `X` px of the target container it is
// being compared to. Since the default container is the `document`, we set this
// up to check once a Smart Link is within `X` px from the bottom of the viewport.
var ROOT_MARGIN_VERTICAL = '360px';
var useIntersectionObserver = function useIntersectionObserver(_ref) {
  var onIntersectingCallback = _ref.onIntersecting,
    onIntersectionCallback = _ref.onIntersection;
  var ref = (0, _react.useRef)(null);
  var onIntersectingCallbackRef = (0, _react.useRef)(onIntersectingCallback);
  var onIntersectionCallbackRef = (0, _react.useRef)(onIntersectionCallback);
  (0, _react.useEffect)(function () {
    onIntersectingCallbackRef.current = onIntersectingCallback;
    onIntersectionCallbackRef.current = onIntersectionCallback;
  }, [onIntersectingCallback, onIntersectionCallback]);
  var onIntersection = (0, _react.useCallback)(function (entries, observer) {
    var _onIntersectionCallba;
    var isIntersecting = entries.some(function (entry) {
      return entry.isIntersecting;
    });
    (_onIntersectionCallba = onIntersectionCallbackRef.current) === null || _onIntersectionCallba === void 0 || _onIntersectionCallba.call(onIntersectionCallbackRef, isIntersecting);
    if (isIntersecting) {
      var _onIntersectingCallba;
      (_onIntersectingCallba = onIntersectingCallbackRef.current) === null || _onIntersectingCallba === void 0 || _onIntersectingCallba.call(onIntersectingCallbackRef);
      observer.disconnect();
    }
  }, []);
  (0, _react.useEffect)(function () {
    if (!(0, _utils.isIntersectionObserverSupported)() || !ref.current) {
      return;
    }
    var intersectionObserver = new IntersectionObserver(onIntersection, {
      rootMargin: "".concat(ROOT_MARGIN_VERTICAL, " 0px ").concat(ROOT_MARGIN_VERTICAL, " 0px")
    });
    intersectionObserver.observe(ref.current);
    return function () {
      return intersectionObserver.disconnect();
    };
  }, [ref, onIntersection]);
  return ref;
};
var _default = exports.default = useIntersectionObserver;