import{marked as t}from"./marked.js";export class WCMarkdown extends HTMLElement{static get observedAttributes(){return["src"]}attributeChangedCallback(t,e,s){e!==s&&(this[t]=s)}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t),this.setSrc(t)}get value(){return this.__value}set value(t){this.__value=t,this.setValue()}constructor(){super(),this.__value=""}async connectedCallback(){this.style.display="block",await this.renderSlotMD()}async renderSlotMD(){const t=this.getElementsByTagName("script")[0];if(t){if("wc-content"===t.getAttribute("type")){let e=WCMarkdown.dedentText(t.innerHTML);e=e.replace(/&lt;(\/?script)(.*?)&gt;/g,"<$1$2>"),this.value=e}}else this.textContent&&(this.__value=this.textContent,this.setValue())}async setSrc(t){this.__value=await this.fetchSrc(t),this.setValue()}async fetchSrc(t){return(await fetch(t)).text()}setValue(){let t=this.__value;t=WCMarkdown.prepare(t),t=WCMarkdown.toHtml(t),this.innerHTML=t}static prepare(t){return t.split("\n").map((t=>(t=t.replace("&lt;","<")).replace("&gt;",">"))).join("\n")}static toHtml(e){return t(e)}static dedentText(t){const e=t.split("\n");""===e[0]&&e.splice(0,1);const s=e[0];let r=0;const n="\t"===s[0]?"\t":" ";for(;s[r]===n;)r+=1;const a=[];for(const t of e){let e=t;for(let t=0;t<r&&e[0]===n;t++)e=e.substring(1);a.push(e)}return""===a[a.length-1]&&a.splice(a.length-1,1),a.join("\n")}}customElements.define("wc-markdown",WCMarkdown);