import { n as r, C as y } from "../chunks/lib-cortex-element.CVMmyPMC.js";
import { c as m } from "../chunks/component.styles.CRO4Odto.js";
import { c as f } from "../chunks/custom-element.L4WJXn1j.js";
import { L as g } from "../chunks/i18n.BABe9hN4.js";
import { H as x } from "../chunks/slot.j5oheLJC.js";
import { w as v } from "../chunks/watch.BCJD77bD.js";
import { i as _, x as b } from "../chunks/lit-element.jLBm65_O.js";
import { r as u } from "../chunks/state.CSDxrqLd.js";
import { e as p } from "../chunks/query.BBf1UFkC.js";
import { e as w } from "../chunks/class-map.BiVq-cVR.js";
import { n as C } from "../chunks/when.Dr1es41R.js";
import { s as d, g as h } from "../chunks/animation-registry.HmpwNuGH.js";
import z from "./icon.js";
import k from "./tooltip.js";
const I = _`:host{--error-color:var(--cx-color-danger);--success-color:var(--cx-color-success);--background-color:light-dark(var(--cx-color-primary),var(--cx-color-primary));--color:light-dark(var(--cx-color-neutral-0),var(--cx-color-neutral-1000));cursor:pointer;width:auto;max-width:100%;line-height:normal;display:inline-block;position:relative}.copy-button{border-style:solid;border-width:var(--cx-input-border-width);width:100%;max-width:100%;font-family:var(--cx-input-font-family);font-weight:var(--cx-font-weight-regular);text-transform:var(--cx-button-text-transform,none);-webkit-user-select:none;user-select:none;white-space:nowrap;vertical-align:middle;transition:var(--cx-transition-x-fast) background-color, var(--cx-transition-x-fast) color, var(--cx-transition-x-fast) border, var(--cx-transition-x-fast) box-shadow;cursor:inherit;justify-content:center;align-items:stretch;padding:0;text-decoration:none;display:inline-flex}.copy-button__label{max-width:100%}.copy-button::-moz-focus-inner{border:0}.copy-button:focus-visible{outline:var(--cx-focus-ring);outline-offset:var(--cx-focus-ring-offset)}.copy-button{color:var(--cx-color-neutral);background-color:#0000;border-color:#0000}.copy-button.copy-button--has-label:hover:not(.copy-button--disabled),.copy-button.copy-button--has-label.copy-button--focused:not(.copy-button--disabled){background-color:var(--cx-color-neutral-100);border-color:var(--cx-color-neutral-100);color:var(--cx-color-neutral)}.copy-button.copy-button--has-label:active:not(.copy-button--disabled){background-color:var(--cx-color-neutral-200);border-color:var(--cx-color-neutral-200);color:var(--cx-color-neutral)}.copy-button--disabled{opacity:.5;cursor:default}.copy-button--disabled *{pointer-events:none}.copy-button__icon-wrapper{background:0 0;border:none;grid-template-columns:1fr;display:inline-grid}.copy-button__icon-wrapper slot{grid-row-start:1;grid-column-start:1;display:inline-flex}.copy-button__icon{pointer-events:none;flex:none;align-items:center;display:flex}.copy-button--copy-success .copy-button__icon{color:var(--success-color)}.copy-button--copy-error .copy-button__icon{color:var(--error-color)}.copy-button__label{justify-content:center;align-items:center;line-height:1;display:inline-flex}.copy-button{background-color:var(--button-background-color,unset)}.copy-button--small{height:auto;min-height:var(--cx-input-height-small);font-size:var(--button-font-size,var(--cx-button-font-size-medium));border-radius:var(--cx-button-border-radius-small)}.copy-button--small .copy-button__icon{--font-size:var(--button-font-size,var(--cx-font-size-medium))}.copy-button--medium{height:auto;min-height:var(--cx-input-height-medium);font-size:var(--button-font-size,var(--cx-button-font-size-medium));border-radius:var(--cx-button-border-radius-medium)}.copy-button--large{height:auto;min-height:var(--cx-input-height-large);font-size:var(--button-font-size,var(--cx-button-font-size-medium));border-radius:var(--cx-button-border-radius-large)}.copy-button--x-large{height:auto;min-height:var(--cx-input-height-x-large);font-size:var(--button-font-size,var(--cx-button-font-size-medium));border-radius:var(--cx-button-border-radius-large)}.copy-button--small{padding:var(--button-padding,0 var(--cx-spacing-x-small))}.copy-button--medium{padding:var(--button-padding,0 var(--cx-spacing-medium))}.copy-button--large{padding:var(--button-padding,0 var(--cx-spacing-large))}.copy-button--x-large{padding:var(--button-padding,0 var(--cx-spacing-x-large))}.copy-button--has-label:not(.copy-button--no-icon).copy-button--small .copy-button__label,.copy-button--has-label:not(.copy-button--no-icon).copy-button--medium .copy-button__label,.copy-button--has-label:not(.copy-button--no-icon).copy-button--large .copy-button__label,.copy-button--has-label:not(.copy-button--no-icon).copy-button--x-large .copy-button__label{padding-inline-start:var(--cx-spacing-x-small)}`;
var L = Object.defineProperty, F = Object.getOwnPropertyDescriptor, o = (e, a, c, n) => {
  for (var i = n > 1 ? void 0 : n ? F(a, c) : a, s = e.length - 1, l; s >= 0; s--)
    (l = e[s]) && (i = (n ? l(a, c, i) : l(i)) || i);
  return n && i && L(a, c, i), i;
};
let t = class extends y {
  constructor() {
    super(...arguments), this.localize = new g(this), this.hasSlotController = new x(this, "[default]"), this.hasFocus = !1, this.isCopying = !1, this.status = "rest", this.value = "", this.size = "medium", this.from = "", this.disabled = !1, this.label = "", this.copyLabel = "", this.focusCopyLabel = "", this.successLabel = "", this.errorLabel = "", this.feedbackDuration = 1e3, this.tooltipPlacement = "top", this.tooltipDistance = 8, this.hoist = !1, this.getValue = void 0, this.noIcon = !1, this.writeToClipboard = async (e) => {
      await navigator.clipboard.writeText(e);
    };
  }
  runFirstUpdated() {
    this.tooltip.content = this.getTooltipContent();
  }
  handleBlur() {
    this.hasFocus = !1, this.emit("cx-blur");
  }
  handleFocus() {
    this.hasFocus = !0, this.emit("cx-focus");
  }
  handleKeyDown(e) {
    (e.ctrlKey || e.metaKey) && e.key === "c" && (e.preventDefault(), e.stopPropagation(), this.handleCopy());
  }
  async handleCopy() {
    if (this.disabled || this.isCopying)
      return;
    this.isCopying = !0;
    let e = this.value;
    if (this.from) {
      const a = this.getRootNode(), c = this.from.includes("."), n = this.from.includes("[") && this.from.includes("]");
      let i = this.from, s = "";
      c ? [i, s] = this.from.trim().split(".") : n && ([i, s] = this.from.trim().replace(/\]$/, "").split("["));
      const l = "getElementById" in a ? a.getElementById(i) : null;
      l ? n ? e = l.getAttribute(s) || "" : c ? e = l[s] || "" : e = l.textContent || "" : (this.showStatus("error"), this.emit("cx-error"));
    }
    if (this.getValue && (e = this.getValue()), !e)
      this.showStatus("error"), this.emit("cx-error");
    else
      try {
        await this.writeToClipboard(e), this.showStatus("success"), this.emit("cx-copy", {
          detail: {
            value: e
          }
        });
      } catch {
        this.showStatus("error"), this.emit("cx-error");
      }
  }
  getTooltipContent() {
    return (this.hasFocus && this.button?.matches(":focus-visible") ? this.focusCopyLabel : this.copyLabel) || this.localize.term("copy");
  }
  async showStatus(e) {
    const a = this.successLabel || this.localize.term("copied"), c = this.errorLabel || this.localize.term("error"), n = e === "success" ? this.successIcon : this.errorIcon, i = h(this, "copy.in", { dir: "ltr" }), s = h(this, "copy.out", { dir: "ltr" });
    this.tooltip.content = e === "success" ? a : c, this.noIcon || (await this.copyIcon.animate(
      s.keyframes,
      s.options
    ).finished, this.copyIcon.hidden = !0, this.status = e, n.hidden = !1, await n.animate(i.keyframes, i.options).finished), setTimeout(async () => {
      this.noIcon || (await n.animate(s.keyframes, s.options).finished, n.hidden = !0, this.status = "rest", this.copyIcon.hidden = !1, await this.copyIcon.animate(
        i.keyframes,
        i.options
      ).finished), this.tooltip.content = this.getTooltipContent(), this.isCopying = !1;
    }, this.feedbackDuration);
  }
  async handleHasFocusChange() {
    await this.updateComplete, this.tooltip.content = this.getTooltipContent();
  }
  render() {
    return b`<cx-tooltip placement=${this.tooltipPlacement} distance=${this.tooltipDistance} ?disabled=${this.disabled} ?hoist=${this.hoist} exportparts="
          base:tooltip__base,
          base__popup:tooltip__base__popup,
          base__arrow:tooltip__base__arrow,
          body:tooltip__body
        "><button class=${w({
      "copy-button": !0,
      "copy-button--copy-error": this.status === "error",
      "copy-button--copy-success": this.status === "success",
      "copy-button--disabled": this.disabled,
      "copy-button--focused": this.hasFocus,
      "copy-button--has-label": this.hasSlotController.test("[default]"),
      "copy-button--large": this.size === "large",
      "copy-button--medium": this.size === "medium",
      "copy-button--no-icon": this.noIcon,
      "copy-button--small": this.size === "small",
      "copy-button--x-large": this.size === "x-large"
    })} aria-label=${this.label || this.localize.term("copy")} part="button" type="button" ?disabled=${this.disabled} @click=${this.handleCopy} @keydown=${this.handleKeyDown} @blur=${this.handleBlur} @focus=${this.handleFocus}>${C(
      !this.noIcon,
      () => b`<div part="icon-wrapper" class="copy-button__icon-wrapper"><slot part="copy-icon" name="copy-icon" class="copy-button__icon"><cx-icon name="content_copy"></cx-icon></slot><slot part="success-icon" name="success-icon" hidden class="copy-button__icon"><cx-icon name="check"></cx-icon></slot><slot part="error-icon" name="error-icon" hidden class="copy-button__icon"><cx-icon name="close"></cx-icon></slot></div>`
    )}<slot part="label" class="copy-button__label"></slot></button></cx-tooltip>`;
  }
};
t.styles = [m, I];
t.dependencies = {
  "cx-icon": z,
  "cx-tooltip": k
};
o([
  p('slot[name="copy-icon"]')
], t.prototype, "copyIcon", 2);
o([
  p('slot[name="success-icon"]')
], t.prototype, "successIcon", 2);
o([
  p('slot[name="error-icon"]')
], t.prototype, "errorIcon", 2);
o([
  p("cx-tooltip")
], t.prototype, "tooltip", 2);
o([
  p("button")
], t.prototype, "button", 2);
o([
  u()
], t.prototype, "hasFocus", 2);
o([
  u()
], t.prototype, "isCopying", 2);
o([
  u()
], t.prototype, "status", 2);
o([
  r()
], t.prototype, "value", 2);
o([
  r({ reflect: !0 })
], t.prototype, "size", 2);
o([
  r()
], t.prototype, "from", 2);
o([
  r({ reflect: !0, type: Boolean })
], t.prototype, "disabled", 2);
o([
  r({ type: String })
], t.prototype, "label", 2);
o([
  r({ attribute: "copy-label", type: String })
], t.prototype, "copyLabel", 2);
o([
  r({ attribute: "focus-copy-label", type: String })
], t.prototype, "focusCopyLabel", 2);
o([
  r({ attribute: "success-label", type: String })
], t.prototype, "successLabel", 2);
o([
  r({ attribute: "error-label", type: String })
], t.prototype, "errorLabel", 2);
o([
  r({ attribute: "feedback-duration", type: Number })
], t.prototype, "feedbackDuration", 2);
o([
  r({ attribute: "tooltip-placement" })
], t.prototype, "tooltipPlacement", 2);
o([
  r({ attribute: "tooltip-distance", type: Number })
], t.prototype, "tooltipDistance", 2);
o([
  r({ type: Boolean })
], t.prototype, "hoist", 2);
o([
  r({ attribute: "get-value", type: Function })
], t.prototype, "getValue", 2);
o([
  r({ attribute: "no-icon", reflect: !0, type: Boolean })
], t.prototype, "noIcon", 2);
o([
  r({ attribute: "write-to-clipboard", type: Function })
], t.prototype, "writeToClipboard", 2);
o([
  v("hasFocus", { waitUntilFirstUpdate: !0 })
], t.prototype, "handleHasFocusChange", 1);
t = o([
  f("cx-copy-button")
], t);
d("copy.in", {
  keyframes: [
    { opacity: ".25", scale: ".25" },
    { opacity: "1", scale: "1" }
  ],
  options: { duration: 100 }
});
d("copy.out", {
  keyframes: [
    { opacity: "1", scale: "1" },
    { opacity: "0", scale: ".25" }
  ],
  options: { duration: 100 }
});
export {
  t as default
};
