import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// oxlint-disable-next-line @atlassian/no-restricted-imports

import memoize from 'lodash/memoize';
// Use this selector to set the intersection observer boundary for editor's inline node views
// If this does not exist, it will use the IntersectionObserver's default root
var INTERSECTION_OBSERVER_ROOT_SELECTOR = '[data-editor-scroll-container="true"]';
var INTERSECTION_OBSERVER_OPTIONS = {
  rootMargin: '0px 0px 100px 0px',
  threshold: 0
};

// Parameterized singleton
export var getOrCreateOnVisibleObserver = memoize(function (view) {
  var intersectionObserverOptions = _objectSpread({
    root: view.dom.closest(INTERSECTION_OBSERVER_ROOT_SELECTOR)
  }, INTERSECTION_OBSERVER_OPTIONS);
  var callbackMap = new WeakMap();
  var observer = new IntersectionObserver(function (entries) {
    entries.filter(function (entry) {
      return entry.isIntersecting;
    }) // Only process visible entries
    .map(function (entry) {
      return callbackMap.get(entry.target);
    })
    // Invoke callbacks together to group browser rendering
    // Avoiding requestAnimationFrame to reduce visual flickering
    .forEach(function (cb) {
      return cb === null || cb === void 0 ? void 0 : cb();
    });
  }, intersectionObserverOptions);
  return {
    observe: function observe(el, callback) {
      callbackMap.set(el, callback);
      observer.observe(el);
      return function () {
        return observer.unobserve(el);
      };
    }
  };
});