var a=function(){a=Object.assign||function(r){for(var t,o=1,u=arguments.length;o<u;o++){t=arguments[o];for(var n in t)if(Object.prototype.hasOwnProperty.call(t,n))r[n]=t[n]}return r};return a.apply(this,arguments)};import{PipelineStageName as e,Sort as c}from"../common/constant";import{DbBuildError as f}from"../common/error";import{GeoPoint as l}from"../mongo-builder/Geo";import{AggregateRequest as g}from"./AggregateRequest";import{QueryRequest as h}from"./QueryRequest";var y=function(){function r(){this.pipeline=[]}r.prototype.addFields=function(t){this.pushStage(e.addFields,t);return this};r.prototype.count=function(t){this.pushStage(e.count,t);return this};r.prototype.match=function(t){this.pushStage(e.match,t);return this};r.prototype.group=function(t){this.pushStage(e.group,t);return this};r.prototype.project=function(t){this.pushStage(e.project,t);return this};r.prototype.replaceRoot=function(t){this.pushStage(e.replaceRoot,t);return this};r.prototype.sample=function(t){this.pushStage(e.sample,t);return this};r.prototype.lookup=function(t){this.pushStage(e.lookup,t);return this};r.prototype.sort=function(t){this.pushStage(e.sort,t);return this};r.prototype.limit=function(t){this.pushStage(e.limit,t);return this};r.prototype.skip=function(t){this.pushStage(e.skip,t);return this};r.prototype.unwind=function(t){this.pushStage(e.unwind,t);return this};r.prototype.geoNear=function(t){this.pushStage(e.geoNear,t);return this};r.prototype.bucket=function(t){this.pushStage(e.bucket,t);return this};r.prototype.bucketAuto=function(t){this.pushStage(e.bucketAuto,t);return this};r.prototype.sortByCount=function(t){this.pushStage(e.sortByCount,t);return this};r.prototype.done=function(){return this.pipeline.map(function(t){var o,u;switch(t.name){case e.geoNear:{var n=a({},t.param);if(t.param.query){n.query=new h(t.param.query).toJSON()}if(t.param.near instanceof l){n.near=t.param.near.toJSON()}return{$geoNear:n}}case e.match:return{$match:new h(t.param).toJSON()};case e.count:case e.sample:case e.limit:case e.skip:case e.unwind:return o={},o[t.name]=t.param,o;case e.lookup:case e.addFields:case e.group:case e.sortByCount:case e.project:case e.bucket:case e.bucketAuto:case e.replaceRoot:return u={},u[t.name]=new g(t.param).toJSON(),u;case e.sort:return{$sort:Object.keys(t.param).reduce(function(p,s){var i;switch(t.param[s]){case c.DESC:i=-1;break;case c.ASC:i=1;break;case 1:i=1;break;case-1:i=-1;break;default:i=1}p[s]=i;return p},{})};default:{var m=t.name;throw f.invalidParam(m,"\u4E0D\u652F\u6301\u7684 aggregate \u64CD\u4F5C")}}})};r.prototype.pushStage=function(t,o){this.pipeline.push({name:t,param:o})};return r}();export{y as AggregatePipeline};