import{neb}from"../dist/web/main2.js";import{State}from"./state.min.js";const isType=(obj,type)=>typeof obj===type,isNumber=obj=>isType(obj,"number"),isString=obj=>isType(obj,"string"),isFunction=obj=>isType(obj,"function"),s=arr=>arr.map(e=>`${e}`),log=console.log,except=msg=>{throw{message:msg}},CT=neb.CustomType,OT=neb.OrderType,RU=neb.Rollup,OP=neb.Operation;export class Nebula{constructor(){this.Type={INT:CT.INT,LONG:CT.LONG,FLOAT:CT.FLOAT,DOUBLE:CT.DOUBLE,STRING:CT.STRING},this.Sort={ASC:OT.ASC,DESC:OT.DESC,NONE:OT.NONE},this.reset=()=>{this.src_="",this.keys_=[],this.metrics_=[],this.start_=0,this.end_=0,this.columns_=[],this.timeline_=!1,this.sort_=this.Sort.ASC,this.limit_=100,this.filter_={},this.pivot_="",this.map_=null,this.rm_=[],this.window_=0},this.source=src=>(this.src_=src,this),this.time=(start,end)=>(this.start_=start,this.end_=end,this),this.select=(...args)=>{for(var i=0;i<args.length;++i){const arg=args[i],type=typeof arg;if("string"===type)this.keys_.push(arg);else if("function"===type)this.metrics_.push(eval(arg));else{if("object"!==type)throw`Unsupported field type: ${type}.`;this.metrics_.push(arg)}}return this},this.pivot=col=>(this.keys_.includes(col)&&1===this.metrics_.length||except("pivot existing key for single metric."),this.pivot_=col,this),this.map=(f,...cols)=>(this.map_=f?`() => ${f.toString()};`:"",this.rm_=cols,this),this.where=f=>(f&&(f.l?this.filter_=f:this.filter_={l:"AND",r:[f]}),this),this.sortby=x=>(this.sort_=x,this),this.limit=l=>(this.limit_=l,this),this.apply=(name,type,expr)=>(name&&isString(name)||except("apply - column name required"),(void 0===type||!isNumber(type)||type<this.Type.INT||type>this.Type.STRING)&&except("apply - unsupported type."),expr&&isFunction(expr)||except("apply - lambda/function required."),this.columns_.push({name:name,type:type,expr:`const ${name} = ${expr.toString()};`}),this),this.run=(timeline,window)=>(this.timeline_=!!timeline,this.window_=window||0,this),this.timeline=window=>this.run(!0,window),this.validate=()=>this.src_?0===this.start_||0===this.end_?(log(`Invalid time range [${this.start_}, ${this.end_}].`),"Invalid time range."):0===this.keys_.length&&0===this.metrics_.length?"Keys or metrics missing":this.limit_<1?(log(`Invalid limit: ${this.limit_}`),"Invalid limit value."):this.sort_!=this.Sort.ASC&&this.sort_!=this.Sort.DESC&&this.sort_!=this.Sort.NONE?(log(`Invalid sort value: ${this.sort_}`),"Invalid sort option."):null:"Invalid table source.",this.build=()=>{const state=new State,s=state.state_;return s.table=this.src_,s.start=this.start_,s.end=this.end_,s.keys=this.keys_,s.window=this.window_,s.timeline=this.timeline_,s.limit=this.limit_,s.sort=this.sort_,s.metrics=this.metrics_,s.filter=this.filter_,s.customs=this.columns_,s.pivot=this.pivot_,s.map=this.map_,s.rm=this.rm_,s}}}export const count=col=>({M:RU.COUNT,C:col,A:col,as:function(alias){return this.A=alias,this}});export const sum=col=>({M:RU.SUM,C:col,A:col,as:function(alias){return this.A=alias,this}});export const max=col=>({M:RU.MAX,C:col,A:col,as:function(alias){return this.A=alias,this}});export const min=col=>({M:RU.MIN,C:col,A:col,as:function(alias){return this.A=alias,this}});export const avg=col=>({M:RU.AVG,C:col,A:col,as:function(alias){return this.A=alias,this}});export const tree=col=>({M:RU.TREEMERGE,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p10=col=>({M:RU.P10,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p25=col=>({M:RU.P25,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p50=col=>({M:RU.P50,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p75=col=>({M:RU.P75,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p90=col=>({M:RU.P90,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p99=col=>({M:RU.P99,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p99_9=col=>({M:RU.P99_9,C:col,A:col,as:function(alias){return this.A=alias,this}});export const p99_99=col=>({M:RU.P99_99,C:col,A:col,as:function(alias){return this.A=alias,this}});export const and=(...args)=>0==args.length?{}:{l:"AND",r:args};export const or=(...args)=>0==args.length?{}:{l:"OR",r:args};export const eq=(col,...args)=>(0==args.length&&except("syntax: eq(<column>, val1, val2, ...)"),{c:col,o:OP.EQ,v:s(args)});export const neq=(col,...args)=>(0==args.length&&except("syntax: eq(<column>, val1, val2, ...)"),{c:col,o:OP.NEQ,v:s(args)});export const gt=(col,val)=>({c:col,o:OP.MORE,v:s([val])});export const lt=(col,val)=>({c:col,o:OP.LESS,v:s([val])});export const like=(col,val)=>({c:col,o:OP.LIKE,v:s([val])});export const ilike=(col,val)=>({c:col,o:OP.ILIKE,v:s([val])});