import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
export var ShadowKeys = /*#__PURE__*/function (ShadowKeys) {
  ShadowKeys["SHOW_LEFT_SHADOW"] = "showLeftShadow";
  ShadowKeys["SHOW_RIGHT_SHADOW"] = "showRightShadow";
  return ShadowKeys;
}({});
export var shadowObserverClassNames = {
  SENTINEL_LEFT: 'sentinel-left',
  SENTINEL_RIGHT: 'sentinel-right',
  SHADOW_CONTAINER: 'with-shadow-observer'
};
var requestIdleCallback = function requestIdleCallback(fn) {
  return window.requestIdleCallback ? window.requestIdleCallback(fn) : window.requestAnimationFrame(fn);
};
var cancelIdleCallback = function cancelIdleCallback(id) {
  return window.cancelIdleCallback ? window.cancelIdleCallback(id) : window.cancelAnimationFrame(id);
};
export var ShadowObserver = /*#__PURE__*/function () {
  function ShadowObserver(_ref) {
    var _defineProperty2,
      _this = this;
    var scrollContainer = _ref.scrollContainer,
      onUpdateShadows = _ref.onUpdateShadows;
    _classCallCheck(this, ShadowObserver);
    _defineProperty(this, "sentinels", {});
    _defineProperty(this, "shadowStates", (_defineProperty2 = {}, _defineProperty(_defineProperty2, ShadowKeys.SHOW_LEFT_SHADOW, false), _defineProperty(_defineProperty2, ShadowKeys.SHOW_RIGHT_SHADOW, false), _defineProperty2));
    _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();
  }
  _createClass(ShadowObserver, [{
    key: "dispose",
    value: function dispose() {
      if (this.intersectionObserver) {
        this.intersectionObserver.disconnect();
        this.intersectionObserver = undefined;
        this.requestCallbackId && cancelIdleCallback(this.requestCallbackId);
      }
    }
  }]);
  return ShadowObserver;
}();