'use strict'; var utilExtractor = require('@flatfile/util-extractor'); var remeda = require('remeda'); var E = require('stream'); var x = require('xlsx'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var E__default = /*#__PURE__*/_interopDefault(E); var x__namespace = /*#__PURE__*/_interopNamespace(x); var v=10,G=s=>{let t="ABCDEFGHIJKLMNOPQRSTUVWXYZ",e="";for(;s>=0;)e=t[s%26]+e,s=Math.floor(s/26)-1;return e},b=s=>s.map((t,e)=>G(e)),m=class{constructor(){}static create(t){switch(t.algorithm){case"explicitHeaders":return new R(t);case"specificRows":return new S(t);case"dataRowAndSubHeaderDetection":return new k(t);case"newfangled":throw new Error("Not implemented");default:return new H(t)}}},h=s=>s.filter(t=>t!==null&&`${t}`.trim()!=="").length,L=s=>s.some(t=>t===null||t.trim()===""||!isNaN(Number(t.trim()))||t.trim().toLowerCase()==="true"||t.trim().toLowerCase()==="false"),H=class extends m{constructor(e){super();this.options=e;this.rowsToSearch=e.rowsToSearch||v;}rowsToSearch;async getHeaders(e){let i=0,a=0,r=[],o=[],d=new E__default.default.Writable({objectMode:!0,write:(n,u,p)=>{i++,i>=this.rowsToSearch&&e.destroy(),h(n)>h(r)&&(r=n,a=i,o=b(r)),p();}});return e.pipe(d,{end:!0}),new Promise((n,u)=>{d.on("finish",()=>{n({header:r,skip:a,letters:o});}),e.on("close",()=>{n({header:r,skip:a,letters:o});}),e.on("error",p=>{u(p);});})}},R=class extends m{constructor(e){super();this.options=e;if(!e.headers||e.headers.length===0)throw new Error("ExplicitHeaders requires at least one header")}headers;async getHeaders(e){let i=b(this.options.headers);return {header:this.options.headers,skip:this.options.skip||0,letters:i}}},S=class extends m{constructor(e){super();this.options=e;if(!e.rowNumbers||e.rowNumbers.length===0)throw new Error("SpecificRows requires at least one row number")}async getHeaders(e){let i=0,a=Math.max(...this.options.rowNumbers),r=[],o=[],d=new E__default.default.Writable({objectMode:!0,write:(u,p,c)=>{if(i>a)e.destroy();else if(this.options.rowNumbers.includes(i))if(r.length===0)r=u,o=b(r);else for(let l=0;l{d.on("finish",()=>{u({header:r,skip:n,letters:o});}),e.on("close",()=>{u({header:r,skip:n,letters:o});}),e.on("error",c=>{p(c);});})}},k=class extends m{constructor(e){super();this.options=e;this.rowsToSearch=e.rowsToSearch||v;}rowsToSearch;async getHeaders(e){let i=0,a=0,r=[],o=[],d=[],n=new E__default.default.Writable({objectMode:!0,write:(c,l,w)=>{if(i++,i>=this.rowsToSearch&&e.destroy(),o.push(c),h(c)>h(r)&&(r=c,a=i,d=b(r)),L(c)){let f=o[o.length-2];f&&h(r)===h(f)&&!L(f)&&(r=f,a=i-1,d=b(r));}w();}});e.pipe(n,{end:!0}),await new Promise((c,l)=>{n.on("finish",()=>{c();}),e.on("close",()=>{c();}),e.on("error",w=>{l(w);});});let u,p;for(let c=a;c(l[O]?.trim()??"").split(/\s+/).every(C=>f.toLowerCase().includes(C.toLowerCase()))).length/r.length>.5&&(u=l,p=c+1,d=b(u));}return {header:u??r,skip:p??a,letters:d}}};function X(s){let t={},e={};for(let[i,a]of Object.entries(s)){let r=a||"empty",o=typeof r=="string"?r.replace("*",""):r;o&&t[o]?(e[i]=`${o}_${t[o]}`,t[o]++):(e[i]=o,t[o]=1);}return e}async function N(s,t){let e;try{e=x__namespace.read(s,{type:"buffer",cellDates:!0,dense:!0,dateNF:t?.dateNF||void 0,WTF:!0});}catch(a){if(a.code==="ERR_STRING_TOO_LONG")throw t?.debug&&console.log("File is too large to parse. Try converting this file to CSV."),new Error("File is too large to parse. Try converting this file to CSV.");e=x__namespace.read(s,{type:"buffer",cellDates:!0,dense:!0,dateNF:t?.dateNF||void 0});}let i=Object.keys(e.Sheets);try{let a=(await Promise.all(i.map(async r=>{let o=e.Sheets[r],d=await $({sheet:o,sheetName:r,rawNumbers:t?.rawNumbers||!1,raw:t?.raw||!1,headerDetectionOptions:t?.headerDetectionOptions||{algorithm:"default"},headerSelectionEnabled:t?.headerSelectionEnabled??!1,skipEmptyLines:t?.skipEmptyLines??!1,debug:t?.debug});if(d)return [r,d]}))).filter(Boolean);return Object.fromEntries(a)}catch(a){throw console.error(a),new Error("Failed to parse workbook")}}async function $({sheet:s,sheetName:t,rawNumbers:e,raw:i,headerDetectionOptions:a,headerSelectionEnabled:r,skipEmptyLines:o,debug:d}){let n=x__namespace.utils.sheet_to_json(s,{header:"A",defval:null,rawNumbers:e,raw:i,blankrows:r||!o});if(!n||n.length===0){d&&console.log(`No rows found in '${t}'`);return}let u=y=>y.map(g=>Object.values(g)),p=m.create(a),c=E.Readable.from(u(n)),{skip:l,header:w}=await p.getHeaders(c),f=Math.max(0,l-1),O=Object.keys(n[f]);if(d&&console.log("Detected header:",w),r||n.splice(0,l),n.length===0){d&&console.log(`No rows found in '${t}'`);return}let j=((y,g)=>y.reduce((D,W,_)=>(D[g[_]]=W,D),{}))(r?O:w,O),P=X(j);for(;n.length>0&&Object.values(n[n.length-1]).every(V);)n.pop();let F=n.map(y=>remeda.mapValues(remeda.mapKeys(y,g=>P[g]),g=>({value:g}))),T;return r&&(T={rowHeaders:[l]}),{headers:Object.values(P).filter(Boolean),data:F,metadata:T}}var V=s=>s===null||typeof s=="string"&&s.trim()==="";var q=s=>utilExtractor.Extractor(/\.(xlsx?|xlsm|xlsb|xltx?|xltm)$/i,"excel",N,s),ne=N,ae=q; exports.ExcelExtractor = q; exports.excelParser = ne; exports.xlsxExtractorPlugin = ae; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map