text: import{columnIndexOf,columnUnique,columnExtents}from'../utils/datatable-utils';import{getDateStart}from'../../../fc-utils/src/time-intervals/datetime-ops';import{duration}from'../../../fc-utils/src/time-intervals/duration';import{interval}from'../../../fc-utils/src/ds-utils/interval';import aggregatorStore from'../aggregators';import'../aggregators/aggregators';import{getConfig}from'../globalConfig';import{OperatorTypes}from'./type-enums';import groupBy,{_singleSortedAddRow,_singleSortedAddRowDate}from'./group-by';import{numberComparator,stringComparator}from'../utils/comparators';import{leftMostExactOrGreater}from'../utils/sorted-search';function pivot(a,b,c){let d=a,e=b,f=c;return{ops:'pivot',type:OperatorTypes.GroupBy,_updateArgs:(a,b,c)=>{d=a,e=b,f=c},fn:(a,b,c,g)=>{if(0===d.length)throw new Error('groupConfigArray cannot be empty');if(0===f.length)throw new Error('aggrConfigArray cannot be empty');if(!e)throw new Error('pivotCol cannot be empty');return!(d instanceof Array)&&d instanceof Object&&(d=[d]),!(f instanceof Array)&&f instanceof Object&&(f=[f]),1===d.length&&c&&d[0].column===c.indexBy?_singleSortedPivot(a,b,c,d[0],f,e):_genericPivot(a,b,c,g,d,f,e)}}}function _createAggrSchema(a,b,c,d,e){var f,g,h,k,l,m,n,o,p,q,r,s,t,u,v=[],w={},x=0;for(c=c instanceof Array?c:[c],d=d instanceof Array?d:[d],k=c.length,l=d.length,r=0;r<k;r++){if(m=columnIndexOf(c[r].column,b),n=b[m],-1===m)throw new Error('incorrect column name in groupConfigArr - '+c[r].column);u={name:c[r].outputAs||c[r].column,type:'date'===n.type?'interval':n.type},void 0!==n.enableUTC&&(u.enableUTC=n.enableUTC),v.push(u)}switch(e.type){case'number':case'date':f=numberComparator;break;default:f=stringComparator;}for(g=columnUnique(e.name,a,b),g.sort((c,a)=>f(c,a)),h=g.length,r=0;r<l;r++){if(t=d[r].operation||'avg',m=columnIndexOf(d[r].column,b),n=b[m],-1===m)throw new Error('incorrect column name in aggrConfigArr - '+d[r].column);if(p=aggregatorStore.resolve(t),!p)throw new Error(`${t} is not a defined operation`);switch(t){case'first':case'last':o=n.type;break;case'count':o='number';break;default:if('number'!==n.type)throw new Error(`${t} can apply only on numbers`);o='number';}for(s=0;s<h;s++)q=g[s]+' - '+(d[r].outputAs||d[r].column+(t?' - '+t:'')),w[q]=x++,v.push({name:q,type:o})}return{schema:v,pivots:w}}function _singleSortedPivot(a,b,c,d,e,f){var g,h,j,k,l,m,n,o,p,q,r,s,t,u=[],v=[],w={},x=a.length,y=0,z=_cacheColumnIndex(),A=[];if(h=columnIndexOf(d.column,b),-1===h)throw new Error('incorrect column name in groupConfigArr - '+d);if(k=columnIndexOf(f,b),-1===k)throw new Error('incorrect pivot column name');if(j=b[h],l=b[k],n=Object.assign({},d),n.type=j.type,n.outputAs=d.outputAs,g=_createAggrSchema(a,b,n,e,l),v=g.schema,w=g.pivots,0<x){if(s=j.enableUTC||getConfig('enableUTC'),'date'===j.type){let e,f,g;if(o=duration(d.timeUnit,Math.abs(d.binSize)||1),a[0]&&a[0][h]&&a[x-1]&&a[x-1][h]||(e=columnExtents(d.column,a,b,c.indexBy)),f=a[0]&&a[0][h]||e.min,g=d.startValue&&parseInt(d.startValue,10),!g)p=getDateStart(f,duration(d.timeUnit,1),s,d.weekStartFrom);else if(g>=f){let b=(c,a)=>numberComparator(c[h],a);p=getDateStart(g,duration(d.timeUnit,1),s,d.weekStartFrom),y=leftMostExactOrGreater(p,a,b,0,a.length)}else p=getDateStart(f,o,s,d.weekStartFrom,g);m=a[x-1]&&a[x-1][h]||e.max}if(void 0===p||p<=m){let c={duration:o,outputFormat:d.outputFormat,enableUTC:s,weekStartFrom:d.weekStartFrom};for(void 0!==p&&(q=[interval(p,c)],u.push(q)),t=y;t<x;t++)0<a[t].length&&('date'===j.type?q=_singleSortedAddRowDate(u,q,a[t][h],c,p):('string'===j.type||'number'===j.type)&&(q=_singleSortedAddRow(u,q,a[t][h])),r=u.length-1,A[r]=A[r]||{},q&&_applyAggr(a[t],b,q,1,e,k,w,z,A[r]))}}return{data:u,schema:v,config:{indexBy:d.groupColumn}}}function _genericPivot(a,b,c,d,e,f,g){let h,k,i,j,l,m,n,o,p,q={},r={},s=a=>r.hasOwnProperty(a)?r[a]:-1,t=(a,b)=>{r[a]=b},u=(a,b,c,d)=>a+' - '+(b||c+(d?' - '+d:'')),v=()=>{let a,b=[];for(let c=0;c<o;c++)if(p.includes(m[c].name))b.push(m[c]),q[m[c].name]=c;else{a=f.filter(a=>(a.outputAs||a.column+(a.operation?' - '+a.operation:''))===m[c].name)[0];for(let d=0;d<l.length;d++)b.push({name:u(l[d],a.outputAs,a.column,a.operation),type:m[c].type})}return b},w=a=>{let b,c,d,e,h,l=[],m=a.length,o={},p={},r='';for(b=0;b<m;b++)o[a[b].name]=b;for(b=0;b<k.data.length;b++){for(p={},r='',c=0;c<k.schema.length;c++)q.hasOwnProperty(k.schema[c].name)?(p[o[k.schema[c].name]]=k.data[b][c],r+='interval'===k.schema[c].type?k.data[b][c].start:k.data[b][c]):k.schema[c].name!==g&&(e=f.filter(a=>(a.outputAs||a.column+(a.operation?' - '+a.operation:''))===k.schema[c].name)[0],h=o[u(k.data[b][n],e.outputAs,e.column,e.operation)],h&&(p[h]=k.data[b][c]));if(d=s(r),-1<d){let a=l[d];for(let b in p)a[b]=p[b]}else{let a=Array(m);for(let b in p)a[b]=p[b];l.push(a),t(r,l.length-1)}}return l},x=[],y=[];switch(h=groupBy(e.concat({column:g}),f),k=h.fn(a,b,c,d),j=columnUnique(g,a,b),n=columnIndexOf(g,k.schema),k.schema[n].type){case'number':case'date':i=numberComparator;break;default:i=stringComparator;}return l=j&&j.sort((c,a)=>i(c,a))||[],m=k.schema.filter(a=>a.name!==g),o=m.length,p=e.map(a=>a.outputAs||a.column),x=v(),0<k.data.length&&(y=w(x)),{data:y,schema:x,config:k.config}}function _cacheColumnIndex(){let a={};return function(b,c){return a[b]||(a[b]=columnIndexOf(b,c)),a[b]}}function _applyAggr(a,b,c,d,e,f,g,h,j){let k,l,m,n,o,p=e.length;for(n=0;n<p;n++)if(k=h(e[n].column,b),void 0!==a[k]&&null!==a[k]){switch(l=e[n].operation||'avg',o=g[a[f]+' - '+(e[n].outputAs||e[n].column+(l?' - '+l:''))],l){case'sum':case'min':case'max':case'first':case'last':m=null;break;default:m=j[o]=j[o]&&j[o]+1||1;}c[d+o]='count'===l?m:aggregatorStore.resolve(l)(void 0===c[d+o]?null:c[d+o],a[k],m)}}export default pivot;