const t=window.webmapgl.h;class e{constructor(){this.format="csv"}async clickHandler(){this.exportCtrl.create(this)}render(){return t("ion-button",{"ion-button":!0,onClick:()=>this.clickHandler()},t("slot",null,this.format.toUpperCase()))}static get is(){return"gl-table-export-button"}static get properties(){return{author:{type:String,attr:"author"},exportCtrl:{connect:"gl-table-export-controller"},filename:{type:String,attr:"filename"},format:{type:String,attr:"format"},pageOrientation:{type:String,attr:"page-orientation"},pageSubtitle:{type:String,attr:"page-subtitle"},pageTitle:{type:String,attr:"page-title"},tableId:{type:String,attr:"table-id"}}}}var a=function(t,e,a,r){var l=new Blob(void 0!==r?[r,t]:[t],{type:a||"application/octet-stream"});if(void 0!==window.navigator.msSaveBlob)window.navigator.msSaveBlob(l,e);else{var o=window.URL.createObjectURL(l),i=document.createElement("a");i.style.display="none",i.href=o,i.setAttribute("download",e),void 0===i.download&&i.setAttribute("target","_blank"),document.body.appendChild(i),i.click(),document.body.removeChild(i),window.URL.revokeObjectURL(o)}};function r(t){return t?-1!==t.indexOf(",")?`"${t.replace(/"/g,'""')}"`:t:""}class l{async create(t){let e=t.table?t.table:document.querySelector("gl-table"+(t.tableId?`#${t.tableId}`:"")),a=t.features?t.features:await e.getSortedFeatures(),r=t.columns?t.columns:Array.from(e.querySelectorAll("gl-table-column"));"csv"===t.format&&this.exportCsv(e,a,r,t),"pdf"===t.format&&this.exportPdf(e,a,r,t)}async exportCsv(t,e,r,l){let o=await this.createCsvDoc(t,e,r);a(o,this.getFilename(l),"text/csv")}async exportPdf(t,e,a,r){pdfMake.createPdf(await this.createDocDef(t,e,a,r)).download(this.getFilename(r))}getFilename(t){return t.filename?t.filename:t.pageTitle?`${t.pageTitle.toLowerCase().replace(/[^a-z0-9]+/g,"_")}.${t.format}`:`table.${t.format}`}zebraFill(t){return 0===t?"black":t%2==0?"#ECEBEB":null}async createCsvDoc(t,e,a){let l=a.filter(t=>t.visible);return[l.map(t=>r(t.label)),...(await Promise.all(e.map(e=>this.getTableRow(t,e,l,"text")))).map(t=>t.map(t=>r(t)))].map(t=>t.join(",")).join("\n")}getTableRow(t,e,a,r="pdf"){return Promise.all(a.map(a=>t.getCellContents(e,a,r)))}async createDocDef(t,e,a,r){let l=a.filter(t=>t.visible),o=l.map(t=>({text:t.label,style:"tableHeader"})),i=await Promise.all(e.map(e=>this.getTableRow(t,e,l))),n={info:{},pageOrientation:r.pageOrientation,content:[{table:{headerRows:1,body:[o,...i]},style:"table",layout:{fillColor:this.zebraFill}}],styles:{tableHeader:{color:"white"},header:{fontSize:15,bold:!0,margin:[0,0,0,5]},subHeader:{margin:[0,5,0,15]}}};return r.pageSubtitle&&n.content.unshift({text:r.pageSubtitle,style:"subHeader",alignment:"center"}),r.pageTitle&&(n.info.title=r.pageTitle,n.content.unshift({text:r.pageTitle,style:"header",alignment:"center"})),r.author&&(n.info.author=r.author),n}static get is(){return"gl-table-export-controller"}static get properties(){return{create:{method:!0}}}}export{e as GlTableExportButton,l as GlTableExportController};