(function(a,p){typeof exports=="object"&&typeof module<"u"?p(exports,require("../../core/plugin/base-plugin")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin"],p):(a=typeof globalThis<"u"?globalThis:a||self,p(a.TbwGridPlugin_export={},a.TbwGrid))})(this,(function(a,p){"use strict";function b(i,t=!0){if(i==null)return"";if(i instanceof Date)return i.toISOString();if(typeof i=="object")return JSON.stringify(i);const e=String(i);return t&&(e.includes(",")||e.includes('"')||e.includes(`
`)||e.includes("\r"))?`"${e.replace(/"/g,'""')}"`:e}function y(i,t,e,o={}){const c=o.delimiter??",",r=o.newline??`
`,s=[],l=o.bom?"\uFEFF":"";if(e.includeHeaders!==!1){const n=t.map(f=>{const d=f.header||f.field,u=e.processHeader?e.processHeader(d,f.field):d;return b(u)});s.push(n.join(c))}for(const n of i){const f=t.map(d=>{let u=n[d.field];return e.processCell&&(u=e.processCell(u,d.field,n)),b(u)});s.push(f.join(c))}return l+s.join(r)}function h(i,t){const e=URL.createObjectURL(i),o=document.createElement("a");o.href=e,o.download=t,o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o),URL.revokeObjectURL(e)}function S(i,t){const e=new Blob([i],{type:"text/csv;charset=utf-8;"});h(e,t)}function m(i){return i.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;")}function C(i,t,e){let o=`<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Sheet1">
<Table>`;if(e.includeHeaders!==!1){o+=`
<Row>`;for(const c of t){const r=c.header||c.field,s=e.processHeader?e.processHeader(r,c.field):r;o+=`<Cell><Data ss:Type="String">${m(s)}</Data></Cell>`}o+="</Row>"}for(const c of i){o+=`
<Row>`;for(const r of t){let s=c[r.field];e.processCell&&(s=e.processCell(s,r.field,c));let l="String",n="";s==null?n="":typeof s=="number"&&!isNaN(s)?(l="Number",n=String(s)):s instanceof Date?(l="DateTime",n=s.toISOString()):n=m(String(s)),o+=`<Cell><Data ss:Type="${l}">${n}</Data></Cell>`}o+="</Row>"}return o+=`
</Table>
</Worksheet>
</Workbook>`,o}function E(i,t){const e=t.endsWith(".xls")?t:`${t}.xls`,o=new Blob([i],{type:"application/vnd.ms-excel;charset=utf-8;"});h(o,e)}class j extends p.BaseGridPlugin{name="export";get defaultConfig(){return{fileName:"export",includeHeaders:!0,onlyVisible:!0,onlySelected:!1}}isExportingFlag=!1;lastExportInfo=null;performExport(t,e){const o=this.config,c={format:t,fileName:e?.fileName??o.fileName??"export",includeHeaders:e?.includeHeaders??o.includeHeaders,processCell:e?.processCell,processHeader:e?.processHeader,columns:e?.columns,rowIndices:e?.rowIndices};let r=[...this.columns];if(o.onlyVisible&&(r=r.filter(n=>!n.hidden&&!n.field.startsWith("__"))),e?.columns){const n=new Set(e.columns);r=r.filter(f=>n.has(f.field))}let s=[...this.rows];if(o.onlySelected){const n=this.getSelectionState();n?.selected?.size&&(s=[...n.selected].sort((d,u)=>d-u).map(d=>this.rows[d]).filter(Boolean))}e?.rowIndices&&(s=e.rowIndices.map(n=>this.rows[n]).filter(Boolean)),this.isExportingFlag=!0;let l=c.fileName;try{switch(t){case"csv":{const n=y(s,r,c,{bom:!0});l=l.endsWith(".csv")?l:`${l}.csv`,S(n,l);break}case"excel":{const n=C(s,r,c);l=l.endsWith(".xls")?l:`${l}.xls`,E(n,l);break}case"json":{const n=s.map(u=>{const w={};for(const g of r){let x=u[g.field];c.processCell&&(x=c.processCell(x,g.field,u)),w[g.field]=x}return w}),f=JSON.stringify(n,null,2);l=l.endsWith(".json")?l:`${l}.json`;const d=new Blob([f],{type:"application/json"});h(d,l);break}}this.lastExportInfo={format:t,timestamp:new Date},this.emit("export-complete",{format:t,fileName:l,rowCount:s.length,columnCount:r.length})}finally{this.isExportingFlag=!1}}getSelectionState(){try{return this.grid?.getPluginState?.("selection")??null}catch{return null}}exportCsv(t){this.performExport("csv",t)}exportExcel(t){this.performExport("excel",t)}exportJson(t){this.performExport("json",t)}isExporting(){return this.isExportingFlag}getLastExport(){return this.lastExportInfo}}a.ExportPlugin=j,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=export.umd.js.map
