import{icon_styles_default as t}from"./chunk.LMJPQPQT.js";import{getIconLibrary as e,unwatchIcon as r,watchIcon as s}from"./chunk.3WAW4E2K.js";import{watch as i}from"./chunk.C7FWPEOY.js";import{ShoelaceElement as l}from"./chunk.Z7XDKKOD.js";import{__decorateClass as o}from"./chunk.6M63UXML.js";import{html as a}from"../../../../lit/index.js";import{isTemplateResult as n}from"../../../../lit/directive-helpers.js";import{property as c,state as h}from"../../../../lit/decorators.js";var u,d=Symbol(),m=Symbol(),p=new Map,v=class extends l{constructor(){super(...arguments),this.initialRender=!1,this.svg=null,this.label="",this.library="default"}async resolveIcon(t,e){var r;let s;if(null==e?void 0:e.spriteSheet)return a`<svg part="svg">
        <use part="use" href="${t}"></use>
      </svg>`;try{if(s=await fetch(t,{mode:"cors"}),!s.ok)return 410===s.status?d:m}catch(t){return m}try{const t=document.createElement("div");t.innerHTML=await s.text();const e=t.firstElementChild;if("svg"!==(null==(r=null==e?void 0:e.tagName)?void 0:r.toLowerCase()))return d;u||(u=new DOMParser);const i=u.parseFromString(e.outerHTML,"text/html").body.querySelector("svg");return i?(i.part.add("svg"),document.adoptNode(i)):d}catch(t){return d}}connectedCallback(){super.connectedCallback(),s(this)}firstUpdated(){this.initialRender=!0,this.setIcon()}disconnectedCallback(){super.disconnectedCallback(),r(this)}getUrl(){const t=e(this.library);return this.name&&t?t.resolver(this.name):this.src}handleLabelChange(){"string"==typeof this.label&&this.label.length>0?(this.setAttribute("role","img"),this.setAttribute("aria-label",this.label),this.removeAttribute("aria-hidden")):(this.removeAttribute("role"),this.removeAttribute("aria-label"),this.setAttribute("aria-hidden","true"))}async setIcon(){var t;const r=e(this.library),s=this.getUrl();if(!s)return void(this.svg=null);let i=p.get(s);if(i||(i=this.resolveIcon(s,r),p.set(s,i)),!this.initialRender)return;const l=await i;if(l===m&&p.delete(s),s===this.getUrl())if(n(l))this.svg=l;else switch(l){case m:case d:this.svg=null,this.emit("sl-error");break;default:this.svg=l.cloneNode(!0),null==(t=null==r?void 0:r.mutator)||t.call(r,this.svg),this.emit("sl-load")}}render(){return this.svg}};v.styles=t,o([h()],v.prototype,"svg",2),o([c({reflect:!0})],v.prototype,"name",2),o([c()],v.prototype,"src",2),o([c()],v.prototype,"label",2),o([c({reflect:!0})],v.prototype,"library",2),o([i("label")],v.prototype,"handleLabelChange",1),o([i(["name","src","library"])],v.prototype,"setIcon",1);export{v as SlIcon};