import{ESGlobalBridgeStore as e}from"../../es-global-bridge/es-global-bridge.js";import{FileSystemBroker as s,FileSystemBrokerSingleton as t}from"../file-system-broker.js";class XLSXFileSystemBroker extends s{static get tag(){return"xlsx-file-system-broker"}constructor(){super(),this.XLSX=null,this.libPath=new URL("./xlsx-file-system-broker.js",import.meta.url).href+"/../",this.libPath+="xlsx/",e.load("xlsx",this.libPath+"dist/xlsx.full.min.js").then((()=>{globalThis.XLSX&&(this.XLSX=globalThis.XLSX,this.dispatchEvent(new CustomEvent("xlsx-ready",{bubbles:!0,composed:!0,cancelable:!1,detail:this})))})),this.XW={msg:"xlsx",worker:this.libPath+"xlsxworker.js"}}workbookFromJSON(e){const s=this.XLSX.utils.book_new();for(const t in e){let r=this.XLSX.utils.json_to_sheet(e[t],{skipHeader:!0});this.XLSX.utils.book_append_sheet(s,r,t)}return this.XLSX.write(s,{bookType:"xlsx",bookSST:!1,type:"array"})}__toJSON(e,s){var t={};return e.SheetNames.forEach((s=>{var r=this.XLSX.utils.sheet_to_json(e.Sheets[s],{header:1,blankrows:!1,raw:!1,dateNF:"yyyy-mm-dd"});r.length&&(t[s]=r)})),s?JSON.stringify(t,null,2):t}__toCSV(e){var s=[];return e.SheetNames.forEach((t=>{var r=this.XLSX.utils.sheet_to_csv(e.Sheets[t]);r.length&&(s.push("SHEET: "+t),s.push(""),s.push(r))})),s.join("\n")}__toFMLA(e){var s=[];return e.SheetNames.forEach((t=>{var r=this.XLSX.utils.get_formulae(e.Sheets[t]);r.length&&(s.push("SHEET: "+t),s.push(""),s.push(r.join("\n")))})),s.join("\n")}__toHTML(e){var s="";return e.SheetNames.forEach((t=>{var r=this.XLSX.write(e,{sheet:t,type:"string",bookType:"html"});s+=r})),s}__toXLSX(e,s){return this.XLSX.write(e)}processWorker(e,s,t){var r="";switch(s){case"form":r=this.__toFMLA(e);break;case"html":r=this.__toHTML(e);break;case"jsonstringify":r=this.__toJSON(e,!0);break;case"json":r=this.__toJSON(e,!1);break;case"xlsx":r=this.__toXLSX(e,t);break;default:r=this.__toCSV(e)}return r}processFile(e,s,t){var r=new FileReader;r.onload=e=>{this.__executeWorker(e.target.result,s,"read",t)},r.readAsBinaryString(e)}__executeWorker(e,s,t="read",r=""){var o=new Worker(this.XW.worker);o.onmessage=e=>{switch(e.data.t){case"ready":break;case"e":console.error(e.data.d);break;case this.XW.msg:globalThis.dispatchEvent(new CustomEvent("xlsx-file-system-data",{composed:!1,bubbles:!1,cancelable:!0,detail:{filename:r,format:s,operation:t,data:this.processWorker(JSON.parse(e.data.d),s,r)}}))}},o.postMessage({d:e,b:"binary"})}}customElements.define(XLSXFileSystemBroker.tag,XLSXFileSystemBroker),globalThis.XLSXFileSystemBroker=globalThis.XLSXFileSystemBroker||{},globalThis.XLSXFileSystemBroker.requestAvailability=()=>(globalThis.XLSXFileSystemBroker.instance||(globalThis.XLSXFileSystemBroker.instance=globalThis.document.createElement("xlsx-file-system-broker"),globalThis.document.body.appendChild(globalThis.XLSXFileSystemBroker.instance)),globalThis.XLSXFileSystemBroker.instance);const r=globalThis.XLSXFileSystemBroker.requestAvailability();export{r as XLSXFileSystemBrokerSingleton,XLSXFileSystemBroker,s as FileSystemBroker,t as FileSystemBrokerSingleton};