import { _ as _applyDecoratedDescriptor, b as _defineProperty, a as _initializerDefineProperty } from '../_rollupPluginBabelHelpers-9wjJaosZ.js';
import { setComponentTemplate } from '@ember/component';
import { precompileTemplate } from '@ember/template-compilation';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import didInsert from '@ember/render-modifiers/modifiers/did-insert';
import style from 'ember-style-modifier/modifiers/style';
import createDetectElementResize from '../utils/detect-element-resize.js';

var _class, _descriptor, _descriptor2;
let EuiAutoSizerComponent = setComponentTemplate(precompileTemplate(`
    <div
      {{didInsert this.setRef}}
      {{style this.style.outerStyle}}
      ...attributes
    >
      {{yield this.style.childStyle}}
    </div>
  `, {
  strictMode: true,
  scope: () => ({
    didInsert,
    style
  })
}), (_class = class EuiAutoSizerComponent extends Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "_autoSizer", void 0);
    _defineProperty(this, "_parentNode", void 0);
    _defineProperty(this, "_window", void 0);
    // uses any instead of Window because Flow doesn't have window type
    _defineProperty(this, "_detectElementResize", void 0);
    _initializerDefineProperty(this, "height", _descriptor, this);
    _initializerDefineProperty(this, "width", _descriptor2, this);
    _defineProperty(this, "_onResize", () => {
      const {
        disableHeight,
        disableWidth
      } = this;
      if (this._parentNode) {
        // Guard against AutoSizer component being removed from the DOM immediately after being added.
        // This can result in invalid style values which can result in NaN values if we don't handle them.
        // See issue #150 for more context.

        const height = this._parentNode.offsetHeight || 0;
        const width = this._parentNode.offsetWidth || 0;
        const win = this._window || window;
        const style = win.getComputedStyle(this._parentNode) || {};
        const paddingLeft = parseInt(style.paddingLeft, 10) || 0;
        const paddingRight = parseInt(style.paddingRight, 10) || 0;
        const paddingTop = parseInt(style.paddingTop, 10) || 0;
        const paddingBottom = parseInt(style.paddingBottom, 10) || 0;
        const newHeight = height - paddingTop - paddingBottom;
        const newWidth = width - paddingLeft - paddingRight;
        if (!disableHeight && this.height !== newHeight || !disableWidth && this.width !== newWidth) {
          this.height = height - paddingTop - paddingBottom;
          this.width = width - paddingLeft - paddingRight;
          this.args.onResize?.({
            height,
            width
          });
        }
      }
    });
    _defineProperty(this, "setRef", ele => {
      this._autoSizer = ele;
      this.setup();
    });
  }
  get disableHeight() {
    return this.args.disableHeight ?? false;
  }
  get disableWidth() {
    return this.args.disableWidth ?? false;
  }
  get style() {
    let style = {
      outerStyle: {
        overflow: 'visible'
      },
      childStyle: {}
    };
    if (!this.disableHeight) {
      style.outerStyle.height = `0px`;
      if (style.childStyle) {
        style.childStyle.height = `${this.height}px`;
      }
    }
    if (!this.disableWidth) {
      style.outerStyle.width = `0px`;
      if (style.childStyle) {
        style.childStyle.width = `${this.width}px`;
      }
    }
    return style;
  }
  setup() {
    const {
      nonce
    } = this.args;
    if (this._autoSizer && this._autoSizer.parentNode && this._autoSizer.parentNode.ownerDocument && this._autoSizer.parentNode.ownerDocument.defaultView && this._autoSizer.parentNode instanceof this._autoSizer.parentNode.ownerDocument.defaultView.HTMLElement) {
      // Delay access of parentNode until mount.
      // This handles edge-cases where the component has already been unmounted before its ref has been set,
      // As well as libraries like react-lite which have a slightly different lifecycle.
      this._parentNode = this._autoSizer.parentNode;
      this._window = this._autoSizer.parentNode.ownerDocument.defaultView;

      // Defer requiring resize handler in order to support server-side rendering.
      // See issue #41
      this._detectElementResize = createDetectElementResize(nonce, this._window);
      this._detectElementResize?.addResizeListener(this._parentNode, this._onResize);
      this._onResize();
    }
  }
  willDestroy() {
    super.willDestroy();
    if (this._detectElementResize && this._parentNode) {
      this._detectElementResize.removeResizeListener(this._parentNode, this._onResize);
    }
  }
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "height", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "width", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
})), _class));

export { EuiAutoSizerComponent as default };
//# sourceMappingURL=eui-auto-sizer.js.map
