var e=function(){e=Object.assign||function(r){for(var t,i=1,s=arguments.length;i<s;i++){t=arguments[i];for(var o in t)if(Object.prototype.hasOwnProperty.call(t,o))r[o]=t[o]}return r};return e.apply(this,arguments)};import{QueryRequest as p}from"../command/QueryRequest";import{UpdateRequest as u}from"../command/UpdateRequest";import{DocumentAction as n,Sort as a}from"../common/constant";import{ActionBuilder as c}from"../common/util";var f=function(){function r(t){this.collectionName=t;this.criteria={sort:[]}}r.prototype.limit=function(t){this.criteria.limit=t;return this};r.prototype.skip=function(t){this.criteria.skip=t;return this};r.prototype.where=function(t){this.criteria.match=t;return this};r.prototype.projection=function(t){for(var i in t){if(t[i]){if(typeof t[i]!=="object"){t[i]=1}}else{t[i]=0}}this.criteria.projection=t;return this};r.prototype.orderBy=function(t,i){this.criteria.sort.push({field:t,sort:i});return this};r.prototype.update=function(t){return this.build(n.update,e(e({},this.baseOptions()),{$update:new u(t.data).toJSON()}))};r.prototype.updateAndReturn=function(t){var i=this.findOptions();return this.build(n.findOneAndUpdate,{$match:i.$match,$update:new u(t.data).toJSON(),$options:{$sort:i.$sort,$returnDocument:"after",$upsert:false}})};r.prototype.set=function(t){return this.build(n.replace,e(e({},this.baseOptions()),{$replace:t.data,$upsert:true}))};r.prototype.remove=function(){return this.build(n.delete,this.baseOptions())};r.prototype.get=function(){return this.build(n.queryList,e({$limit:100},this.findOptions()))};r.prototype.count=function(){return this.build(n.count,this.baseOptions())};r.prototype.baseOptions=function(){var t={};if(typeof this.criteria.match!=="undefined"){var i=new p(this.criteria.match);t.$match=i.toJSON()}if(typeof this.criteria.limit!=="undefined"){t.$limit=this.criteria.limit}return t};r.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 i=0,s=this.criteria.sort;i<s.length;i++){var o=s[i];t.$sort[o.field]=o.sort===a.DESC?-1:1}}if(typeof this.criteria.skip!=="undefined"){t.$skip=this.criteria.skip}return t};r.prototype.build=function(t,i){return c.buildDocumentAction(t,this.collectionName,i)};return r}();export{f as QueryBuilder};