"use strict";var __assign=this&&this.__assign||function(){__assign=Object.assign||function(i){for(var t,e=1,o=arguments.length;e<o;e++){t=arguments[e];for(var r in t)if(Object.prototype.hasOwnProperty.call(t,r))i[r]=t[r]}return i};return __assign.apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:true});exports.QueryBuilder=void 0;var QueryRequest_1=require("../command/QueryRequest");var UpdateRequest_1=require("../command/UpdateRequest");var constant_1=require("../common/constant");var util_1=require("../common/util");var QueryBuilder=function(){function i(t){this.collectionName=t;this.criteria={sort:[]}}i.prototype.limit=function(t){this.criteria.limit=t;return this};i.prototype.skip=function(t){this.criteria.skip=t;return this};i.prototype.where=function(t){this.criteria.match=t;return this};i.prototype.projection=function(t){for(var e in t){if(t[e]){if(typeof t[e]!=="object"){t[e]=1}}else{t[e]=0}}this.criteria.projection=t;return this};i.prototype.orderBy=function(t,e){this.criteria.sort.push({field:t,sort:e});return this};i.prototype.update=function(t){return this.build(constant_1.DocumentAction.update,__assign(__assign({},this.baseOptions()),{$update:new UpdateRequest_1.UpdateRequest(t.data).toJSON()}))};i.prototype.updateAndReturn=function(t){var e=this.findOptions();return this.build(constant_1.DocumentAction.findOneAndUpdate,{$match:e.$match,$update:new UpdateRequest_1.UpdateRequest(t.data).toJSON(),$options:{$sort:e.$sort,$returnDocument:"after",$upsert:false}})};i.prototype.set=function(t){return this.build(constant_1.DocumentAction.replace,__assign(__assign({},this.baseOptions()),{$replace:t.data,$upsert:true}))};i.prototype.remove=function(){return this.build(constant_1.DocumentAction.delete,this.baseOptions())};i.prototype.get=function(){return this.build(constant_1.DocumentAction.queryList,__assign({$limit:100},this.findOptions()))};i.prototype.count=function(){return this.build(constant_1.DocumentAction.count,this.baseOptions())};i.prototype.baseOptions=function(){var t={};if(typeof this.criteria.match!=="undefined"){var e=new QueryRequest_1.QueryRequest(this.criteria.match);t.$match=e.toJSON()}if(typeof this.criteria.limit!=="undefined"){t.$limit=this.criteria.limit}return t};i.prototype.findOptions=function(){var t=this.baseOptions();if(typeof this.criteria.projection!=="undefined"){t.$project=this.criteria.projection}if(typeof this.criteria.sort!=="undefined"){t.$sort={};for(var e=0,o=this.criteria.sort;e<o.length;e++){var r=o[e];t.$sort[r.field]=r.sort===constant_1.Sort.DESC?-1:1}}if(typeof this.criteria.skip!=="undefined"){t.$skip=this.criteria.skip}return t};i.prototype.build=function(t,e){return util_1.ActionBuilder.buildDocumentAction(t,this.collectionName,e)};return i}();exports.QueryBuilder=QueryBuilder;