import { n as i, C as b } from "../chunks/lib-cortex-element.CVMmyPMC.js";
import { c as g } from "../chunks/component.styles.CRO4Odto.js";
import { p as c, s as u, a as m } from "../chunks/animate.El-MpOws.js";
import { c as w } from "../chunks/custom-element.L4WJXn1j.js";
import { w as y } from "../chunks/event.mFzZi4sr.js";
import { L as x } from "../chunks/i18n.BABe9hN4.js";
import { w as l } from "../chunks/watch.BCJD77bD.js";
import { i as C, x as k } from "../chunks/lit-element.jLBm65_O.js";
import { e as d } from "../chunks/query.BBf1UFkC.js";
import { e as O } from "../chunks/class-map.BiVq-cVR.js";
import { s as v, g as f } from "../chunks/animation-registry.HmpwNuGH.js";
import B from "./popup.js";
const D = C`:host{--hide-delay:0s;--show-delay:.5s;display:contents}.tooltip{--arrow-size:var(--cx-tooltip-arrow-size);--arrow-color:var(--cx-tooltip-background-color)}.tooltip::part(popup){z-index:var(--cx-z-index-tooltip)}.tooltip[placement^=top]::part(popup){transform-origin:bottom}.tooltip[placement^=bottom]::part(popup){transform-origin:top}.tooltip[placement^=left]::part(popup){transform-origin:100%}.tooltip[placement^=right]::part(popup){transform-origin:0}.tooltip--disabled-hover::part(popup){pointer-events:none}.tooltip__body{width:max-content;max-width:var(--max-width,min(80vw, 40rem));border-radius:var(--cx-tooltip-border-radius);background-color:var(--cx-tooltip-background-color);font-family:var(--cx-tooltip-font-family);font-size:var(--cx-tooltip-font-size);font-weight:var(--cx-tooltip-font-weight);line-height:var(--cx-tooltip-line-height);text-align:start;white-space:normal;color:var(--cx-tooltip-color);padding:var(--cx-tooltip-padding);pointer-events:none;-webkit-user-select:none;user-select:none;overflow-wrap:break-word;display:block}`;
var E = Object.defineProperty, T = Object.getOwnPropertyDescriptor, e = (o, s, n, a) => {
  for (var r = a > 1 ? void 0 : a ? T(s, n) : s, p = o.length - 1, h; p >= 0; p--)
    (h = o[p]) && (r = (a ? h(s, n, r) : h(r)) || r);
  return a && r && E(s, n, r), r;
};
let t = class extends b {
  constructor() {
    super(), this.localize = new x(this), this.content = "", this.placement = "top", this.disabled = !1, this.distance = 8, this.open = !1, this.skidding = 0, this.trigger = "hover focus", this.hoist = !1, this.disabledHoverBridge = !1, this.handleBlur = this.handleBlur.bind(this), this.handleFocus = this.handleFocus.bind(this), this.handleClick = this.handleClick.bind(this), this.handleMouseOver = this.handleMouseOver.bind(this), this.handleMouseOut = this.handleMouseOut.bind(this), this.handleDocumentKeyDown = this.handleDocumentKeyDown.bind(this);
  }
  runConnectedCallback() {
    this.addEventListener("blur", this.handleBlur, !0), this.addEventListener("focus", this.handleFocus, !0), this.addEventListener("click", this.handleClick), this.addEventListener("mouseover", this.handleMouseOver), this.addEventListener("mouseout", this.handleMouseOut);
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.removeEventListener("blur", this.handleBlur, !0), this.removeEventListener("focus", this.handleFocus, !0), this.removeEventListener("click", this.handleClick), this.removeEventListener("mouseover", this.handleMouseOver), this.removeEventListener("mouseout", this.handleMouseOut), this.closeWatcher?.destroy(), document.removeEventListener("keydown", this.handleDocumentKeyDown);
  }
  runFirstUpdated() {
    this.body.hidden = !this.open, this.open && (this.popup.active = !0, this.popup.reposition());
  }
  handleBlur() {
    this.hasTrigger("focus") && this.hide();
  }
  handleClick() {
    this.hasTrigger("click") && (this.open ? this.hide() : this.show());
  }
  handleFocus() {
    this.hasTrigger("focus") && this.show();
  }
  handleDocumentKeyDown(o) {
    o.key === "Escape" && (o.stopPropagation(), this.hide());
  }
  handleMouseOver() {
    if (this.hasTrigger("hover")) {
      const o = c(
        getComputedStyle(this).getPropertyValue("--show-delay")
      );
      clearTimeout(this.hoverTimeout), this.hoverTimeout = window.setTimeout(() => this.show(), o);
    }
  }
  handleMouseOut() {
    if (this.hasTrigger("hover")) {
      const o = c(
        getComputedStyle(this).getPropertyValue("--hide-delay")
      );
      clearTimeout(this.hoverTimeout), this.hoverTimeout = window.setTimeout(() => this.hide(), o);
    }
  }
  hasTrigger(o) {
    return this.trigger.split(" ").includes(o);
  }
  async handleOpenChange() {
    if (this.open) {
      if (this.disabled)
        return;
      this.emit("cx-show"), "CloseWatcher" in window ? (this.closeWatcher?.destroy(), this.closeWatcher = new CloseWatcher(), this.closeWatcher.onclose = () => {
        this.hide();
      }) : document.addEventListener("keydown", this.handleDocumentKeyDown), await u(this.body), this.body.hidden = !1, this.popup.active = !0;
      const { keyframes: o, options: s } = f(this, "tooltip.show", {
        dir: this.localize.dir()
      });
      await m(this.popup.popup, o, s), this.popup.reposition(), this.emit("cx-after-show");
    } else {
      this.emit("cx-hide"), this.closeWatcher?.destroy(), document.removeEventListener("keydown", this.handleDocumentKeyDown), await u(this.body);
      const { keyframes: o, options: s } = f(this, "tooltip.hide", {
        dir: this.localize.dir()
      });
      await m(this.popup.popup, o, s), this.popup.active = !1, this.body.hidden = !0, this.emit("cx-after-hide");
    }
  }
  async handleOptionsChange() {
    this.hasUpdated && (await this.updateComplete, this.popup.reposition());
  }
  handleDisabledChange() {
    this.disabled && this.open && this.hide();
  }
  /** Shows the tooltip. */
  async show() {
    if (!this.open)
      return this.open = !0, y(this, "cx-after-show");
  }
  /** Hides the tooltip */
  async hide() {
    if (this.open)
      return this.open = !1, y(this, "cx-after-hide");
  }
  //
  // NOTE: Tooltip is a bit unique in that we're using aria-live instead of aria-labelledby to trick screen readers into
  // announcing the content. It works really well, but it violates an accessibility rule. We're also adding the
  // aria-describedby attribute to a slot, which is required by <cx-popup> to correctly locate the first assigned
  // element, otherwise positioning is incorrect.
  //
  render() {
    return k`<cx-popup part="base" exportparts="
          popup:base__popup,
          arrow:base__arrow
        " class=${O({
      tooltip: !0,
      "tooltip--disabled-hover": this.disabledHoverBridge,
      "tooltip--open": this.open
    })} placement=${this.placement} distance=${this.distance} skidding=${this.skidding} strategy=${this.hoist ? "overlay" : "absolute"} flip shift arrow ?hover-bridge=${!this.disabledHoverBridge} .anchor=${this.anchor} .flipBoundary=${this.flipBoundary} .shiftBoundary=${this.shiftBoundary}><slot slot="anchor" aria-describedby="tooltip"></slot><div part="body" id="tooltip" class="tooltip__body" role="tooltip" aria-live=${this.open ? "polite" : "off"}><slot name="content">${this.content}</slot></div></cx-popup>`;
  }
};
t.styles = [g, D];
t.dependencies = { "cx-popup": B };
e([
  d("slot:not([name])")
], t.prototype, "defaultSlot", 2);
e([
  d(".tooltip__body")
], t.prototype, "body", 2);
e([
  d("cx-popup")
], t.prototype, "popup", 2);
e([
  i()
], t.prototype, "anchor", 2);
e([
  i()
], t.prototype, "content", 2);
e([
  i()
], t.prototype, "placement", 2);
e([
  i({ reflect: !0, type: Boolean })
], t.prototype, "disabled", 2);
e([
  i({ type: Number })
], t.prototype, "distance", 2);
e([
  i({ reflect: !0, type: Boolean })
], t.prototype, "open", 2);
e([
  i({ type: Number })
], t.prototype, "skidding", 2);
e([
  i()
], t.prototype, "trigger", 2);
e([
  i({ type: Boolean })
], t.prototype, "hoist", 2);
e([
  i({ attribute: "disabled-hover-bridge", reflect: !0, type: Boolean })
], t.prototype, "disabledHoverBridge", 2);
e([
  i({ attribute: "flip-boundary", type: Object })
], t.prototype, "flipBoundary", 2);
e([
  i({ attribute: "shift-boundary", type: Object })
], t.prototype, "shiftBoundary", 2);
e([
  l("open", { waitUntilFirstUpdate: !0 })
], t.prototype, "handleOpenChange", 1);
e([
  l(["content", "distance", "hoist", "placement", "skidding"])
], t.prototype, "handleOptionsChange", 1);
e([
  l("disabled")
], t.prototype, "handleDisabledChange", 1);
t = e([
  w("cx-tooltip")
], t);
v("tooltip.show", {
  keyframes: [
    { opacity: 0, scale: 0.8 },
    { opacity: 1, scale: 1 }
  ],
  options: { duration: 100, easing: "ease" }
});
v("tooltip.hide", {
  keyframes: [
    { opacity: 1, scale: 1 },
    { opacity: 0, scale: 0.8 }
  ],
  options: { duration: 100, easing: "ease" }
});
export {
  t as default
};
