import { _ as _applyDecoratedDescriptor, 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 { action } from '@ember/object';
import { throttle } from '@ember/runloop';
import { modifier } from 'ember-modifier';
import { not, and, or } from 'ember-truth-helpers';
import randomId from '../-private/random-id.js';
import argOrDefault from '../helpers/arg-or-default.js';
import { isWithinMinBreakpoint } from '../utils/breakpoint.js';
import EuiFlyoutComponent from './eui-flyout.js';

var _class, _descriptor;
const triggerHandlersModifier = modifier((element, _pos, {
  flyoutID,
  isOpen
}) => {
  element.setAttribute('aria-controls', flyoutID);
  element.setAttribute('aria-expanded', isOpen ? 'true' : '');
  element.setAttribute('aria-pressed', isOpen ? 'true' : '');
  const fn = e => {
    e.stopImmediatePropagation();
  };
  element.addEventListener('touchend', fn);
  element.addEventListener('mouseup', fn, true);
  return () => {
    element.removeEventListener('touchend', fn);
    element.removeEventListener('mouseup', fn, true);
  };
});
const onWindowResizeModifier = modifier((_ele, _pos, {
  isDocked = false,
  functionToCallOnWindowResize
}) => {
  if (isDocked) {
    window.addEventListener('resize', functionToCallOnWindowResize);
  }
  return () => {
    if (isDocked) {
      window.removeEventListener('resize', functionToCallOnWindowResize);
    }
  };
});
let EuiCollapsibleNavComponent = setComponentTemplate(precompileTemplate(`
    <span
      {{onWindowResizeModifier
        isDocked=this.isDocked
        functionToCallOnWindowResize=this.functionToCallOnWindowResize
      }}
    ></span>
    {{#let (argOrDefault @id (randomId)) as |flyoutID|}}

      {{#if (not (and this.navIsDocked (not this.showButtonIfDocked)))}}
        {{yield
          (modifier triggerHandlersModifier isOpen=@isOpen flyoutID=flyoutID)
          to="button"
        }}
      {{/if}}

      {{#if (or @isOpen this.navIsDocked)}}
        <EuiFlyout
          id={{flyoutID}}
          class="euiCollapsibleNav"
          @as={{this.as}}
          @size={{this.size}}
          @side={{this.side}}
          @role={{this.role}}
          @ownFocus={{this.ownFocus}}
          @outsideClickCloses={{this.outsideClickCloses}}
          @closeButtonPosition={{this.closeButtonPosition}}
          @paddingSize={{this.paddingSize}}
          @type={{if this.navIsDocked "push" "overlay"}}
          @hideCloseButton={{this.navIsDocked}}
          @pushMinBreakpoint={{this.dockedBreakpoint}}
          @onClose={{@onClose}}
          ...attributes
        >
          {{yield to="content"}}
        </EuiFlyout>
      {{/if}}
    {{/let}}
  `, {
  strictMode: true,
  scope: () => ({
    onWindowResizeModifier,
    argOrDefault,
    randomId,
    not,
    and,
    triggerHandlersModifier,
    or,
    EuiFlyout: EuiFlyoutComponent
  })
}), (_class = class EuiCollapsibleNavComponent extends Component {
  constructor(...args) {
    super(...args);
    _initializerDefineProperty(this, "windowIsLargeEnoughToPush", _descriptor, this);
  }
  get isDocked() {
    return this.args.isDocked ?? false;
  }
  get navIsDocked() {
    return this.isDocked && this.windowIsLargeEnoughToPush;
  }
  get dockedBreakpoint() {
    return this.args.dockedBreakpoint ?? 'l';
  }
  get showButtonIfDocked() {
    return this.args.showButtonIfDocked ?? false;
  }
  get as() {
    return this.args.as ?? 'nav';
  }
  get size() {
    if (this.args.size && typeof this.args.size !== 'string') {
      return `${this.args.size}px`;
    }
    return this.args.size ?? '320px';
  }
  get side() {
    return this.args.side ?? 'left';
  }
  get role() {
    return this.args.role ?? null;
  }
  get ownFocus() {
    return this.args.ownFocus ?? true;
  }
  get outsideClickCloses() {
    return this.args.outsideClickCloses ?? true;
  }
  get closeButtonPosition() {
    return this.args.closeButtonPosition ?? 'outside';
  }
  get paddingSize() {
    return this.args.paddingSize ?? 'none';
  }
  functionToCallOnWindowResize() {
    throttle(() => {
      if (isWithinMinBreakpoint(window.innerWidth, this.dockedBreakpoint)) {
        this.windowIsLargeEnoughToPush = true;
      } else {
        this.windowIsLargeEnoughToPush = false;
      }
    }, 50);
  }
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "windowIsLargeEnoughToPush", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return isWithinMinBreakpoint(typeof window === 'undefined' ? -Infinity : window.innerWidth, this.dockedBreakpoint);
  }
}), _applyDecoratedDescriptor(_class.prototype, "functionToCallOnWindowResize", [action], Object.getOwnPropertyDescriptor(_class.prototype, "functionToCallOnWindowResize"), _class.prototype)), _class));

export { EuiCollapsibleNavComponent as default };
//# sourceMappingURL=eui-collapsible-nav.js.map
