import "../chunks/button.CAa1kvwe.js";
import "./tooltip.js";
import "./resize-observer.js";
import { n as r, C as b } from "../chunks/lib-cortex-element.CVMmyPMC.js";
import { c as g } from "../chunks/component.styles.CRO4Odto.js";
import { i as f } from "../chunks/browser.BHOCIF_A.js";
import { c as w } from "../chunks/custom-element.L4WJXn1j.js";
import { d as x } from "../chunks/debounce.DaHuiSGU.js";
import { i as v, x as h } from "../chunks/lit-element.jLBm65_O.js";
import { r as u } from "../chunks/state.CSDxrqLd.js";
import { e as m } from "../chunks/query.BBf1UFkC.js";
const C = v`:host{--toggler-color:var(--cx-color-primary)}.content{text-overflow:ellipsis;white-space:normal;overflow-wrap:break-word;-webkit-box-orient:vertical;display:-webkit-box;overflow:hidden}.toggler{margin-top:var(--cx-spacing-2x-small);display:block}.toggler::part(base){color:var(--toggler-color);font-weight:var(--cx-font-weight-medium);text-transform:none;width:auto}.toggler::part(label){padding-left:0}`;
var T = Object.defineProperty, $ = Object.getOwnPropertyDescriptor, e = (i, s, n, a) => {
  for (var l = a > 1 ? void 0 : a ? $(s, n) : s, o = i.length - 1, p; o >= 0; o--)
    (p = i[o]) && (l = (a ? p(s, n, l) : p(l)) || l);
  return a && l && T(s, n, l), l;
};
let t = class extends b {
  constructor() {
    super(...arguments), this.lines = 1, this.open = !1, this.showMore = !1, this.showMoreText = "View more...", this.showLessText = "View less...", this.disabledTooltip = !1, this.disabledHoverBridge = !1, this.placement = "top", this.isClamped = !1, this.tooltipContent = "", this.calculated = !1;
  }
  connectedUpdatedCallback() {
    this.updateTooltipContent(), this.mutationObserver = new MutationObserver(() => {
      this.updateTooltipContent();
    }), this.mutationObserver.observe(this, {
      characterData: !0,
      childList: !0,
      subtree: !0
    });
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.mutationObserver?.disconnect();
  }
  async updateTooltipContent() {
    const s = this.shadowRoot.querySelector("slot").assignedNodes({ flatten: !0 }), n = async (o) => {
      if (o.nodeType === Node.ELEMENT_NODE)
        try {
          let c = o.childNodes.length > 0 || o.shadowRoot ? "" : o.textContent ?? "";
          if (await customElements.whenDefined(o.nodeName.toLowerCase()), o.shadowRoot) {
            const d = o.shadowRoot?.childNodes ?? [];
            for (const y of d)
              c += await n(y);
          }
          for (const d of o.childNodes)
            c += await n(d);
          return c.trim();
        } catch {
        }
      return o.nodeType !== Node.COMMENT_NODE ? o.textContent ?? "" : "";
    }, a = [];
    for (const o of s) {
      const p = await n(o);
      a.push(p);
    }
    const l = a.join("");
    this.tooltipContent = l.trim();
  }
  /**
   * Safari has a bug where -webkit-line-clamp with mixed content (text + elements)
   * can lose text nodes when tooltip hides. This forces a reflow to fix it.
   */
  handleTooltipAfterHide(i) {
    i.currentTarget === this.tooltipElement && requestAnimationFrame(() => {
      if (!this.content)
        return;
      const s = this.content.style.display;
      this.content.style.display = "none", this.content.getBoundingClientRect(), this.content.style.display = s;
    });
  }
  /**
   * This works by comparing the scrollHeight of the element to its clientHeight. When the text is clamped,
   * the scroll height extends below the element's visible area (clientHeight). When the text is not clamped,
   * the scroll height is the same as the clientHeight.
   */
  isTextClamped(i) {
    return i.scrollHeight > i.clientHeight;
  }
  handleResize() {
    this.content && (this.isClamped = this.isTextClamped(this.content), this.calculated = !0);
  }
  toggleShowingMore() {
    this.open = !this.open;
  }
  render() {
    const i = h`<div part="base"><cx-resize-observer @cx-resize=${this.handleResize}><span class="content" part="content" style="-webkit-line-clamp: ${this.lines}"><slot></slot></span></cx-resize-observer></div>`;
    let s = null;
    return this.isClamped && (s = h`<cx-button part="toggler" size="small" variant="text" class="toggler" @click=${this.toggleShowingMore}>${this.open ? this.showLessText : this.showMoreText}</cx-button>`), this.showMore ? h`
        ${this.open && this.calculated ? h`<slot></slot>` : i}
        ${s}
      ` : this.disabledTooltip ? i : this.tooltip ? h`<cx-tooltip part="tooltip" class="tooltip" content=${this.tooltip} hoist ?disabled-hover-bridge=${this.disabledHoverBridge} .flipBoundary=${this.flipBoundary} .shiftBoundary=${this.shiftBoundary} placement=${this.placement} @cx-after-hide=${f() ? this.handleTooltipAfterHide : null}>${i}</cx-tooltip>` : h`
      ${this.isClamped ? h`<cx-tooltip part="tooltip" class="tooltip" content=${this.tooltipContent} hoist ?disabled-hover-bridge=${this.disabledHoverBridge} .flipBoundary=${this.flipBoundary} .shiftBoundary=${this.shiftBoundary} placement=${this.placement} @cx-after-hide=${f() ? this.handleTooltipAfterHide : null}>${i}</cx-tooltip>` : i}
    `;
  }
};
t.styles = [g, C];
e([
  r({ type: Number })
], t.prototype, "lines", 2);
e([
  r({ reflect: !0, type: Boolean })
], t.prototype, "open", 2);
e([
  r({ attribute: "show-more", type: Boolean })
], t.prototype, "showMore", 2);
e([
  r({ attribute: "show-more-text", type: String })
], t.prototype, "showMoreText", 2);
e([
  r({ attribute: "show-less-text", type: String })
], t.prototype, "showLessText", 2);
e([
  r({ type: String })
], t.prototype, "tooltip", 2);
e([
  r({ attribute: "disabled-tooltip", type: Boolean })
], t.prototype, "disabledTooltip", 2);
e([
  r({ attribute: "disabled-hover-bridge", reflect: !0, type: Boolean })
], t.prototype, "disabledHoverBridge", 2);
e([
  r({ attribute: "flip-boundary", type: Object })
], t.prototype, "flipBoundary", 2);
e([
  r({ attribute: "shift-boundary", type: Object })
], t.prototype, "shiftBoundary", 2);
e([
  r()
], t.prototype, "placement", 2);
e([
  u()
], t.prototype, "isClamped", 2);
e([
  u()
], t.prototype, "tooltipContent", 2);
e([
  u()
], t.prototype, "calculated", 2);
e([
  m(".content")
], t.prototype, "content", 2);
e([
  m(".tooltip")
], t.prototype, "tooltipElement", 2);
e([
  x(100)
], t.prototype, "handleResize", 1);
t = e([
  w("cx-line-clamp")
], t);
export {
  t as default
};
