import{SlTooltip as t}from"./chunk.5SZQCM5C.js";import{copy_button_styles_default as o}from"./chunk.PH5IIYTE.js";import{getAnimation as s,setDefaultAnimation as e}from"./chunk.STXFIMXZ.js";import{LocalizeController as i}from"./chunk.2A3352EO.js";import{SlIcon as r}from"./chunk.M4HIRIJ5.js";import{ShoelaceElement as c}from"./chunk.Z7XDKKOD.js";import{__decorateClass as a}from"./chunk.6M63UXML.js";import{classMap as p}from"../../../../lit/directives/class-map.js";import{html as n}from"../../../../lit/index.js";import{property as l,query as h,state as y}from"../../../../lit/decorators.js";var m=class extends c{constructor(){super(...arguments),this.localize=new i(this),this.isCopying=!1,this.status="rest",this.value="",this.from="",this.disabled=!1,this.copyLabel="",this.successLabel="",this.errorLabel="",this.feedbackDuration=1e3,this.tooltipPlacement="top",this.hoist=!1}async handleCopy(){if(this.disabled||this.isCopying)return;this.isCopying=!0;let t=this.value;if(this.from){const o=this.getRootNode(),s=this.from.includes("."),e=this.from.includes("[")&&this.from.includes("]");let i=this.from,r="";s?[i,r]=this.from.trim().split("."):e&&([i,r]=this.from.trim().replace(/\]$/,"").split("["));const c="getElementById"in o?o.getElementById(i):null;c?t=e?c.getAttribute(r)||"":s?c[r]||"":c.textContent||"":(this.showStatus("error"),this.emit("sl-error"))}if(t)try{await navigator.clipboard.writeText(t),this.showStatus("success"),this.emit("sl-copy",{detail:{value:t}})}catch(t){this.showStatus("error"),this.emit("sl-error")}else this.showStatus("error"),this.emit("sl-error")}async showStatus(t){const o=this.copyLabel||this.localize.term("copy"),e=this.successLabel||this.localize.term("copied"),i=this.errorLabel||this.localize.term("error"),r="success"===t?this.successIcon:this.errorIcon,c=s(this,"copy.in",{dir:"ltr"}),a=s(this,"copy.out",{dir:"ltr"});this.tooltip.content="success"===t?e:i,await this.copyIcon.animate(a.keyframes,a.options).finished,this.copyIcon.hidden=!0,this.status=t,r.hidden=!1,await r.animate(c.keyframes,c.options).finished,setTimeout((async()=>{await r.animate(a.keyframes,a.options).finished,r.hidden=!0,this.status="rest",this.copyIcon.hidden=!1,await this.copyIcon.animate(c.keyframes,c.options).finished,this.tooltip.content=o,this.isCopying=!1}),this.feedbackDuration)}render(){const t=this.copyLabel||this.localize.term("copy");return n`
      <sl-tooltip
        class=${p({"copy-button":!0,"copy-button--success":"success"===this.status,"copy-button--error":"error"===this.status})}
        content=${t}
        placement=${this.tooltipPlacement}
        ?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="copy-button__button"
          part="button"
          type="button"
          ?disabled=${this.disabled}
          @click=${this.handleCopy}
        >
          <slot part="copy-icon" name="copy-icon">
            <sl-icon library="system" name="copy"></sl-icon>
          </slot>
          <slot part="success-icon" name="success-icon" hidden>
            <sl-icon library="system" name="check"></sl-icon>
          </slot>
          <slot part="error-icon" name="error-icon" hidden>
            <sl-icon library="system" name="x-lg"></sl-icon>
          </slot>
        </button>
      </sl-tooltip>
    `}};m.styles=o,m.dependencies={"sl-icon":r,"sl-tooltip":t},a([h('slot[name="copy-icon"]')],m.prototype,"copyIcon",2),a([h('slot[name="success-icon"]')],m.prototype,"successIcon",2),a([h('slot[name="error-icon"]')],m.prototype,"errorIcon",2),a([h("sl-tooltip")],m.prototype,"tooltip",2),a([y()],m.prototype,"isCopying",2),a([y()],m.prototype,"status",2),a([l()],m.prototype,"value",2),a([l()],m.prototype,"from",2),a([l({type:Boolean,reflect:!0})],m.prototype,"disabled",2),a([l({attribute:"copy-label"})],m.prototype,"copyLabel",2),a([l({attribute:"success-label"})],m.prototype,"successLabel",2),a([l({attribute:"error-label"})],m.prototype,"errorLabel",2),a([l({attribute:"feedback-duration",type:Number})],m.prototype,"feedbackDuration",2),a([l({attribute:"tooltip-placement"})],m.prototype,"tooltipPlacement",2),a([l({type:Boolean})],m.prototype,"hoist",2),e("copy.in",{keyframes:[{scale:".25",opacity:".25"},{scale:"1",opacity:"1"}],options:{duration:100}}),e("copy.out",{keyframes:[{scale:"1",opacity:"1"},{scale:".25",opacity:"0"}],options:{duration:100}});export{m as SlCopyButton};