import {
  HasSlotController
} from "./chunk.IVMQWO2L.js";
import {
  getAnimation,
  setDefaultAnimation
} from "./chunk.N4LHE3QK.js";
import {
  waitForEvent
} from "./chunk.B4BZKR24.js";
import {
  LocalizeController
} from "./chunk.X3FE7OHE.js";
import {
  watch
} from "./chunk.X2OQMWGY.js";
import {
  SolidElement,
  customElement
} from "./chunk.H53WQBDK.js";
import {
  animateTo,
  parseDuration,
  stopAnimations
} from "./chunk.HQWAKJBV.js";
import {
  __decorateClass
} from "./chunk.S3NI7NKU.js";

// src/components/tooltip/tooltip.ts
import { css, html } from "lit";
import { property, query } from "lit/decorators.js";
import cx from "classix";
var SdTooltip = class extends SolidElement {
  constructor() {
    super(...arguments);
    this.localize = new LocalizeController(this);
    this.hasSlotController = new HasSlotController(this, "[default]", "content");
    this.size = "lg";
    this.content = "";
    this.placement = "top";
    this.disabled = false;
    this.open = false;
    this.trigger = "click focus";
    this.closeTrigger = "click focus escape";
    this.hoist = false;
    this.handleMouseInteraction = () => {
      this.interactionType = "mouse";
    };
    this.handleKeyboardInteraction = () => {
      this.interactionType = "keyboard";
    };
  }
  connectedCallback() {
    super.connectedCallback();
    this.handleBlur = this.handleBlur.bind(this);
    this.handleClick = this.handleClick.bind(this);
    this.handleFocus = this.handleFocus.bind(this);
    this.handleKeyDown = this.handleKeyDown.bind(this);
    this.handleMouseOver = this.handleMouseOver.bind(this);
    this.handleMouseOut = this.handleMouseOut.bind(this);
    document.addEventListener("mousedown", this.handleMouseInteraction);
    document.addEventListener("keydown", this.handleKeyboardInteraction);
    this.handleDocumentClick = this.handleDocumentClick.bind(this);
    document.addEventListener("click", this.handleDocumentClick);
    this.updateComplete.then(() => {
      this.addEventListener("blur", this.handleBlur, true);
      this.addEventListener("focus", this.handleFocus, true);
      this.addEventListener("click", this.handleClick);
      this.addEventListener("keydown", this.handleKeyDown);
      this.addEventListener("mouseover", this.handleMouseOver);
      this.addEventListener("mouseout", this.handleMouseOut);
    });
  }
  firstUpdated() {
    this.body.hidden = !this.open;
    this.formatDefaultSlot(this.defaultSlot);
    if (this.open) {
      this.popup.active = true;
      this.popup.reposition();
    }
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    this.removeEventListener("blur", this.handleBlur, true);
    this.removeEventListener("focus", this.handleFocus, true);
    this.removeEventListener("click", this.handleClick);
    this.removeEventListener("keydown", this.handleKeyDown);
    this.removeEventListener("mouseover", this.handleMouseOver);
    this.removeEventListener("mouseout", this.handleMouseOut);
    document.removeEventListener("mousedown", this.handleMouseInteraction);
    document.removeEventListener("keydown", this.handleKeyboardInteraction);
    document.removeEventListener("click", this.handleDocumentClick);
  }
  get hasCustomTrigger() {
    return this.hasSlotController.test("[default]");
  }
  // removes empty text nodes from the default slot
  formatDefaultSlot(slot) {
    if (!slot) {
      return;
    }
    const nodes = slot.assignedNodes({ flatten: true });
    nodes.forEach((node) => {
      var _a, _b;
      if (node.nodeType === Node.TEXT_NODE && !((_a = node == null ? void 0 : node.textContent) == null ? void 0 : _a.trim())) {
        (_b = node == null ? void 0 : node.parentNode) == null ? void 0 : _b.removeChild(node);
      }
    });
  }
  handleBlur() {
    if (this.hasCloseTrigger("focus")) {
      this.hide();
    }
  }
  handleClick() {
    if (this.open && this.hasCloseTrigger("click")) {
      this.hide();
    } else if (this.hasTrigger("click")) {
      this.show();
    }
  }
  handleFocus() {
    if (this.hasTrigger("focus")) {
      if (this.interactionType === "keyboard") {
        this.show();
      }
    }
  }
  handleKeyDown(event) {
    if (this.open && event.key === "Escape" && this.hasCloseTrigger("escape")) {
      event.stopPropagation();
      this.hide();
    }
  }
  handleMouseOver() {
    if (this.hasTrigger("hover")) {
      const delay = parseDuration(getComputedStyle(this).getPropertyValue("--show-delay"));
      clearTimeout(this.hoverTimeout);
      this.hoverTimeout = window.setTimeout(() => this.show(), delay);
    }
  }
  handleMouseOut() {
    if (this.hasCloseTrigger("hover")) {
      const delay = parseDuration(getComputedStyle(this).getPropertyValue("--hide-delay"));
      clearTimeout(this.hoverTimeout);
      this.hoverTimeout = window.setTimeout(() => this.hide(), delay);
    }
  }
  handleDocumentClick(event) {
    if (!this.open) return;
    if (this.interactionType === "keyboard") return;
    const path = event.composedPath();
    if (this.hasCloseTrigger("focus") && !path.includes(this) && !path.includes(this.popup)) {
      this.hide();
    }
  }
  hasTrigger(triggerType) {
    const triggers = this.trigger.split(" ");
    return triggers.includes(triggerType);
  }
  hasCloseTrigger(type) {
    const triggers = this.closeTrigger.split(" ");
    return triggers.includes(type);
  }
  async handleOpenChange() {
    if (this.open) {
      if (this.disabled) {
        return;
      }
      this.emit("sd-show");
      await stopAnimations(this.body);
      this.body.hidden = false;
      this.popup.active = true;
      const { keyframes, options } = getAnimation(this, "tooltip.show", { dir: this.localize.dir() });
      await animateTo(this.popup.popup, keyframes, options);
      this.emit("sd-after-show");
    } else {
      this.emit("sd-hide");
      await stopAnimations(this.body);
      const { keyframes, options } = getAnimation(this, "tooltip.hide", { dir: this.localize.dir() });
      await animateTo(this.popup.popup, keyframes, options);
      this.popup.active = false;
      this.body.hidden = true;
      this.emit("sd-after-hide");
    }
  }
  async handleOptionsChange() {
    if (this.hasUpdated) {
      await this.updateComplete;
      this.popup.reposition();
    }
  }
  handleDisabledChange() {
    if (this.disabled && this.open) {
      this.hide();
    }
  }
  /** Shows the tooltip. */
  async show() {
    if (this.open) {
      return void 0;
    }
    this.open = true;
    return waitForEvent(this, "sd-after-show");
  }
  /** Hides the tooltip */
  async hide() {
    if (!this.open) {
      return void 0;
    }
    this.open = false;
    if (this.trigger === "click" || this.trigger === "click focus") {
      this.focus();
    } else {
      this.blur();
    }
    return waitForEvent(this, "sd-after-hide");
  }
  render() {
    const isStart = this.placement.endsWith("-start");
    const isEnd = this.placement.endsWith("-end");
    return html`<sd-6-22-0-popup part="base" exportparts="
          popup:base__popup,
          arrow:base__arrow
        " class="${cx(this.open && "tooltip--open")}" placement="${this.placement}" distance="10" skidding="${{ start: 2, end: -2, default: 0 }[isStart ? "start" : isEnd ? "end" : "default"] * (this.size === "sm" ? -1 : 1)}" strategy="${this.hoist ? "fixed" : "absolute"}" flip shift arrow auto-size="vertical" arrow-padding="0">${this.hasCustomTrigger ? html`<slot slot="anchor"></slot>` : html`<button slot="anchor" aria-describedby="tooltip" class="${cx("flex sd-interactive rounded-full", this.size === "lg" ? "text-xl" : "text-base")}" ?disabled="${this.disabled}" aria-expanded="${this.open}" aria-controls="tooltip"><sd-6-22-0-icon library="_internal" name="info-circle" label="Tooltip" class="${cx(this.disabled && "sd-interactive--disabled")}"></sd-6-22-0-icon></button>`}<slot name="content" part="body" id="tooltip" class="sd-6-22-0-tooltip-color-background sd-6-22-0-tooltip-color-text py-3 px-4 block rounded-none text-sm text-left" role="tooltip" aria-label="${this.content}" aria-live="${this.open ? "polite" : "off"}">${this.content}</slot></sd-6-22-0-popup>`;
  }
};
SdTooltip.styles = [
  ...SolidElement.styles,
  css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */@layer properties;@layer theme,base,components,utilities;
:host{--hide-delay:0ms;--show-delay:150ms;display:contents}sd-6-22-0-popup{--arrow-color:rgba(var(--sd-tooltip-color-background));--arrow-size:10px}sd-6-22-0-popup::part(popup){pointer-events:none;z-index:10;--tw-shadow:var(--sd-shadow);box-shadow:var(--tw-inset-shadow),var(--tw-inset-ring-shadow),var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow)}sd-6-22-0-popup::part(arrow){--tw-shadow:var(--sd-shadow);box-shadow:var(--tw-inset-shadow),var(--tw-inset-ring-shadow),var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow)}sd-6-22-0-popup[placement^=top]::part(popup){transform-origin:bottom}sd-6-22-0-popup[placement^=bottom]::part(popup){transform-origin:top}#tooltip{max-width:var(--max-width)}::slotted([slot=content]){overflow:auto;max-width:var(--auto-size-available-width)!important;max-height:var(--auto-size-available-height)!important}@property --tw-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-shadow-color{syntax:"*";inherits:false}@property --tw-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-inset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-inset-shadow-color{syntax:"*";inherits:false}@property --tw-inset-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-ring-color{syntax:"*";inherits:false}@property --tw-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-inset-ring-color{syntax:"*";inherits:false}@property --tw-inset-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-ring-inset{syntax:"*";inherits:false}@property --tw-ring-offset-width{syntax:"<length>";inherits:false;initial-value:0}@property --tw-ring-offset-color{syntax:"*";inherits:false;initial-value:#fff}@property --tw-ring-offset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}`
];
__decorateClass([
  query("slot:not([name])")
], SdTooltip.prototype, "defaultSlot", 2);
__decorateClass([
  query("#tooltip")
], SdTooltip.prototype, "body", 2);
__decorateClass([
  query("sd-6-22-0-popup")
], SdTooltip.prototype, "popup", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTooltip.prototype, "size", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTooltip.prototype, "content", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTooltip.prototype, "placement", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTooltip.prototype, "disabled", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTooltip.prototype, "open", 2);
__decorateClass([
  property({ type: String, reflect: true })
], SdTooltip.prototype, "trigger", 2);
__decorateClass([
  property({ type: String, attribute: "close-trigger", reflect: true })
], SdTooltip.prototype, "closeTrigger", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], SdTooltip.prototype, "hoist", 2);
__decorateClass([
  watch("open", { waitUntilFirstUpdate: true })
], SdTooltip.prototype, "handleOpenChange", 1);
__decorateClass([
  watch(["content", "hoist", "placement"])
], SdTooltip.prototype, "handleOptionsChange", 1);
__decorateClass([
  watch("disabled")
], SdTooltip.prototype, "handleDisabledChange", 1);
SdTooltip = __decorateClass([
  customElement("sd-6-22-0-tooltip")
], SdTooltip);
setDefaultAnimation("tooltip.show", {
  keyframes: [{ opacity: 0 }, { opacity: 1 }],
  options: { duration: "var(--sd-duration-fast, 150)", easing: "ease-in-out" }
});
setDefaultAnimation("tooltip.hide", {
  keyframes: [{ opacity: 1 }, { opacity: 0 }],
  options: { duration: "var(--sd-duration-fast, 150)", easing: "ease-in-out" }
});

export {
  SdTooltip
};
