"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "ShadowKeys", {
  enumerable: true,
  get: function get() {
    return _ShadowKeys.ShadowKeys;
  }
});
exports.ShadowObserver = void 0;
Object.defineProperty(exports, "shadowObserverClassNames", {
  enumerable: true,
  get: function get() {
    return _shadowObserverClassNames.shadowObserverClassNames;
  }
});
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 = require("./ShadowKeys");
var _shadowObserverClassNames = require("./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);
};
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.ShadowKeys.SHOW_LEFT_SHADOW, false), _ShadowKeys.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.shadowObserverClassNames.SENTINEL_RIGHT);
      _this.scrollContainer.appendChild(_this.sentinels.right);
      _this.sentinels.left = document.createElement('div');
      _this.sentinels.left.classList.add(_shadowObserverClassNames.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.shadowObserverClassNames.SENTINEL_LEFT)) {
          _this.shadowStates[_ShadowKeys.ShadowKeys.SHOW_LEFT_SHADOW] = !entry.isIntersecting;
        }
        if (entry.target.classList.contains(_shadowObserverClassNames.shadowObserverClassNames.SENTINEL_RIGHT)) {
          _this.shadowStates[_ShadowKeys.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 = {};
      }
    }
  }]);
}(); // eslint-disable-next-line @atlaskit/editor/no-re-export
// eslint-disable-next-line @atlaskit/editor/no-re-export