import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { ShadowKeys } from './ShadowKeys';
import { shadowObserverClassNames } from './shadowObserverClassNames';
var requestIdleCallback = function requestIdleCallback(fn) {
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return window.requestIdleCallback ?
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  window.requestIdleCallback(fn) : window.requestAnimationFrame(fn);
};
var cancelIdleCallback = function cancelIdleCallback(id) {
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return window.cancelIdleCallback ?
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  window.cancelIdleCallback(id) : window.cancelAnimationFrame(id);
};
export var ShadowObserver = /*#__PURE__*/function () {
  function ShadowObserver(_ref) {
    var _this = this;
    var scrollContainer = _ref.scrollContainer,
      onUpdateShadows = _ref.onUpdateShadows;
    _classCallCheck(this, ShadowObserver);
    _defineProperty(this, "sentinels", {});
    _defineProperty(this, "shadowStates", _defineProperty(_defineProperty({}, ShadowKeys.SHOW_LEFT_SHADOW, false), ShadowKeys.SHOW_RIGHT_SHADOW, false));
    _defineProperty(this, "init", function () {
      if (!_this.scrollContainer || _this.intersectionObserver) {
        return;
      }
      _this.sentinels.right = document.createElement('div');
      _this.sentinels.right.classList.add(shadowObserverClassNames.SENTINEL_RIGHT);
      _this.scrollContainer.appendChild(_this.sentinels.right);
      _this.sentinels.left = document.createElement('div');
      _this.sentinels.left.classList.add(shadowObserverClassNames.SENTINEL_LEFT);
      _this.scrollContainer.prepend(_this.sentinels.left);
      _this.intersectionObserver = new IntersectionObserver(function (entries, _) {
        entries.forEach(_this.onIntersect);
      }, {
        root: _this.scrollContainer,
        rootMargin: '1px'
      });
      _this.intersectionObserver.observe(_this.sentinels.left);
      _this.intersectionObserver.observe(_this.sentinels.right);
    });
    _defineProperty(this, "onIntersect", function (entry) {
      _this.requestCallbackId = requestIdleCallback(function () {
        if (entry.target.classList.contains(shadowObserverClassNames.SENTINEL_LEFT)) {
          _this.shadowStates[ShadowKeys.SHOW_LEFT_SHADOW] = !entry.isIntersecting;
        }
        if (entry.target.classList.contains(shadowObserverClassNames.SENTINEL_RIGHT)) {
          _this.shadowStates[ShadowKeys.SHOW_RIGHT_SHADOW] = !entry.isIntersecting;
        }
        _this.onUpdateShadows(_this.shadowStates);
      });
    });
    this.scrollContainer = scrollContainer;
    this.onUpdateShadows = onUpdateShadows;
    this.init();
  }
  return _createClass(ShadowObserver, [{
    key: "dispose",
    value: function dispose() {
      if (this.intersectionObserver) {
        this.intersectionObserver.disconnect();
        this.intersectionObserver = undefined;
        this.requestCallbackId && cancelIdleCallback(this.requestCallbackId);
      }
      if (expValEquals('platform_editor_renderer_shadow_observer_cleanup', 'isEnabled', true)) {
        var _this$sentinels$left, _this$sentinels$right;
        (_this$sentinels$left = this.sentinels.left) === null || _this$sentinels$left === void 0 || _this$sentinels$left.remove();
        (_this$sentinels$right = this.sentinels.right) === null || _this$sentinels$right === void 0 || _this$sentinels$right.remove();
        this.sentinels = {};
      }
    }
  }]);
}();
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { ShadowKeys } from './ShadowKeys';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { shadowObserverClassNames } from './shadowObserverClassNames';