import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
export let ShadowKeys = /*#__PURE__*/function (ShadowKeys) {
  ShadowKeys["SHOW_LEFT_SHADOW"] = "showLeftShadow";
  ShadowKeys["SHOW_RIGHT_SHADOW"] = "showRightShadow";
  return ShadowKeys;
}({});
export const shadowObserverClassNames = {
  SENTINEL_LEFT: 'sentinel-left',
  SENTINEL_RIGHT: 'sentinel-right',
  SHADOW_CONTAINER: 'with-shadow-observer'
};
const 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);
};
const 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 class ShadowObserver {
  constructor({
    scrollContainer,
    onUpdateShadows
  }) {
    _defineProperty(this, "sentinels", {});
    _defineProperty(this, "shadowStates", {
      [ShadowKeys.SHOW_LEFT_SHADOW]: false,
      [ShadowKeys.SHOW_RIGHT_SHADOW]: false
    });
    _defineProperty(this, "init", () => {
      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((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", entry => {
      this.requestCallbackId = requestIdleCallback(() => {
        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();
  }
  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 ? void 0 : _this$sentinels$left.remove();
      (_this$sentinels$right = this.sentinels.right) === null || _this$sentinels$right === void 0 ? void 0 : _this$sentinels$right.remove();
      this.sentinels = {};
    }
  }
}