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 { macroCondition, dependencySatisfies, importSync } from '@embroider/macros';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import didInsert from '@ember/render-modifiers/modifiers/did-insert';
import { next, cancel, later, scheduleOnce } from '@ember/runloop';
import style from 'ember-style-modifier/modifiers/style';
import { eq, and, or } from 'ember-truth-helpers';
import argOrDefault, { argOrDefaultDecorator } from '../helpers/arg-or-default.js';
import classNames from '../helpers/class-names.js';
import ResizeObserver from '../modifiers/resize-observer.js';
import { keys } from '../utils/keys.js';
import { findPopoverPosition } from '../utils/popover/index.js';
import EuiPortal from './eui-portal.js';
import EuiTooltipPopover from './eui-tool-tip-popover.js';

var _dec, _dec2, _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9;
let cached = macroCondition(dependencySatisfies('ember-source', '>= 4.1.0-alpha.0')) ? importSync('@glimmer/tracking').cached : importSync('ember-cached-decorator-polyfill').cached;
const delayToMsMap = {
  regular: 250,
  long: 250 * 5
};
const DEFAULT_TOOLTIP_STYLES = {
  // position the tooltip content near the top-left
  // corner of the window so it can't create scrollbars
  // 50,50 because who knows what negative margins, padding, etc
  top: '50px',
  left: '50px',
  // just in case, avoid any potential flicker by hiding
  // the tooltip before it is positioned
  opacity: '0',
  // prevent accidental mouse interaction while positioning
  visibility: 'hidden',
  display: 'inlineBlock'
};
let EuiToolTip = setComponentTemplate(precompileTemplate(`
    {{#let
      (has-block "content") (has-block "title") (has-block "anchor")
      as |hasContentBlock hasTitleBlock hasAnchorBlock|
    }}
      {{#if @attachTo}}
        <span
          {{didInsert this.updateAttachTo}}
          {{didUpdate this.updateAttachTo @attachTo}}
          {{didUpdate
            (if (eq @isShown true) this.onFocus this.onBlur)
            @isShown
          }}
          {{didInsert (if (eq @isShown true) this.onFocus this.onBlur)}}
        ></span>
      {{else}}
        <span
          role="tooltip"
          class={{classNames
            "euiToolTipAnchor"
            (if (eq @display "block") "euiToolTipAnchor--displayBlock")
            @anchorClassName
          }}
          {{didInsert this.didInsertAnchor}}
          {{on "mouseover" this.showToolTip}}
          {{on "focusin" this.onFocus}}
          {{on "focusout" this.onBlur}}
          {{on "mouseout" this.onMouseOut}}
          {{didUpdate
            (if (eq @isShown true) this.onFocus this.onBlur)
            @isShown
          }}
          {{didInsert (if (eq @isShown true) this.onFocus this.onBlur)}}
        >
          {{#if hasAnchorBlock}}
            {{yield this.id to="anchor"}}
          {{else}}
            {{yield this.id}}
          {{/if}}
        </span>
      {{/if}}
      {{#if
        (and this.visible (or @title @content hasContentBlock hasTitleBlock))
      }}
        <EuiPortal>
          <EuiToolTipPopover
            class={{classNames
              "euiToolTip"
              @className
              componentName="EuiToolTip"
              position=this.calculatedPosition
            }}
            @positionToolTip={{this.positionToolTip}}
            @popoverRef={{this.setPopoverRef}}
            id={{this.id}}
            @hasTitle={{if
              (and (argOrDefault @hasTitle true) (or hasTitleBlock @title))
              true
            }}
            role="tooltip"
            ...attributes
            {{style this.toolTipStyles}}
          >
            <:title>
              {{#if hasTitleBlock}}
                {{yield to="title"}}
              {{else}}
                {{@title}}
              {{/if}}
            </:title>
            <:content>
              <div class="euiToolTip__arrow" {{style this.arrowStyles}}></div>
              <div {{resizeObserver onResize=this.positionToolTip}}>
                {{#if hasContentBlock}}
                  {{yield to="content"}}
                {{else}}
                  {{@content}}
                {{/if}}
              </div>
            </:content>
          </EuiToolTipPopover>
        </EuiPortal>
      {{/if}}
    {{/let}}
  `, {
  strictMode: true,
  scope: () => ({
    didInsert,
    didUpdate: didInsert,
    eq,
    classNames,
    on,
    and,
    or,
    EuiPortal,
    EuiToolTipPopover: EuiTooltipPopover,
    argOrDefault,
    style,
    resizeObserver: ResizeObserver
  })
}), (_dec = argOrDefaultDecorator('top'), _dec2 = argOrDefaultDecorator('regular'), (_class = class EuiToolTip extends Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "anchor", null);
    _defineProperty(this, "popover", null);
    _initializerDefineProperty(this, "position", _descriptor, this);
    _initializerDefineProperty(this, "delay", _descriptor2, this);
    //STATE
    _initializerDefineProperty(this, "visible", _descriptor3, this);
    _initializerDefineProperty(this, "hasFocus", _descriptor4, this);
    _initializerDefineProperty(this, "calculatedPosition", _descriptor5, this);
    _initializerDefineProperty(this, "toolTipStyles", _descriptor6, this);
    _initializerDefineProperty(this, "arrowStyles", _descriptor7, this);
    _initializerDefineProperty(this, "id", _descriptor8, this);
    _initializerDefineProperty(this, "_attachTo", _descriptor9, this);
    _defineProperty(this, "timeoutId", void 0);
  }
  updateAttachTo() {
    if (!this.args.attachTo && this._attachTo) {
      this.removeAttachToHandlers();
      this._attachTo = null;
      return;
    }
    if (this.args.attachTo && this.args.attachTo !== this._attachTo) {
      next(() => {
        this.removeAttachToHandlers();
        this._attachTo = this.args.attachTo;
        this.setupAttachToHandlers();
      });
    }
  }
  setupAttachToHandlers() {
    if (this._attachTo && this.attachTo) {
      this.attachTo.addEventListener('mousemove', this.showToolTip);
      this.attachTo.addEventListener('focusin', this.onFocus);
      //@ts-expect-error
      this.attachTo.addEventListener('mouseout', this.onMouseOut);
      this.attachTo.addEventListener('focusout', this.onBlur);
      this.positionToolTip();
    }
  }
  removeAttachToHandlers() {
    if (this._attachTo && this.attachTo) {
      this.attachTo.removeEventListener('mousemove', this.showToolTip);
      this.attachTo.removeEventListener('focusin', this.onFocus);
      //@ts-expect-error
      this.attachTo.removeEventListener('mouseout', this.onMouseOut);
      this.attachTo.removeEventListener('focusout', this.onBlur);
    }
  }
  clearAnimationTimeout() {
    if (this.timeoutId) {
      cancel(this.timeoutId);
      this.timeoutId = undefined;
    }
  }
  visibleDidUpdate(value) {
    if (this.visible === false && value === true) {
      requestAnimationFrame(this.testAnchor);
    }
  }
  testAnchor() {
    // when the tooltip is visible, this checks if the anchor is still part of document
    // this fixes when the react root is removed from the dom without unmounting
    // https://github.com/elastic/eui/issues/1105
    if (document.body.contains(this._anchor) === false) {
      // the anchor is no longer part of `document`
      this.hideToolTip();
    } else {
      if (this.visible) {
        // if still visible, keep checking
        requestAnimationFrame(this.testAnchor);
      }
    }
  }
  didInsertAnchor(ref) {
    this.anchor = ref;
  }
  setPopoverRef(ref) {
    this.popover = ref;

    // if the popover has been unmounted, clear
    // any previous knowledge about its size
    if (ref === null) {
      this.toolTipStyles = DEFAULT_TOOLTIP_STYLES;
      this.arrowStyles = {};
    } else {
      this.positionToolTip();
    }
  }
  showToolTip() {
    if (!this.timeoutId && !this.visible) {
      const fn = () => {
        this.visible = true;
      };
      this.timeoutId = later(this, () => {
        scheduleOnce('afterRender', this, fn);
      }, delayToMsMap[this.delay]);
    }
  }
  get attachTo() {
    if (typeof this._attachTo === 'string') {
      return document.querySelector(this._attachTo);
    } else {
      return this._attachTo;
    }
  }
  get _anchor() {
    return this.attachTo || this.anchor;
  }
  positionToolTip() {
    const requestedPosition = this.position;
    if (!this._anchor || !this.popover) {
      return;
    }
    const {
      position,
      left,
      top,
      arrow
    } = findPopoverPosition({
      anchor: this._anchor,
      popover: this.popover,
      position: requestedPosition,
      offset: 16,
      // offset popover 16px from the anchor
      arrowConfig: {
        arrowWidth: 12,
        arrowBuffer: 4
      }
    });
    const windowWidth = document.documentElement.clientWidth || window.innerWidth;
    const useRightValue = windowWidth / 2 < left;
    const toolTipStyles = {
      top: `${top}px`,
      left: useRightValue ? 'auto' : `${left}px`,
      right: useRightValue ? `${windowWidth - left - this.popover.offsetWidth}px` : 'auto'
    };
    this.visible = true;
    this.calculatedPosition = position;
    this.toolTipStyles = toolTipStyles;
    if (arrow) {
      this.arrowStyles = {
        left: `${arrow.left}px`,
        top: `${arrow?.top}px`
      };
    }
  }
  hideToolTip() {
    if (this.args.isShown === true) {
      return;
    }
    this.clearAnimationTimeout();
    const fn = () => {
      if (!this.isDestroying || !this.isDestroyed) {
        this.visible = false;
      }
    };
    scheduleOnce('afterRender', this, fn);
  }
  hasFocusMouseMoveListener() {
    this.hideToolTip();
    window.removeEventListener('mousemove', this.hasFocusMouseMoveListener);
  }
  onKeyUp(event) {
    if (event.key === keys.TAB) {
      window.addEventListener('mousemove', this.hasFocusMouseMoveListener);
    }
  }
  onFocus() {
    this.hasFocus = true;
    this.showToolTip();
  }
  onBlur() {
    this.hasFocus = false;
    this.hideToolTip();
  }
  onMouseOut(event) {
    // Prevent mousing over children from hiding the tooltip by testing for whether the mouse has
    // left the anchor for a non-child.
    if (this._anchor === event.relatedTarget || this._anchor != null && !this._anchor.contains(event.relatedTarget)) {
      if (!this.hasFocus) {
        this.hideToolTip();
      }
    }
    if (this.args.onMouseOut) {
      this.args.onMouseOut(event);
    }
  }
  willDestroy() {
    super.willDestroy();
    this.clearAnimationTimeout();
    this.removeAttachToHandlers();
    this.anchor = null;
    this.popover = null;
    this._attachTo = null;
  }
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "position", [_dec], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "delay", [_dec2], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "visible", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return false;
  }
}), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, "hasFocus", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return false;
  }
}), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, "calculatedPosition", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return this.position;
  }
}), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, "toolTipStyles", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return DEFAULT_TOOLTIP_STYLES;
  }
}), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, "arrowStyles", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return {};
  }
}), _descriptor8 = _applyDecoratedDescriptor(_class.prototype, "id", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return this.args.id || guidFor({});
  }
}), _descriptor9 = _applyDecoratedDescriptor(_class.prototype, "_attachTo", [tracked], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function () {
    return null;
  }
}), _applyDecoratedDescriptor(_class.prototype, "updateAttachTo", [action], Object.getOwnPropertyDescriptor(_class.prototype, "updateAttachTo"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setupAttachToHandlers", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setupAttachToHandlers"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "removeAttachToHandlers", [action], Object.getOwnPropertyDescriptor(_class.prototype, "removeAttachToHandlers"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "clearAnimationTimeout", [action], Object.getOwnPropertyDescriptor(_class.prototype, "clearAnimationTimeout"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "visibleDidUpdate", [action], Object.getOwnPropertyDescriptor(_class.prototype, "visibleDidUpdate"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "testAnchor", [action], Object.getOwnPropertyDescriptor(_class.prototype, "testAnchor"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "didInsertAnchor", [action], Object.getOwnPropertyDescriptor(_class.prototype, "didInsertAnchor"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setPopoverRef", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setPopoverRef"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "showToolTip", [action], Object.getOwnPropertyDescriptor(_class.prototype, "showToolTip"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "attachTo", [cached], Object.getOwnPropertyDescriptor(_class.prototype, "attachTo"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "positionToolTip", [action], Object.getOwnPropertyDescriptor(_class.prototype, "positionToolTip"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "hideToolTip", [action], Object.getOwnPropertyDescriptor(_class.prototype, "hideToolTip"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "hasFocusMouseMoveListener", [action], Object.getOwnPropertyDescriptor(_class.prototype, "hasFocusMouseMoveListener"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onKeyUp", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onKeyUp"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onFocus", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onFocus"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onBlur", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onBlur"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onMouseOut", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onMouseOut"), _class.prototype)), _class)));

export { EuiToolTip as default };
//# sourceMappingURL=eui-tool-tip.js.map
