(()=>{"use strict";var e,t;(t=e||(e={})).csv="text/csv",t.tsv="text/tab-separated-values",t.plain="text/plain";var r=e=>e,o=e=>e,n=r,a=r,s=r,i=r,l=r,u={fieldSeparator:",",decimalSeparator:".",quoteStrings:!0,quoteCharacter:'"',showTitle:!1,title:"My Generated Report",filename:"generated",showColumnHeaders:!0,useTextFile:!1,fileExtension:"csv",mediaType:e.csv,useBom:!0,columnHeaders:[],useKeysAsHeaders:!1,boolDisplay:{true:"TRUE",false:"FALSE"},replaceUndefinedWith:""};class c extends Error{constructor(e){super(e),this.name="CsvGenerationError"}}class d extends Error{constructor(e){super(e),this.name="EmptyHeadersError"}}Error;class f extends Error{constructor(e){super(e),this.name="UnsupportedDataFormatError"}}var p=e=>i("object"==typeof e?e.key:e),h=e=>l("object"==typeof e?e.displayLabel:e),y=e=>t=>a(e+t+"\r\n"),m=e=>(t,r)=>g(e)(s(t+r)),g=e=>t=>t+e.fieldSeparator,v=(e,t)=>{let r=t;return(e.quoteStrings||e.fieldSeparator&&t.indexOf(e.fieldSeparator)>-1||e.quoteCharacter&&t.indexOf(e.quoteCharacter)>-1||t.indexOf("\n")>-1||t.indexOf("\r")>-1)&&(r=e.quoteCharacter+function(e,t){return'"'==t&&e.indexOf('"')>-1?e.replace(/"/g,'""'):e}(t,e.quoteCharacter)+e.quoteCharacter),n(r)},b=(e,t)=>{if("number"==typeof t)return((e,t)=>{if((e=>+e===e&&(!isFinite(e)||Boolean(e%1)))(t)){if("locale"===e.decimalSeparator)return n(t.toLocaleString());if(e.decimalSeparator)return n(t.toString().replace(".",e.decimalSeparator))}return n(t.toString())})(e,t);if("string"==typeof t)return v(e,t);if("boolean"==typeof t&&e.boolDisplay)return((e,t)=>{const r=t?"true":"false";return n(e.boolDisplay[r])})(e,t);if(null==t)return((e,t)=>void 0===t&&void 0!==e.replaceUndefinedWith?v(e,e.replaceUndefinedWith+""):v(e,null===t?"null":""))(e,t);throw new f(`\n    typeof ${typeof t} isn't supported. Only number, string, boolean, null and undefined are supported.\n    Please convert the data in your object to one of those before generating the CSV.\n    `)},S=e=>t=>{const r=(e=>Object.assign({},u,e))(e),n=r.useKeysAsHeaders?Object.keys(t[0]):r.columnHeaders;let i=((e,...t)=>t.reduce((e,t)=>t(e),e))(a(""),(e=>t=>e.useBom?a(t+"\ufeff"):t)(r),(e=>t=>e.showTitle?y(a(t+e.title))(s("")):t)(r),((e,t)=>r=>{if(!e.showColumnHeaders)return r;if(t.length<1)throw new d("Option to show headers but none supplied. Make sure there are keys in your collection or that you've supplied headers through the config options.");let n=s("");for(let r=0;r<t.length;r++){const a=h(t[r]);n=m(e)(n,b(e,o(a)))}return n=s(n.slice(0,-1)),y(r)(n)})(r,n),((e,t,r)=>n=>{let a=n;for(var i=0;i<r.length;i++){let n=s("");for(let a=0;a<t.length;a++){const s=p(t[a]),l=r[i][o(s)];n=m(e)(n,b(e,l))}n=s(o(n).slice(0,-1)),a=y(a)(n)}return a})(r,n,t));if(i.length<1)throw new c("Output is empty. Is your data formatted correctly?");return i},x=function(e,t,r){for(var o in void 0===t&&(t=""),void 0===r&&(r={}),e){var n=e[o],a=t?"".concat(t,"__").concat(o):o;null===n?r[a]="":"object"!=typeof n||Array.isArray(n)?r[a]=n:x(n,a,r)}return r};self.onmessage=function(e){var t,r,o,n=e.data,a=(t=n.data,r=n.csvConfig,o=t.map(function(e){return x(e)}),S(r)(o));postMessage(a)}})();