import{createEvent}from"./helpers.mjs";function escapeString(e){return`"${e.replaceAll('"','""')}"`}function stringify(e){return e instanceof Date?e.toISOString().split(".")[0]:e.toString()}function checkEscapeString(e){return e.indexOf('"')>-1||e.indexOf(",")>-1?escapeString(e):e}function escapeValue(e){return"string"==typeof e?checkEscapeString(e):stringify(e)}class CSVFile{constructor(e){this.config=e}set data(e){this.headers=Object.getOwnPropertyNames(e[0]),this.rows=e.map((e=>this.headers.map((t=>e[t]))))}get blob(){let e=this.headers.map((e=>escapeString(e))),t=this.rows.map((e=>e.map((e=>escapeValue(e))).join(",")));return new Blob([[e].concat(t).join("\r\n")],{type:"text/csv"})}get url(){return void 0!==this.objectUrl&&window.URL.revokeObjectURL(this.objectUrl),this.objectUrl=window.URL.createObjectURL(this.blob),this.objectUrl}download(e){let t=document.createElement("a");t.setAttribute("download",e),t.href=this.url,t.innerText="Download",document.body.appendChild(t),window.requestAnimationFrame((()=>{t.dispatchEvent(createEvent("click")),document.body.removeChild(t)}))}}export{CSVFile};
