import { _ as _applyDecoratedDescriptor, b as _defineProperty } from '../_rollupPluginBabelHelpers-9wjJaosZ.js';
import { registerDestructor } from '@ember/destroyable';
import { action } from '@ember/object';
import Modifier from 'ember-modifier';

var _class;

// IE11 and Safari don't support the `ResizeObserver` API at the time of writing
const hasResizeObserver = typeof window !== 'undefined' && typeof window.ResizeObserver !== 'undefined';
const mutationObserverOptions = {
  // [MutationObserverInit](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserverInit)
  attributes: true,
  // Account for style changes from `className` or `style`
  characterData: true,
  // Account for text content size differences
  childList: true,
  // Account for adding/removing child nodes
  subtree: true // Account for deep child nodes
};
const makeCompatibleObserver = (node, callback) => {
  const observer = new MutationObserver(callback);
  //eslint-disable-next-line
  observer.observe(node, mutationObserverOptions);
  window.addEventListener('resize', callback);
  const _disconnect = observer.disconnect.bind(observer);
  observer.disconnect = () => {
    _disconnect();
    window.removeEventListener('resize', callback);
  };
  return observer;
};
const makeResizeObserver = (node, callback) => {
  let observer;
  if (hasResizeObserver) {
    observer = new window.ResizeObserver(callback);
    observer.observe(node);
  } else {
    observer = makeCompatibleObserver(node, callback);
    requestAnimationFrame(callback); // Mimic ResizeObserver behavior of triggering a resize event on init
  }
  return observer;
};
let ResizeObserver = (_class = class ResizeObserver extends Modifier {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "height", 0);
    _defineProperty(this, "width", 0);
    _defineProperty(this, "observer", null);
    _defineProperty(this, "element", null);
    _defineProperty(this, "named", null);
    _defineProperty(this, "positional", null);
  }
  setSize({
    width,
    height
  }) {
    let [dimension] = this.positional;
    const doesWidthMatter = dimension !== 'height';
    const doesHeightMatter = dimension !== 'width';
    if (doesWidthMatter && width !== this.width || doesHeightMatter && height !== this.height) {
      this.width = width;
      this.height = height;
      this.named?.onResize({
        width,
        height
      });
    }
  }
  modify(element, positional = [], named) {
    this.element = element;
    this.named = named;
    this.positional = positional;
    this._setup();
    registerDestructor(this, () => this._teardown());
  }
  _setup() {
    let {
      setSize,
      element
    } = this;
    if (element != null) {
      // ResizeObserver's first call to the observation callback is scheduled in the future
      // so find the element's initial dimensions now
      const boundingRect = element.getBoundingClientRect();
      setSize({
        width: boundingRect.width,
        height: boundingRect.height
      });
      this.observer = makeResizeObserver(element, () => {
        if (!element) {
          return;
        }
        const boundingRect = element.getBoundingClientRect();
        setSize({
          width: boundingRect.width,
          height: boundingRect.height
        });
      });
    } else {
      setSize({
        width: 0,
        height: 0
      });
    }
  }
  _teardown() {
    this.observer?.disconnect();
    this.observer = null;
    this.element = null;
    this.named = null;
    this.positional = null;
  }
}, (_applyDecoratedDescriptor(_class.prototype, "setSize", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setSize"), _class.prototype)), _class);

export { ResizeObserver as default };
//# sourceMappingURL=resize-observer.js.map
