var e=function(e){return e[e.FIELD_START=0]=`FIELD_START`,e[e.UNQUOTED=1]=`UNQUOTED`,e[e.QUOTED=2]=`QUOTED`,e[e.QUOTE_IN_QUOTED=3]=`QUOTE_IN_QUOTED`,e}(e||{});function t(t){let n=[],r=[],i=``,a=e.FIELD_START;for(let o=0;o<t.length;o++){let s=t[o];switch(a){case e.FIELD_START:s===`"`?a=e.QUOTED:s===`,`?(r.push(i),i=``):s===`\r`||(s===`
`?(r.push(i),i=``,n.push(r),r=[]):(i+=s,a=e.UNQUOTED));break;case e.UNQUOTED:s===`,`?(r.push(i),i=``,a=e.FIELD_START):s===`\r`||(s===`
`?(r.push(i),i=``,n.push(r),r=[],a=e.FIELD_START):i+=s);break;case e.QUOTED:s===`"`?a=e.QUOTE_IN_QUOTED:i+=s;break;case e.QUOTE_IN_QUOTED:s===`"`?(i+=`"`,a=e.QUOTED):s===`,`?(r.push(i),i=``,a=e.FIELD_START):s===`\r`||(s===`
`?(r.push(i),i=``,n.push(r),r=[],a=e.FIELD_START):(i+=s,a=e.UNQUOTED));break}}if((i||r.length>0)&&(r.push(i),n.push(r)),n.length===0)return{headers:[],rows:[]};let o=n[0],s=n.slice(1),c=o.length;for(let e=0;e<s.length;e++){let t=s[e];if(t.length<c)for(;t.length<c;)t.push(``);else t.length>c&&(s[e]=t.slice(0,c))}return{headers:o,rows:s}}function n(e,t){let n=e=>e.includes(`,`)||e.includes(`"`)||e.includes(`
`)?`"${e.replace(/"/g,`""`)}"`:e,r=[e.map(n).join(`,`)];for(let e of t)r.push(e.map(n).join(`,`));return r.join(`
`)}export{n,t};