"use strict";var __assign=this&&this.__assign||function(){__assign=Object.assign||function(t){for(var e,a=1,n=arguments.length;a<n;a++){e=arguments[a];for(var i in e)if(Object.prototype.hasOwnProperty.call(e,i))t[i]=e[i]}return t};return __assign.apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:true});exports.AggregatePipeline=void 0;var constant_1=require("../common/constant");var error_1=require("../common/error");var Geo_1=require("../mongo-builder/Geo");var AggregateRequest_1=require("./AggregateRequest");var QueryRequest_1=require("./QueryRequest");var AggregatePipeline=function(){function t(){this.pipeline=[]}t.prototype.addFields=function(e){this.pushStage(constant_1.PipelineStageName.addFields,e);return this};t.prototype.count=function(e){this.pushStage(constant_1.PipelineStageName.count,e);return this};t.prototype.match=function(e){this.pushStage(constant_1.PipelineStageName.match,e);return this};t.prototype.group=function(e){this.pushStage(constant_1.PipelineStageName.group,e);return this};t.prototype.project=function(e){this.pushStage(constant_1.PipelineStageName.project,e);return this};t.prototype.replaceRoot=function(e){this.pushStage(constant_1.PipelineStageName.replaceRoot,e);return this};t.prototype.sample=function(e){this.pushStage(constant_1.PipelineStageName.sample,e);return this};t.prototype.lookup=function(e){this.pushStage(constant_1.PipelineStageName.lookup,e);return this};t.prototype.sort=function(e){this.pushStage(constant_1.PipelineStageName.sort,e);return this};t.prototype.limit=function(e){this.pushStage(constant_1.PipelineStageName.limit,e);return this};t.prototype.skip=function(e){this.pushStage(constant_1.PipelineStageName.skip,e);return this};t.prototype.unwind=function(e){this.pushStage(constant_1.PipelineStageName.unwind,e);return this};t.prototype.geoNear=function(e){this.pushStage(constant_1.PipelineStageName.geoNear,e);return this};t.prototype.bucket=function(e){this.pushStage(constant_1.PipelineStageName.bucket,e);return this};t.prototype.bucketAuto=function(e){this.pushStage(constant_1.PipelineStageName.bucketAuto,e);return this};t.prototype.sortByCount=function(e){this.pushStage(constant_1.PipelineStageName.sortByCount,e);return this};t.prototype.done=function(){return this.pipeline.map(function(e){var a,n;switch(e.name){case constant_1.PipelineStageName.geoNear:{var i=__assign({},e.param);if(e.param.query){i.query=new QueryRequest_1.QueryRequest(e.param.query).toJSON()}if(e.param.near instanceof Geo_1.GeoPoint){i.near=e.param.near.toJSON()}return{$geoNear:i}}case constant_1.PipelineStageName.match:return{$match:new QueryRequest_1.QueryRequest(e.param).toJSON()};case constant_1.PipelineStageName.count:case constant_1.PipelineStageName.sample:case constant_1.PipelineStageName.limit:case constant_1.PipelineStageName.skip:case constant_1.PipelineStageName.unwind:return a={},a[e.name]=e.param,a;case constant_1.PipelineStageName.lookup:case constant_1.PipelineStageName.addFields:case constant_1.PipelineStageName.group:case constant_1.PipelineStageName.sortByCount:case constant_1.PipelineStageName.project:case constant_1.PipelineStageName.bucket:case constant_1.PipelineStageName.bucketAuto:case constant_1.PipelineStageName.replaceRoot:return n={},n[e.name]=new AggregateRequest_1.AggregateRequest(e.param).toJSON(),n;case constant_1.PipelineStageName.sort:return{$sort:Object.keys(e.param).reduce(function(o,p){var r;switch(e.param[p]){case constant_1.Sort.DESC:r=-1;break;case constant_1.Sort.ASC:r=1;break;case 1:r=1;break;case-1:r=-1;break;default:r=1}o[p]=r;return o},{})};default:{var u=e.name;throw error_1.DbBuildError.invalidParam(u,"\u4E0D\u652F\u6301\u7684 aggregate \u64CD\u4F5C")}}})};t.prototype.pushStage=function(e,a){this.pipeline.push({name:e,param:a})};return t}();exports.AggregatePipeline=AggregatePipeline;