"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.shadowObserverClassNames = exports.ShadowObserver = exports.ShadowKeys = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty3 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var ShadowKeys = exports.ShadowKeys = /*#__PURE__*/function (ShadowKeys) {
  ShadowKeys["SHOW_LEFT_SHADOW"] = "showLeftShadow";
  ShadowKeys["SHOW_RIGHT_SHADOW"] = "showRightShadow";
  return ShadowKeys;
}({});
var shadowObserverClassNames = exports.shadowObserverClassNames = {
  SENTINEL_LEFT: 'sentinel-left',
  SENTINEL_RIGHT: 'sentinel-right',
  SHADOW_CONTAINER: 'with-shadow-observer'
};
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);
};
var ShadowObserver = exports.ShadowObserver = /*#__PURE__*/function () {
  function ShadowObserver(_ref) {
    var _this = this;
    var scrollContainer = _ref.scrollContainer,
      onUpdateShadows = _ref.onUpdateShadows;
    (0, _classCallCheck2.default)(this, ShadowObserver);
    (0, _defineProperty3.default)(this, "sentinels", {});
    (0, _defineProperty3.default)(this, "shadowStates", (0, _defineProperty3.default)((0, _defineProperty3.default)({}, ShadowKeys.SHOW_LEFT_SHADOW, false), ShadowKeys.SHOW_RIGHT_SHADOW, false));
    (0, _defineProperty3.default)(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);
    });
    (0, _defineProperty3.default)(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 (0, _createClass2.default)(ShadowObserver, [{
    key: "dispose",
    value: function dispose() {
      if (this.intersectionObserver) {
        this.intersectionObserver.disconnect();
        this.intersectionObserver = undefined;
        this.requestCallbackId && cancelIdleCallback(this.requestCallbackId);
      }
      if ((0, _expValEquals.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 = {};
      }
    }
  }]);
}();