UNPKG

mingo

Version:

MongoDB query language for in-memory objects

2 lines 96 kB
var mingo=(()=>{var Or=Object.defineProperty;var Uo=Object.getOwnPropertyDescriptor;var Fo=Object.getOwnPropertyNames;var Bo=Object.prototype.hasOwnProperty;var ht=(e,t)=>{for(var r in t)Or(e,r,{get:t[r],enumerable:!0})},Lo=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Fo(t))!Bo.call(e,o)&&o!==r&&Or(e,o,{get:()=>t[o],enumerable:!(n=Uo(t,o))||n.enumerable});return e};var zo=e=>Lo(Or({},"__esModule",{value:!0}),e);var mp={};ht(mp,{Aggregator:()=>ve,Context:()=>Ot,ProcessingMode:()=>wt,Query:()=>Te,aggregate:()=>ko,default:()=>up,find:()=>Co,update:()=>_o,updateMany:()=>Mo,updateOne:()=>Do});function X(e,t){return e*16777619^t>>>0}function On(e){if(Number.isNaN(e))return 2143289344;if(!Number.isFinite(e))return e>0?2139095040:4286578688;let t=Math.trunc(e),r=e-t,n=t|0;if(r!==0){let o=Math.floor(r*4294967296);n=X(n,o|0)}return n>>>0}function Ft(e){let t=0;for(let r=0;r<e.length;r++)t=X(t,e.charCodeAt(r));return t>>>0}function Wo(e){let t=0,r=e<0n,n=r?-e:e;if(n===0n)t=X(t,0);else for(;n>0n;){let o=Number(n&0xffn);t=X(t,o),n>>=8n}return X(t,+r)>>>0}function qo(e){let t=Ft((e.name||"")+e.toString());return t=X(t,e.length),t>>>0}function Yo(e){let t=0;for(let r=0;r<e.length;r++)t=X(t,e[r]);return t>>>0}function Vo(e){let t=Ft(e.constructor.name),r=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return t=X(t,Yo(r)),t>>>0}function Ko(e,t){if(t.has(e))return 13;t.add(e);let r=1;for(let n=0;n<e.length;n++)r=X(r,Ar(e[n],t));return t.delete(e),r>>>0}function Qo(e,t){if(t.has(e))return 13;t.add(e);let r=Object.keys(e).sort(),n=Ft(e?.constructor?.name);for(let o of r)n=X(n,Ft(o)),n=X(n,Ar(e[o],t));return t.delete(e),n>>>0}var Ho=[3735928559,305441741].map(e=>X(3,e)),Go=X(1,0),Xo=X(2,0);function Ar(e,t){if(e===null)return Go;switch(typeof e){case"undefined":return Xo;case"boolean":return Ho[+e];case"number":return X(4,On(e));case"string":return X(5,Ft(e));case"bigint":return X(6,Wo(e));case"function":return X(7,qo(e));default:{if(ArrayBuffer.isView(e)&&!(e instanceof DataView))return X(12,Vo(e));if(e instanceof Date)return X(10,On(e.getTime()));if(e instanceof RegExp){let n=Ft(e.source);return X(11,X(n,Ft(e.flags)))}return Array.isArray(e)?X(8,Ko(e,t)):X(9,Qo(e,t))}}}function ft(e){return Ar(e,new WeakSet)>>>0}var kt=class extends Error{},ee=Symbol("missing"),Jo="mingo: cycle detected while processing object/array",It=e=>typeof e!="object"&&typeof e!="function"||e===null,bn=e=>It(e)||R(e)||ot(e),An={undefined:1,null:2,number:3,string:4,symbol:5,object:6,array:7,arraybuffer:8,boolean:9,date:10,regexp:11,function:12},At=(e,t)=>e<t?-1:e>t?1:0,Zo=(e,t)=>{let r=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),n=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),o=Math.min(r.length,n.length);for(let i=0;i<o;i++){let s=At(r[i],n[i]);if(s!==0)return s}return At(r.length,n.length)};function jt(e,t,r=!1){if(e===ee&&(e=void 0),t===ee&&(t=void 0),e===t||Object.is(e,t))return 0;let n=at(e),o=at(t),i=0;if(n===o)switch(n){case"number":case"string":case"boolean":return At(e,t);case"date":return At(+e,+t);case"regexp":return(i=At(e.source,t.source))?i:At(e.flags,t.flags);case"arraybuffer":return Zo(e,t);case"array":{let c=e.slice().sort(jt),p=t.slice().sort(jt),m=Math.min(c.length,p.length);for(let O=0;O<m;O++)if(i=jt(c[O],p[O]))return i;return At(c.length,p.length)}default:{if(n!=="object"&&e?.constructor===t?.constructor&&dn(e))return At(e.toString(),t.toString());let c=Object.keys(e).sort(),p=Object.keys(t).sort();if(i=jt(c,p))return i;for(let m of c)if(i=jt(e[m],t[m]))return i;return 0}}if(n=="undefined")return-1;if(o=="undefined")return 1;if(r){if(n=="array"){let c=e;if(!c.length)return-1;let p=c.slice().sort(jt);i=1;for(let m of p)if((i=Math.min(i,jt(m,t)))<0)return i;return i}if(o=="array"){let c=t;if(!c.length)return 1;let p=c.slice().sort(jt);i=-1;for(let m of p)if((i=Math.max(i,jt(e,m)))>0)return i;return i}}let s=An[n]??Number.MAX_VALUE,a=An[o]??Number.MAX_VALUE;return s!==a?At(s,a):At(n,o)}var M=(e,t)=>jt(e,t,!0),dn=e=>e!=null&&e.toString!==Object.prototype.toString;function Q(e,t){if(e===t||Object.is(e,t))return!0;if(e===null||t===null||typeof e!=typeof t||typeof e!="object"||e.constructor!==t?.constructor)return!1;if(R(e))return R(t)&&+e==+t;if(ot(e))return ot(t)&&e.source===t.source&&e.flags===t.flags;if(l(e)&&l(t))return e.length===t.length&&e.every((s,a)=>Q(s,t[a]));if(e?.constructor!==Object&&dn(e))return e?.toString()===t?.toString();let r=e,n=t,o=Object.keys(r),i=Object.keys(n);return o.length!==i.length?!1:o.every(s=>T(n,s)&&Q(r[s],n[s]))}var et=class e extends Map{#t=new Map;#e=t=>{let r=ft(t);return[(this.#t.get(r)??[]).find(o=>Q(o,t)),r]};constructor(){super()}static init(){return new e}clear(){super.clear(),this.#t.clear()}delete(t){if(It(t))return super.delete(t);let[r,n]=this.#e(t);return super.delete(r)?(this.#t.set(n,this.#t.get(n).filter(o=>!Q(o,r))),!0):!1}get(t){if(It(t))return super.get(t);let[r,n]=this.#e(t);return super.get(r)}has(t){if(It(t))return super.has(t);let[r,n]=this.#e(t);return super.has(r)}set(t,r){if(It(t))return super.set(t,r);let[n,o]=this.#e(t);if(super.has(n))super.set(n,r);else{super.set(t,r);let i=this.#t.get(o)||[];i.push(t),this.#t.set(o,i)}return this}get size(){return super.size}};function u(e,t){if(!e)throw new kt(t)}var fe=e=>{if(e==="__proto__"||e.startsWith("__proto__.")||e.endsWith(".__proto__")||e.includes(".__proto__."))throw new kt(`Accessing __proto__ is not allowed in selector: '${e}'.`)};function at(e){let t=typeof e;switch(t){case"number":case"string":case"boolean":case"undefined":case"function":case"symbol":return t}return e===null?"null":l(e)?"array":R(e)?"date":ot(e)?"regexp":gn(e)?"arraybuffer":e?.constructor===Object?"object":e?.constructor?.name?.toLowerCase()??"object"}var Bt=e=>typeof e=="boolean",$=e=>typeof e=="string";var x=e=>!Number.isNaN(e)&&typeof e=="number",w=Number.isInteger,l=Array.isArray,h=e=>at(e)==="object";var R=e=>e instanceof Date,ot=e=>e instanceof RegExp,br=e=>typeof e=="function",y=e=>e==null,nt=(e,t=!0)=>!!e||t&&e==="",le=e=>y(e)||$(e)&&!e||l(e)&&e.length===0||h(e)&&Object.keys(e).length===0,ct=e=>l(e)?e:[e],T=(e,t,r="",n="")=>!!(e&&Object.prototype.hasOwnProperty.call(e,t)&&(r===""||Object.prototype.hasOwnProperty.call(e,r))&&(n===""||Object.prototype.hasOwnProperty.call(e,n))),gn=e=>typeof ArrayBuffer<"u"&&ArrayBuffer.isView(e),we=e=>{for(let t=0;t<e.length;t++)if(e.charCodeAt(t)<48||e.charCodeAt(t)>57)return!1;return!0},it=(e,t)=>{if(It(e))return e;if(R(e))return new Date(e);if(ot(e))return new RegExp(e);if(gn(e)){let r=e.constructor;return new r(e)}if(t instanceof WeakSet||(t=new WeakSet),t.has(e))throw new Error(Jo);t.add(e);try{if(l(e)){let r=new Array(e.length);for(let n=0;n<e.length;n++)r[n]=it(e[n],t);return r}if(h(e)){let r={};for(let n of Object.keys(e))r[n]=it(e[n],t);return r}}finally{t.delete(e)}return e};function Lt(e){if(e.length===0)return[];if(e.length===1)return e[0].slice();for(let n of e)if(n.length===0)return[];let t=[et.init(),et.init()],r=0;for(let n of e[e.length-1])t[0].set(n,!0);for(let n=e.length-2;n>=0;n--){for(let o=0;o<e[n].length;o++){let i=e[n][o];t[r].has(i)&&t[r^1].set(i,!0)}if(t[r^1].size===0)return[];t[r].clear(),r=r^1}return Array.from(t[r].keys())}function pt(e,t=1){let r=new Array;function n(o,i){for(let s=0,a=o.length;s<a;s++)l(o[s])&&(i>0||i<0)?n(o[s],Math.max(-1,i-1)):r.push(o[s])}return n(e,t),r}function $t(e){let t=et.init();for(let r of e)t.set(r,!0);return Array.from(t.keys())}function zt(e,t){if(e.length<1)return new Map;let r=et.init();for(let n=0;n<e.length;n++){let o=e[n],i=t(o,n)??null,s=r.get(i);s?s.push(o):(s=[o],r.set(i,s))}return r}var ti=new Set(Object.getOwnPropertyNames(Object.prototype).find(e=>e!=="__proto__"));function Ne(e,t){if(!(It(e)||Number.isNaN(t)))return l(e)?e[Number(t)]:!ti.has(t)||T(e,t)?e[t]:void 0}function ei(e,t){if(t<1)return e;for(;t--&&e.length===1&&l(e[0]);)e=e[0];return e}function D(e,t,r){if(fe(t),bn(e))return e;if(!t.includes(".")&&!l(e))return Ne(e,t);let n=0;function o(s,a){let c=s,p=0,m=0;for(;m!==-1;){m=a.indexOf(".",p);let O=m===-1?a.substring(p):a.substring(p,m);if(!we(O)&&l(c)){if(p===0&&n>0)break;n+=1;let b=a.substring(p);c=c.reduce((d,g)=>{let E=o(g,b);return E!==void 0&&d.push(E),d},[]);break}else c=Ne(c,O);if(c===void 0)break;p+=O.length+1}return c}let i=o(e,t);return l(i)&&r?.unwrapArray?ei(i,n):i}function Tt(e,t,r){r?.ignoreProto!==!0&&(fe(t),r={...r,ignoreProto:!0});let n=t.indexOf("."),o=n==-1?t:t.substring(0,n),i=t.substring(n+1),s=n!=-1,{preserveIndex:a}=r;if(l(e)){let m=we(o),O=m&&a==="default"?e.slice():[];if(m){let A=Number(o),b=Ne(e,o);s&&(b=Tt(b,i,r)),a==="default"?O[A]=b:O.push(b)}else for(let A=0;A<e.length;A++){let b=Tt(e[A],t,r);a==="missing"?O.push(b??ee):(b!==void 0||a==="default")&&O.push(b)}return O}let c=r?.preserveKeys?{...e}:{},p=Ne(e,o);if(s&&(p=Tt(p,i,r)),p!==void 0)return c[o]=p,c}function Se(e){if(l(e))for(let t=e.length-1;t>=0;t--)e[t]===ee?e.splice(t,1):Se(e[t]);else if(h(e)){let t=Object.keys(e);for(let r=0;r<t.length;r++)Se(e[t[r]])}}function re(e,t,r,n){n?.ignoreProto!==!0&&(fe(t),n={...n,ignoreProto:!0});let o=t.indexOf("."),i=o===-1?t:t.substring(0,o),s=t.substring(i.length+1);if(s.length===0)(h(e)||l(e)&&we(i))&&r(e,i);else{n?.buildGraph&&y(e[i])&&(e[i]={});let a=e[i];if(!a)return;let c=s.indexOf("."),p=c===-1?s:s.substring(0,c),m=we(p);if(l(a)&&n?.descendArray&&!m)for(let O=0;O<a.length;O++)re(a[O],s,r,n);else re(a,s,r,n)}}function yt(e,t,r){re(e,t,(n,o)=>n[o]=r,{buildGraph:!0})}function Wt(e,t,r){re(e,t,((n,o)=>{l(n)?n.splice(Number(o),1):delete n[o]}),r)}var ut=e=>!!e&&e[0]==="$"&&/^\$[a-zA-Z0-9_]+$/.test(e);function qt(e){if(bn(e))return ot(e)?{$regex:e}:{$eq:e};if(!Object.keys(e).some(ut))return{$eq:e};if(h(e)&&T(e,"$regex")){let t={...e};return t.$regex=new RegExp(e.$regex,e.$options),delete t.$options,t}return e}function Yt(e,t,r=M){let n=0,o=e.length-1;for(;n<=o;){let i=Math.round(n+(o-n)/2);if(r(t,e[i])<0)o=i-1;else if(r(t,e[i])>0)n=i+1;else return i}return n}var vt=class{constructor(){this.root={children:new Map,isTerminal:!1}}add(t){let r=t.split("."),n=this.root;for(let o of r){if(n.isTerminal)return!1;n.children.has(o)||n.children.set(o,{children:new Map,isTerminal:!1}),n=n.children.get(o)}return n.isTerminal||n.children.size?!1:n.isTerminal=!0}};var wt=(o=>(o[o.CLONE_OFF=0]="CLONE_OFF",o[o.CLONE_INPUT=1]="CLONE_INPUT",o[o.CLONE_OUTPUT=2]="CLONE_OUTPUT",o[o.CLONE_ALL=3]="CLONE_ALL",o))(wt||{}),C=class e{constructor(t,r){this.options=t;this.#t=r?{...r}:{}}#t;static init(t){return t instanceof e?new e(t.options,t.#t):new e({idKey:"_id",scriptEnabled:!0,useStrictMode:!0,failOnError:!0,processingMode:0,...t,context:t?.context?Ot.from(t?.context):Ot.init()})}update(t){return Object.assign(this.#t,t,{timestamp:this.#t.timestamp,variables:{...this.#t?.variables,...t?.variables}}),this}get local(){return this.#t}get now(){let t=this.#t.timestamp??0;return t||(t=Date.now(),Object.assign(this.#t,{timestamp:t})),new Date(t)}get idKey(){return this.options.idKey}get collation(){return this.options?.collation}get processingMode(){return this.options?.processingMode}get useStrictMode(){return this.options?.useStrictMode}get scriptEnabled(){return this.options?.scriptEnabled}get failOnError(){return this.options?.failOnError}get collectionResolver(){return this.options?.collectionResolver}get jsonSchemaValidator(){return this.options?.jsonSchemaValidator}get variables(){return this.options?.variables}get context(){return this.options?.context}},Vt=(s=>(s.ACCUMULATOR="accumulator",s.EXPRESSION="expression",s.PIPELINE="pipeline",s.PROJECTION="projection",s.QUERY="query",s.WINDOW="window",s))(Vt||{}),Ot=class e{#t;constructor(){this.#t={accumulator:{},expression:{},pipeline:{},projection:{},query:{},window:{}}}static init(t={}){let r=new e;for(let n of Object.keys(t))r.#t[n]={...t[n]};return r}static from(...t){if(t.length===1)return e.init(t[0].#t);let r=new e;for(let n of t)for(let o of Object.values(Vt))r.addOps(o,n.#t[o]);return r}addOps(t,r){return this.#t[t]=Object.assign({},r,this.#t[t]),this}getOperator(t,r){return this.#t[t][r]??null}addAccumulatorOps(t){return this.addOps("accumulator",t)}addExpressionOps(t){return this.addOps("expression",t)}addQueryOps(t){return this.addOps("query",t)}addPipelineOps(t){return this.addOps("pipeline",t)}addProjectionOps(t){return this.addOps("projection",t)}addWindowOps(t){return this.addOps("window",t)}};function f(e,t,r){let n=!(r instanceof C)||y(r.local.root)?C.init(r).update({root:e}):r;return Ce(e,t,n)}var ri=new Set(["$$ROOT","$$CURRENT","$$REMOVE","$$NOW"]);function Ce(e,t,r){if($(t)&&t.length>0&&t[0]==="$"){if(t==="$$KEEP"||t==="$$PRUNE"||t==="$$DESCEND")return t;let n=r.local.root,o=t.indexOf("."),i=o===-1?t:t.substring(0,o);if(ri.has(i)){switch(i){case"$$ROOT":break;case"$$CURRENT":n=e;break;case"$$REMOVE":n=void 0;break;case"$$NOW":n=new Date(r.now);break}t=o===-1?"":t.substring(o+1)}else if(i.length>=2&&i[1]==="$"){n=Object.assign({},r.variables,{this:e},r?.local?.variables);let s=i.substring(2);u(T(n,s),`Use of undefined variable: ${s}`),t=t.substring(2)}else t=t.substring(1);return t===""?n:D(n,t)}if(l(t)){let n=new Array(t.length);for(let o=0;o<t.length;o++)n[o]=Ce(e,t[o],r);return n}if(h(t)){let n=Object.keys(t);if(ut(n[0]))return u(n.length===1,"Expression must contain a single operator."),ni(e,t[n[0]],n[0],r);let o={};for(let i=0;i<n.length;i++)o[n[i]]=Ce(e,t[n[i]],r);return o}return t}function ni(e,t,r,n){let o=n.context,i=o.getOperator("expression",r);if(i)return i(e,t,n);let s=o.getOperator("accumulator",r);return u(!!s,`accumulator '${r}' is not registered.`),l(e)||(e=Ce(e,t,n),t=null),u(l(e),`arguments must resolve to array for ${r}.`),s(e,t,n)}function S(e){return new ye(e)}function Nt(...e){let t=0;return S(()=>{for(;t<e.length;){let r=e[t].next();if(!r.done)return r;t++}return{done:!0,value:void 0}})}function oi(e){return!!e&&typeof e=="object"&&typeof e?.next=="function"}function ii(e){return!!e&&(typeof e=="object"||typeof e=="function")&&typeof e[Symbol.iterator]=="function"}var ye=class{#t=[];#e=[];#r;#o=!1;constructor(t){let r;ii(t)?r=t[Symbol.iterator]():oi(t)?r=t:typeof t=="function"?r={next:t}:u(0,"mingo: iterator requires an iterable, generator or function");let n=-1;this.#r=()=>{for(;;){let{value:o,done:i}=r.next();if(i)return{done:i};let s=!0;n++;for(let a=0;a<this.#t.length;a++){let{op:c,fn:p}=this.#t[a],m=p(o,n);if(c==="map")o=m;else if(!m){s=!1;break}}if(s)return{value:o,done:i}}}}push(t,r){return this.#t.push({op:t,fn:r}),this}next(){return this.#r()}map(t){return this.push("map",t)}filter(t){return this.push("filter",t)}take(t){return u(t>=0,"value must be a non-negative integer"),this.filter(r=>t-- >0)}drop(t){return u(t>=0,"value must be a non-negative integer"),this.filter(r=>t--<=0)}transform(t){let r=this,n;return S(()=>(n||(n=t(r.collect())),n.next()))}collect(){for(;!this.#o;){let{done:t,value:r}=this.#r();t||this.#e.push(r),this.#o=t}return this.#e}each(t){for(let r=this.next();r.done!==!0;r=this.next())t(r.value)}reduce(t,r){let n=this.next();for(r===void 0&&!n.done&&(r=n.value,n=this.next());!n.done;)r=t(r,n.value),n=this.next();return r}size(){return this.collect().length}[Symbol.iterator](){return this}};var bt=class{#t;#e;constructor(t,r){this.#t=t,this.#e=C.init(r)}stream(t,r){let n=S(t),o=r??this.#e,i=o.processingMode;return i&1&&n.map(s=>it(s)),n=this.#t.map((s,a)=>{let c=Object.keys(s);u(c.length===1,`aggregation stage must have single operator, got ${c.toString()}.`);let p=c[0];u(p!=="$documents"||a==0,"$documents must be first stage in pipeline.");let m=o.context.getOperator("pipeline",p);return u(!!m,`unregistered pipeline operator ${p}.`),[m,s[p]]}).reduce((s,[a,c])=>a(s,c,o),n),i&2&&n.map(s=>it(s)),n}run(t,r){return this.stream(t,r).collect()}};var Er={};ht(Er,{$accumulator:()=>si,$addToSet:()=>ai,$avg:()=>ci,$bottom:()=>mi,$bottomN:()=>dr,$count:()=>fi,$covariancePop:()=>li,$covarianceSamp:()=>yi,$first:()=>gr,$firstN:()=>De,$last:()=>xr,$lastN:()=>Re,$max:()=>Oi,$maxN:()=>Pe,$median:()=>hr,$mergeObjects:()=>jr,$min:()=>Ai,$minN:()=>Ue,$percentile:()=>Oe,$push:()=>N,$stdDevPop:()=>bi,$stdDevSamp:()=>di,$sum:()=>gi,$top:()=>xi,$topN:()=>$r});var N=(e,t,r)=>{if(y(t))return e;let n=C.init(r),o=new Array(e.length);for(let i=0;i<e.length;i++){let s=e[i];o[i]=f(s,t,n.update({root:s}))??null}return o};var si=(e,t,r)=>{u(r.scriptEnabled,"$accumulator requires 'scriptEnabled' option to be true");let n=C.init(r),o=t,i=f(n?.local?.groupId,o.initArgs||[],n.update({root:n?.local?.groupId})),s=N(e,o.accumulateArgs,n);for(let m=0;m<s.length;m++)for(let O=0;O<s[m].length;O++)s[m][O]=s[m][O]??null;let a=o.init.apply(null,i),c=o.accumulate,p=a;for(let m=0;m<s.length;m++)p=c.apply(null,[p,...s[m]]);return o.finalize&&(p=o.finalize.call(null,p)),p};var ai=(e,t,r)=>$t(N(e,t,r));var ci=(e,t,r)=>{let n=N(e,t,r).filter(x);if(n.length===0)return null;let o=0;for(let i=0;i<n.length;i++)o+=n[i];return o/n.length};function st(e,t,r){u(h(t)&&Object.keys(t).length>0,"$sort specification is invalid");let n=M,o=r.collation;return h(o)&&$(o.locale)&&(n=ui(o)),e.transform(i=>{let s=Object.keys(t);for(let a of s.reverse()){let c=zt(i,A=>D(A,a)),p=Array.from(c.keys()),m=!1;if(n===M){let A=!0,b=!0;for(let d of p)if(A&&=$(d),b&&=x(d),!A&&!b)break;if(m=A||b,A)p.sort();else if(b){let d=new Float64Array(p).sort();for(let g=0;g<d.length;g++)p[g]=d[g]}}m||p.sort(n),t[a]===-1&&p.reverse();let O=0;for(let A of p)for(let b of c.get(A))i[O++]=b;u(O==i.length,"bug: counter must match collection size.")}return S(i)})}var pi={1:"base",2:"accent",3:"variant"};function ui(e){let t={sensitivity:pi[e.strength||3],caseFirst:e.caseFirst==="off"?"false":e.caseFirst,numeric:e.numericOrdering||!1,ignorePunctuation:e.alternate==="shifted"};e.caseLevel===!0&&(t.sensitivity==="base"&&(t.sensitivity="case"),t.sensitivity==="accent"&&(t.sensitivity="variant"));let r=new Intl.Collator(e.locale,t);return(n,o)=>$(n)&&$(o)?r.compare(n,o):M(n,o)}var dr=(e,t,r)=>{let n=r,o=t,i=f(n?.local?.groupId,o.n,n),s=st(S(e),o.sortBy,r).collect(),a=s.length;return N(a<=i?s:s.slice(a-i),o.output,n)};var mi=(e,t,r)=>dr(e,{...t,n:1},r);var fi=(e,t,r)=>e.length;function ke(e,t=!0){let r=0;for(let i=0;i<e.length;i++)r+=e[i];let n=r/e.length,o=0;for(let i=0;i<e.length;i++){let s=e[i]-n;o+=s*s}return Math.sqrt(o/(e.length-Number(t)))}function _e(e,t=!0){if(e.length<2)return t?null:0;let r=0,n=0;for(let i=0;i<e.length;i++){let[s,a]=e[i];r+=s,n+=a}r/=e.length,n/=e.length;let o=0;for(let i=0;i<e.length;i++){let[s,a]=e[i];o+=(s-r)*(a-n)}return o/(e.length-Number(t))}var li=(e,t,r)=>_e(N(e,t,r),!1);var yi=(e,t,r)=>_e(N(e,t,r),!0);var gr=(e,t,r)=>{let n=e[0],o=C.init(r).update({root:n});return f(n,t,o)??null};var xn={int:{type:"integers"},obj:{type:"objects"}};function L(e,t){return u(!e,t),null}function Me(e,t){let r=`${t} expression must resolve to object`;return u(!e,r),null}function Z(e,t){let r=`${t} expression must resolve to string`;return u(!e,r),null}function H(e,t,r){let n=r?.integer?"integer":"number",o=r?.min??-1/0,i=r?.max??1/0,s;return o===0&&i===0?s=`${t} expression must resolve to non-zero ${n}`:o===0&&i===1/0?s=`${t} expression must resolve to non-negative ${n}`:o!==-1/0&&i!==1/0?s=`${t} expression must resolve to ${n} in range [${o}, ${i}]`:o>0?s=`${t} expression must resolve to positive ${n}`:s=`${t} expression must resolve to ${n}`,u(!e,s),null}var F=(e,t,r)=>H(e,t,{...r,integer:!0});function j(e,t,r){let n="array";!y(r?.size)&&r?.size>=0&&(n=r.size===0?"non-zero array":`array(${r.size})`),r?.type&&(n=`array of ${r.type}`);let o=`${t} expression must resolve to ${n}`;return u(!e,o),null}var De=(e,t,r)=>{let n=r.failOnError,o=r,i=e.length,s=f(o?.local?.groupId,t.n,o);return!w(s)||s<1?F(n,"$firstN 'n'",{min:1}):N(i<=s?e:e.slice(0,s),t.input,r)};var xr=(e,t,r)=>{let n=e[e.length-1],o=C.init(r).update({root:n});return f(n,t,o)??null};var Re=(e,t,r)=>{let n=r,o=e.length,i=f(n?.local?.groupId,t.n,n),s=r.failOnError;return!w(i)||i<1?F(s,"$lastN 'n'",{min:1}):N(o<=i?e:e.slice(o-i),t.input,r)};var Oi=(e,t,r)=>{let n=N(e,t,r).slice().sort(M),o=n.length;return y(n[o-1])?null:n[o-1]};var Pe=(e,t,r)=>{let n=r,o=f(n?.local?.groupId,t.n,n);return!w(o)||o<1?F(r.failOnError,"$maxN 'n'",{min:1}):N(e,t.input,r).filter(i=>!y(i)).sort(M).slice(-o).reverse()};var Oe=(e,t,r)=>{u(h(t)&&T(t,"input","p")&&l(t.p),"$percentile expects object { input, p }");let n=N(e,t.input,r).filter(x).sort(),o=N(t.p,"$$CURRENT",r),i=t.method||"approximate",s=new Array(o.length);for(let a=0;a<o.length;a++){let c=o[a];if(!x(c)||c<0||c>1)return L(r.failOnError,"$percentile 'p' must resolve to array of numbers between [0.0, 1.0]");let p=c*(n.length-1)+1,m=Math.floor(p),O=p===m?n[p-1]:n[m-1]+p%1*(n[m]-n[m-1]);switch(i){case"exact":s[a]=O;break;case"approximate":{let A=Yt(n,O);s[a]=A/n.length>=c?n[Math.max(A-1,0)]:n[A];break}}}return s};var hr=(e,t,r)=>Oe(e,{...t,p:[.5]},r)?.pop();var jr=(e,t,r)=>{let n={};for(let o of e)if(!y(o))for(let i of Object.keys(o))o[i]!==void 0&&(n[i]=o[i]);return n};var Ai=(e,t,r)=>{let n=N(e,t,r).filter(o=>!y(o)).sort(M);return y(n[0])?null:n[0]};var Ue=(e,t,r)=>{let n=r,o=f(n?.local?.groupId,t.n,n);return!w(o)||o<1?F(r.failOnError,"$minN 'n'",{min:1}):N(e,t.input,r).filter(i=>!y(i)).sort(M).slice(0,o)};var bi=(e,t,r)=>ke(N(e,t,r).filter(x),!1);var di=(e,t,r)=>ke(N(e,t,r).filter(x),!0);var gi=(e,t,r)=>{if(x(t))return e.length*t;let n=N(e,t,r),o=0;for(let i=0;i<n.length;i++)x(n[i])&&(o+=n[i]);return o};var $r=(e,t,r)=>{let n=r,{n:o,sortBy:i}=f(n?.local?.groupId,t,n),s=st(S(e),i,r).take(o).collect();return N(s,t.output,n)};var xi=(e,t,r)=>$r(e,{...t,n:1},r);var Vr={};ht(Vr,{$abs:()=>hi,$acos:()=>Pa,$acosh:()=>Ua,$add:()=>ji,$allElementsTrue:()=>sa,$and:()=>$n,$anyElementTrue:()=>aa,$arrayElemAt:()=>Ui,$arrayToObject:()=>Fi,$asin:()=>Fa,$asinh:()=>Ba,$atan:()=>La,$atan2:()=>za,$atanh:()=>Wa,$bitAnd:()=>os,$bitNot:()=>is,$bitOr:()=>ss,$bitXor:()=>as,$ceil:()=>$i,$cmp:()=>Tn,$concat:()=>la,$concatArrays:()=>Bi,$cond:()=>Os,$convert:()=>Na,$cos:()=>qa,$cosh:()=>Ya,$dateAdd:()=>xe,$dateDiff:()=>$s,$dateFromParts:()=>Ts,$dateFromString:()=>Cs,$dateSubtract:()=>ks,$dateToParts:()=>_s,$dateToString:()=>Ds,$dateTrunc:()=>Us,$dayOfMonth:()=>Fs,$dayOfWeek:()=>Bs,$dayOfYear:()=>Ls,$degreesToRadians:()=>Ka,$divide:()=>Ei,$eq:()=>Fn,$exp:()=>Ii,$filter:()=>Li,$first:()=>zi,$firstN:()=>Wi,$floor:()=>Ti,$function:()=>Mr,$getField:()=>ta,$gt:()=>Bn,$gte:()=>Ln,$hour:()=>zs,$ifNull:()=>_r,$in:()=>qi,$indexOfArray:()=>Yi,$indexOfBytes:()=>ya,$isArray:()=>Vi,$isNumber:()=>Sa,$isoDayOfWeek:()=>Ws,$isoWeek:()=>qs,$isoWeekYear:()=>Ys,$last:()=>Ki,$lastN:()=>Qi,$let:()=>tc,$literal:()=>Js,$ln:()=>vi,$log:()=>wi,$log10:()=>Ni,$lt:()=>zn,$lte:()=>Wn,$ltrim:()=>Aa,$map:()=>Hi,$maxN:()=>Gi,$median:()=>Zs,$mergeObjects:()=>Ur,$millisecond:()=>Vs,$minN:()=>Xi,$minute:()=>Ks,$mod:()=>Si,$month:()=>Qs,$multiply:()=>Ci,$ne:()=>qn,$not:()=>En,$objectToArray:()=>na,$or:()=>In,$percentile:()=>ia,$pow:()=>ki,$radiansToDegrees:()=>Ha,$rand:()=>ea,$range:()=>Ji,$reduce:()=>Zi,$regexFind:()=>ba,$regexFindAll:()=>da,$regexMatch:()=>ga,$replaceAll:()=>xa,$replaceOne:()=>ha,$reverseArray:()=>ts,$round:()=>_i,$rtrim:()=>ja,$sampleRate:()=>ra,$second:()=>Hs,$setDifference:()=>ca,$setEquals:()=>pa,$setField:()=>Fr,$setIntersection:()=>ua,$setIsSubset:()=>ma,$setUnion:()=>fa,$sigmoid:()=>Mi,$sin:()=>Ga,$sinh:()=>Xa,$size:()=>es,$slice:()=>Tr,$sortArray:()=>rs,$split:()=>$a,$sqrt:()=>Di,$strLenBytes:()=>Ia,$strLenCP:()=>Ta,$strcasecmp:()=>Ea,$substr:()=>va,$substrBytes:()=>Lr,$substrCP:()=>wa,$subtract:()=>Ri,$switch:()=>As,$tan:()=>Ja,$tanh:()=>Za,$toBool:()=>zr,$toDate:()=>Wr,$toDecimal:()=>Ca,$toDouble:()=>je,$toHashedIndexKey:()=>Ra,$toInt:()=>qr,$toLong:()=>Yr,$toLower:()=>_a,$toString:()=>Gt,$toUpper:()=>Ma,$trim:()=>Da,$trunc:()=>Pi,$type:()=>ka,$unsetField:()=>oa,$week:()=>Gs,$year:()=>Xs,$zip:()=>ns});var hi=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?H(r.failOnError,"$abs"):Math.abs(n)};var hn="$add expression must resolve to array of numbers.",ji=(e,t,r)=>{let n=f(e,t,r),o=r.failOnError,i=!1,s=0;if(!l(n))return L(o,hn);for(let a of n){if(y(a))return null;if(typeof a=="number")s+=a;else if(R(a)){if(i)return L(o,"$add must only have one date");i=!0,s+=+a}else return L(o,hn)}return i?new Date(s):s};var $i=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?H(r.failOnError,"$ceil"):Math.ceil(n)};var Ei=(e,t,r)=>{u(l(t),"$divide expects array(2)");let n=f(e,t,r),o=r.failOnError,i=!0;for(let s of n){if(y(s))return null;i&&=x(s)}return i?n[1]===0?L(o,"$divide cannot divide by zero"):n[0]/n[1]:j(o,"$divide",{size:2,type:"number"})};var Ii=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?H(r.failOnError,"$exp"):Math.exp(n)};var Ti=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?H(r.failOnError,"$floor"):Math.floor(n)};var vi=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?H(r.failOnError,"$ln"):Math.log(n)};var wi=(e,t,r)=>{let n=f(e,t,r);if(l(n)&&n.length==2){let o=!0;for(let i of n){if(y(i))return null;o&&=typeof i=="number"}if(o)return Math.log10(n[0])/Math.log10(n[1])}return j(r.failOnError,"$log",{size:2,type:"number"})};var Ni=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n=="number"?Math.log10(n):H(r.failOnError,"$log10")};var Si=(e,t,r)=>{let n=f(e,t,r),o=!l(n)||n.length!=2;return o||=!n.every(i=>typeof i=="number"),o?j(r.failOnError,"$mod",{size:2,type:"number"}):n[0]%n[1]};var Ci=(e,t,r)=>{u(l(t),"$multiply expects array");let n=f(e,t,r),o=r.failOnError;if(n.some(y))return null;let i=1;for(let s of n){if(!x(s))return j(o,"$multiply",{type:"number"});i*=s}return i};var ki=(e,t,r)=>{u(l(t)&&t.length===2,"$pow expects array(2)");let n=f(e,t,r),o=r.failOnError,i=!0;for(let s of n){if(y(s))return null;i&&=x(s)}return i?(n[0]===0&&n[1]<0&&L(o,"$pow cannot raise 0 to a negative exponent"),Math.pow(n[0],n[1])):j(o,"$pow",{size:2,type:"number"})};function Fe(e,t,r){let{name:n,roundOff:o,failOnError:i}=r;if(y(e))return null;if(Number.isNaN(e)||Math.abs(e)===1/0)return e;if(!x(e))return H(i,`${n} arg1 <number>`);if(!w(t)||t<-20||t>100)return F(i,`${n} arg2 <precision>`,{min:-20,max:100});let s=Math.abs(e)===e?1:-1;e=Math.abs(e);let a=Math.trunc(e),c=parseFloat((e-a).toFixed(Math.abs(t)+1));if(t===0){let p=Math.trunc(10*c);o&&((a&1)===1&&p>=5||p>5)&&a++}else if(t>0){let p=Math.pow(10,t),m=Math.trunc(c*p),O=Math.trunc(c*p*10)%10;o&&O>5&&(m+=1),a=(a*p+m)/p}else{let p=Math.pow(10,-1*t),m=a%p;if(a=Math.max(0,a-m),o&&s===-1){for(;m>10;)m-=m/10;a>0&&m>=5&&(a+=p)}}return a*s}var _i=(e,t,r)=>{u(l(t),"$round expects array(2)");let[n,o]=f(e,t,r);return Fe(n,o??0,{name:"$round",roundOff:!0,failOnError:r.failOnError})};var jn=1e10,Mi=(e,t,r)=>{if(y(t))return null;let n=f(e,t,r),{input:o,onNull:i}=h(n)?n:{input:n};if(y(o))return x(i)?i:null;if(x(o)){let s=1/(1+Math.exp(-o));return Math.round(s*jn)/jn}return H(r.failOnError,"$sigmoid")};var Di=(e,t,r)=>{let n=f(e,t,r),o=!r.failOnError;return y(n)?null:typeof n!="number"||n<0?(u(o,"$sqrt expression must resolve to non-negative number."),null):Math.sqrt(n)};var Ri=(e,t,r)=>{u(l(t),"$subtract expects array(2)");let n=f(e,t,r);if(n.some(y))return null;let o=r.failOnError,[i,s]=n;return R(i)&&x(s)?new Date(+i-Math.round(s)):R(i)&&R(s)?+i-+s:n.every(a=>typeof a=="number")?i-s:x(i)&&R(s)?L(o,"$subtract cannot subtract date from number"):j(o,"$subtract",{size:2,type:"number|date"})};var Pi=(e,t,r)=>{u(l(t),"$trunc expects array(2)");let[n,o]=f(e,t,r);return Fe(n,o??0,{name:"$trunc",roundOff:!1,failOnError:r.failOnError})};var Ir="$arrayElemAt",Ui=(e,t,r)=>{u(l(t)&&t.length===2,`${Ir} expects array(2)`);let n=f(e,t,r);if(n.some(y))return null;let o=r.failOnError,[i,s]=n;if(!l(i))return j(o,`${Ir} arg1 <array>`);if(!w(s))return F(o,`${Ir} arg2 <index>`);if(s<0&&Math.abs(s)<=i.length)return i[(s+i.length)%i.length];if(s>=0&&s<i.length)return i[s]};var Be={generic:{type:"key-value pairs"},array:{type:"[k,v]"},object:{type:"{k,v}"}},Fi=(e,t,r)=>{let n=r.failOnError,o=f(e,t,r);if(y(o))return null;if(!l(o))return j(n,"$arrayToObject",Be.generic);let i=0,s={};for(let a of o)if(l(a)){let c=pt(a);if(i||(i=1),i!==1)return j(n,"$arrayToObject",Be.object);let[p,m]=c;s[p]=m}else if(h(a)&&T(a,"k","v")){if(i||(i=2),i!==2)return j(n,"$arrayToObject",Be.array);let{k:c,v:p}=a;s[c]=p}else return j(n,"$arrayToObject",Be.generic);return s};var Bi=(e,t,r)=>{let n=f(e,t,r),o=r.failOnError;if(y(n))return null;if(!l(n))return j(o,"$concatArrays");let i=0;for(let c of n){if(y(c))return null;if(!l(c))return j(o,"$concatArrays");i+=c.length}let s=new Array(i),a=0;for(let c of n)for(let p of c)s[a++]=p;return s};var Li=(e,t,r)=>{u(h(t)&&T(t,"input","cond"),"$filter expects object { input, as, cond, limit }");let n=f(e,t.input,r),o=r.failOnError;if(y(n))return null;if(!l(n))return j(o,"$filter 'input'");let i=t.limit??Math.max(n.length,1);if(!w(i)||i<1)return F(o,"$filter 'limit'",{min:1});if(n.length===0)return[];let s=C.init(r),a=t?.as||"this",c={variables:{}},p=[];for(let m=0,O=0;m<n.length&&O<i;m++){c.variables[a]=n[m];let A=f(e,t.cond,s.update(c));nt(A,r.useStrictMode)&&(p.push(n[m]),O++)}return p};var zi=(e,t,r)=>{if(l(e))return gr(e,t,r);let n=f(e,t,r);return y(n)?null:l(n)?pt(n)[0]:j(r.failOnError,"$first")};var Wi=(e,t,r)=>{if(u(h(t)&&T(t,"input","n"),"$firstN expects object { input, n }"),l(e))return De(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?De(n,{n:o,input:"$$this"},r):j(r.failOnError,"$firstN 'input'")};var qi=(e,t,r)=>{u(l(t)&&t.length===2,"$in expects array(2)");let n=f(e,t,r),[o,i]=n;if(!l(i))return L(r.failOnError,"$in arg2 <array>");for(let s of i)if(Q(s,o))return!0;return!1};var Le="$indexOfArray",Yi=(e,t,r)=>{u(l(t)&&t.length>1&&t.length<5,`${Le} expects array(4)`);let n=f(e,t,r),o=r.failOnError,i=n[0];if(y(i))return null;if(!l(i))return j(o,`${Le} arg1 <array>`);let s=n[1],a=n[2]??0,c=n[3]??i.length;return!w(a)||a<0?F(o,`${Le} arg3 <start>`,{min:1}):!w(c)||c<0?F(o,`${Le} arg4 <end>`,{min:1}):a>c?-1:(a>0||c<i.length?i.slice(a,c):i).findIndex(m=>Q(m,s))+a};var Vi=(e,t,r)=>{let n=t;return l(t)&&(u(t.length===1,"$isArray expects array(1)"),n=t[0]),l(f(e,n,r))};var Ki=(e,t,r)=>{if(l(e))return xr(e,t,r);let n=f(e,t,r);return y(n)?null:!l(n)||n.length===0?j(r.failOnError,"$last",{size:0}):pt(n)[n.length-1]};var Qi=(e,t,r)=>{if(u(h(t)&&T(t,"input","n"),"$lastN expects object { input, n }"),l(e))return Re(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?Re(n,{n:o,input:"$$this"},r):j(r.failOnError,"$lastN 'input'")};var Hi=(e,t,r)=>{u(h(t)&&T(t,"input","in"),"$map expects object { input, as, in }");let n=f(e,t.input,r),o=r.failOnError;if(y(n))return null;if(!l(n))return j(o,"$map 'input'");if(!y(t.as)&&!$(t.as))return Z(o,"$map 'as'");let i=C.init(r),s=t.as||"this",a={variables:{}},c=new Array(n.length);for(let p=0;p<n.length;p++)a.variables[s]=n[p],c[p]=f(e,t.in,i.update(a));return c};var Gi=(e,t,r)=>{if(u(h(t)&&T(t,"input","n"),"$maxN expects object { input, n }"),l(e))return Pe(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?Pe(n,{n:o,input:"$$this"},r):j(r.failOnError,"$maxN 'input'")};var Xi=(e,t,r)=>{if(u(h(t)&&T(t,"input","n"),"$minN expects object { input, n }"),l(e))return Ue(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?Ue(n,{n:o,input:"$$this"},r):j(r.failOnError,"$minN 'input'")};var Ji=(e,t,r)=>{u(l(t)&&t.length>1&&t.length<4,"$range expects array(3)");let[n,o,i]=f(e,t,r),s=r.failOnError,a=i??1;if(!w(n))return F(s,"$range arg1 <start>");if(!w(o))return F(s,"$range arg2 <end>");if(!w(a)||a===0)return F(s,"$range arg3 <step>",{min:0,max:0});let c=new Array,p=n;for(;p<o&&a>0||p>o&&a<0;)c.push(p),p+=a;return c};function Zi(e,t,r){u(h(t)&&T(t,"input","initialValue","in"),"$reduce expects object { input, initialValue, in }");let n=f(e,t.input,r),o=f(e,t.initialValue,r),i=t.in;if(y(n))return null;if(!l(n))return j(r.failOnError,"$reduce 'input'");let s=C.init(r),a={variables:{value:null}},c=o;for(let p=0;p<n.length;p++)a.variables.value=c,c=f(n[p],i,s.update(a));return c}var ts=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:l(n)?n.slice().reverse():j(r.failOnError,"$reverseArray")};var es=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:l(n)?n.length:H(r.failOnError,"$size")};var Tr=(e,t,r)=>{u(l(t)&&t.length>1&&t.length<4,"$slice expects array(3)");let n=r.failOnError,o=f(e,t,r),i=o[0],s=o[1],a=o[2];if(!l(i))return j(n,"$slice arg1 <array>");if(!w(s))return F(n,"$slice arg2 <n>");if(!y(a)&&!w(a))return F(n,"$slice arg3 <n>");if(y(a))s<0?s=Math.max(0,i.length+s):(a=s,s=0);else{if(s<0&&(s=Math.max(0,i.length+s)),a<1)return F(n,"$slice arg3 <n>",{min:1});a+=s}return i.slice(s,a)};var rs=(e,t,r)=>{u(h(t)&&"input"in t&&"sortBy"in t,"$sortArray expects object { input, sortBy }");let{input:n,sortBy:o}=f(e,t,r);if(y(n))return null;if(!l(n))return j(r.failOnError,"$sortArray 'input'");if(h(o))return st(S(n),o,r).collect();let i=n.slice().sort(M);return o===-1&&i.reverse(),i};var ns=(e,t,r)=>{u(h(t)&&T(t,"inputs"),"$zip received invalid arguments");let n=f(e,t.inputs,r),o=f(e,t.defaults,r)??[],i=t.useLongestLength??!1,s=r.failOnError;if(y(n))return null;if(!l(n))return j(s,"$zip 'inputs'");let a=0;for(let m of n){if(y(m))return null;l(m)||a++}if(a)return j(s,"$zip elements of 'inputs'");Bt(i)||L(s,"$zip 'useLongestLength' must be boolean"),l(o)&&o.length>0&&u(i&&o.length===n.length,"$zip 'useLongestLength' must be set to true to use 'defaults'");let c=0;for(let m of n)c=i?Math.max(c,m.length):Math.min(c||m.length,m.length);let p=[];for(let m=0;m<c;m++){let O=new Array(n.length);for(let A=0;A<n.length;A++){let b=n[A][m];O[A]=y(b)?o[A]??null:b}p.push(O)}return p};function ne(e,t,r,n,o){u(l(t),`${n} expects array as argument`);let i=f(e,t,r),s=!0;for(let a of i){if(y(a))return null;s&&=w(a)}return s?o(i):L(r.failOnError,`${n} array elements must resolve to integers`)}var os=(e,t,r)=>ne(e,t,r,"$bitAnd",n=>n.reduce((o,i)=>o&i,-1));var is=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:w(n)?~n:F(r.failOnError,"$bitNot")};var ss=(e,t,r)=>ne(e,t,r,"$bitOr",n=>n.reduce((o,i)=>o|i,0));var as=(e,t,r)=>ne(e,t,r,"$bitXor",n=>n.reduce((o,i)=>o^i,0));var Ae={};ht(Ae,{$and:()=>$n,$not:()=>En,$or:()=>In});var $n=(e,t,r)=>{u(l(t),"$and expects array");let n=r.useStrictMode;return t.every(o=>nt(f(e,o,r),n))};var En=(e,t,r)=>{let n=ct(t);return n.length===0?!1:n.length>1?j(r.failOnError,"$not",{size:1}):!f(e,n[0],r)};var In=(e,t,r)=>{u(l(t),"$or expects array of expressions");let n=r.useStrictMode;for(let o of t)if(nt(f(e,o,r),n))return!0;return!1};var de={};ht(de,{$cmp:()=>Tn,$eq:()=>Fn,$gt:()=>Bn,$gte:()=>Ln,$lt:()=>zn,$lte:()=>Wn,$ne:()=>qn});var Tn=(e,t,r)=>{u(l(t)&&t.length===2,"$cmp expects array(2)");let[n,o]=f(e,t,r);return M(n,o)};function vr(e,t,r){return e.take(t)}function wr(e,t,r){let n=f(null,t,r);return u(l(n),"$documents expression must resolve to an array."),S(n).map(o=>it(o))}var cs=S([]);function ze(e,t){if(!e)return{};let r=e[0]?.$documents;return r?{documents:wr(cs,r,t).collect(),pipeline:e.slice(1)}:{pipeline:e}}function Nr(e,t,r=!0){let n={exclusions:[],inclusions:[],positional:0},o=Object.keys(e);u(o.length,"Invalid empty sub-projection");let i=t?.idKey,s=!1;for(let a of o){if(a.startsWith("$"))return u(!r&&o.length===1,`FieldPath field names may not start with '$', given '${a}'.`),n;a.endsWith(".$")&&n.positional++;let c=e[a];if(c===!1||x(c)&&c===0)a===i?s=!0:n.exclusions.push(a);else if(!h(c))n.inclusions.push(a);else{let p=Nr(c,t,!1);if(!p.inclusions.length&&!p.exclusions.length)n.inclusions.includes(a)||n.inclusions.push(a);else{for(let m of p.exclusions)n.exclusions.push(`${a}.${m}`);for(let m of p.inclusions)n.inclusions.push(`${a}.${m}`)}n.positional+=p.positional}u(!(n.exclusions.length&&n.inclusions.length),"Cannot do exclusion and inclusion in projection."),u(n.positional<=1,"Cannot specify more than one positional projection.")}if(s&&n.exclusions.push(i),r){let a=new vt;for(let c of n.exclusions)u(a.add(c),`Path collision at ${c}.`);for(let c of n.inclusions)u(a.add(c),`Path collision at ${c}.`);n.exclusions.sort(),n.inclusions.sort()}return n}function Et(e,t,r){$(t)&&u(r.collectionResolver,`${e} requires 'collectionResolver' option to resolve named collection`);let n=$(t)?r.collectionResolver(t):t;return u(l(n),`${e} could not resolve input collection`),n}var Sr="$project";function Kt(e,t,r){if(le(t))return e;let n=Nr(t,r),o=ps(t,C.init(r),n);return e.map(o)}function ps(e,t,r){let n=t.idKey,{exclusions:o,inclusions:i}=r,s={},a={preserveIndex:"missing"};for(let A of o)s[A]=(b,d)=>{Wt(b,A,{descendArray:!0})};for(let A of i){let b=D(e,A)??e[A];if(A.endsWith(".$")&&b===1){let d=t?.local?.condition??{};u(d,`${Sr}: positional operator '.$' requires array condition.`);let g=A.slice(0,-2);s[g]=fs(g,d,t);continue}if(l(b))s[A]=(d,g)=>{t.update({root:g});let E=b.map(I=>f(g,I,t)??null);yt(d,A,E)};else if(x(b)||b===!0)s[A]=(d,g)=>{t.update({root:g});let E=Tt(g,A,a);wn(d,E)};else if(!h(b))s[A]=(d,g)=>{t.update({root:g});let E=f(g,b,t);yt(d,A,E)};else{let d=Object.keys(b);u(d.length===1&&ut(d[0]),"Not a valid operator");let g=d[0],E=b[g],I=t.context.getOperator("projection",g);!I||g==="$slice"&&!ct(E).every(x)?s[A]=(U,k)=>{t.update({root:k});let J=f(k,b,t);yt(U,A,J)}:s[A]=(U,k)=>{t.update({root:k});let J=I(k,E,A,t);yt(U,A,J)}}}let c=o.length===1&&o.includes(n),p=!o.includes(n),m=!i.length,O=m&&c||m&&o.length&&!c;return A=>{let b={};O&&Object.assign(b,A);for(let d in s)s[d](b,A);return m||Se(b),p&&!T(b,n)&&T(A,n)&&(b[n]=D(A,n)),b}}var vn=(e,t,r,n)=>{let o=D(e,t);l(o)||(o=D(o,r)),u(l(o),`${Sr}: field '${t}' must resolve to array`);let i=[];for(let s=0;s<o.length;s++)n({[r]:[o[s]]})&&i.push(s);return i},us=e=>(t=>!e(t)),ms={$and:1,$or:1,$nor:1};function fs(e,t,r){let n=Object.entries(t).slice(),o={$and:[],$or:[]};for(let c=0;c<n.length;c++){let[p,m,O]=n[c];if(p===e||p.startsWith(e+".")){let A=qt(m),b=Object.keys(A)[0],d=A[b],g=r.context.getOperator("query",b),E=p.substring(p.lastIndexOf(".")+1),I=g(E,d,r);!O||O==="$and"?o.$and.push([p,I,E]):O==="$nor"?o.$and.push([p,us(I),E]):O==="$or"&&o.$or.push([p,I,E])}else if(ut(p)){u(!!ms[p],`${Sr}: '${p}' is not allowed in this context`);for(let A of m)for(let b of Object.keys(A))n.push([b,A[b],p])}}let i=e.lastIndexOf("."),s=e.substring(0,i)||e,a=e.substring(i+1);return(c,p)=>{let m=[];for(let[b,d,g]of o.$and)m.push(vn(p,b,g,d));if(o.$or.length){let b=[];for(let[d,g,E]of o.$or)b.push(...vn(p,d,E,g));m.push($t(b))}let O=Lt(m).sort()[0],A=D(p,e)[O];s!=a&&!h(A)&&(A={[a]:A}),yt(c,s,[A])}}function wn(e,t){if(e===ee||y(e))return t;if(y(t))return e;let r=e,n=t;for(let o of Object.keys(t))r[o]=wn(r[o],n[o]);return r}function Cr(e,t,r){return u(t>=0,"$skip value must be a non-negative integer"),e.drop(t)}var Nn={$sort:st,$skip:Cr,$limit:vr},We=class{#t;#e;#r;#o;#s={};#n=null;#i=[];constructor(t,r,n,o){this.#t=t,this.#e=r,this.#r=n,this.#o=o}fetch(){if(this.#n)return this.#n;this.#n=S(this.#t).filter(this.#e);let t=this.#o.processingMode;t&1&&this.#n.map(r=>it(r));for(let r of Object.keys(Nn))if(T(this.#s,r)){let n=Nn[r];this.#n=n(this.#n,this.#s[r],this.#o)}return Object.keys(this.#r).length&&(this.#n=Kt(this.#n,this.#r,this.#o)),t&2&&this.#n.map(r=>it(r)),this.#n}fetchAll(){let t=S(Array.from(this.#i));return this.#i.length=0,Nt(t,this.fetch())}all(){return this.fetchAll().collect()}skip(t){return this.#s.$skip=t,this}limit(t){return this.#s.$limit=t,this}sort(t){return this.#s.$sort=t,this}collation(t){return this.#o={...this.#o,collation:t},this}next(){if(this.#i.length>0)return this.#i.pop();let t=this.fetch().next();if(!t.done)return t.value}hasNext(){if(this.#i.length>0)return!0;let t=this.fetch().next();return t.done?!1:(this.#i.push(t.value),!0)}[Symbol.iterator](){return this.fetchAll()}};var ls=new Set(["$and","$or","$nor","$expr","$jsonSchema"]),G=class{#t;#e;#r;constructor(t,r){this.#e=it(t),this.#r=C.init(r).update({condition:t}),this.#t=[],this.compile()}compile(){u(h(this.#e),`query criteria must be an object: ${JSON.stringify(this.#e)}`);let t={};for(let r of Object.keys(this.#e)){let n=this.#e[r];if(r==="$where")u(this.#r.scriptEnabled,"$where operator requires 'scriptEnabled' option to be true."),Object.assign(t,{field:r,expr:n});else if(ls.has(r))this.processOperator(r,r,n);else{u(!ut(r),`unknown top level operator: ${r}`);let o=qt(n);for(let i of Object.keys(o))this.processOperator(r,i,o[i])}t.field&&this.processOperator(t.field,t.field,t.expr)}}processOperator(t,r,n){let o=this.#r.context.getOperator("query",r);u(!!o,`unknown query operator ${r}`),this.#t.push(o(t,n,this.#r))}test(t){return this.#t.every(r=>r(t))}find(t,r){return new We(t,n=>this.test(n),r||{},this.#r)}};function kn(e,t){if(Object.keys(e).length===0)return i=>h(i);let r=i=>i,n=!0;for(let i of Object.keys(e))if(n&&=ut(i)&&i!=="$and"&&i!=="$or"&&i!=="$nor",!n)break;n&&(e={field:e},r=i=>({field:i}));let o=new G(e,t);return i=>o.test(r(i))}function B(e,t,r,n){let[o,i]=[-1,0];for(;(o=e.indexOf(".",o+1))!==-1;)i++;let s=C.init(r).update({depth:i}),a={unwrapArray:!0};return n===He&&(t=kn(t,r)),c=>{let p=D(c,e,a);return n(p,t,s)}}function dt(e,t,r,n){u(l(t)&&t.length===2,`${n.name} expects array(2)`);let[o,i]=f(e,t,r);return n(o,i,r)}function be(e,t,r){if(Q(e,t)||y(e)&&y(t))return!0;if(l(e)){let n=r?.local?.depth??1;return e.some(o=>Q(o,t))||pt(e,n).some(o=>Q(o,t))}return!1}function qe(e,t,r){return!be(e,t,r)}function kr(e,t,r){if(y(e))return t.some(i=>i===null);let n=ct(e);if(Lt([n,t]).length>0)return!0;let o=t.filter(ot);return o.length>0&&n.some(i=>$(i)&&o.some(s=>s.test(i)))}function _n(e,t,r){return!kr(e,t,r)}function Ye(e,t,r){return Ge(e,t,(n,o)=>M(n,o)<0)}function Ve(e,t,r){return Ge(e,t,(n,o)=>M(n,o)<=0)}function Ke(e,t,r){return Ge(e,t,(n,o)=>M(n,o)>0)}function Qe(e,t,r){return Ge(e,t,(n,o)=>M(n,o)>=0)}function Mn(e,t,r){return ct(e).some((n=>t.length===2&&n%t[0]===t[1]))}function Dn(e,t,r){let n=ct(e),o=i=>$(i)&&nt(t.exec(i),r?.useStrictMode);return n.some(o)||pt(n,1).some(o)}function Rn(e,t,r){if(!l(e)||!l(t)||!e.length||!t.length)return!1;let n=!0;for(let o of t){if(!n)break;if(h(o)&&Object.keys(o)[0]==="$elemMatch"){let i=o.$elemMatch,s=kn(i,r);n=He(e,s,r)}else ot(o)?n=e.some(i=>$(i)&&o.test(i)):n=e.some(i=>Q(o,i))}return n}function Pn(e,t,r){return Array.isArray(e)&&e.length===t}function He(e,t,r){if(l(e)&&e.length!==0){for(let n=0,o=e.length;n<o;n++)if(t(e[n]))return!0}return!1}var Sn=e=>e===null,ys={array:l,boolean:Bt,bool:Bt,date:R,number:x,int:w,long:w,double:x,decimal:x,null:Sn,object:h,regexp:ot,regex:ot,string:$,undefined:y,1:x,2:$,3:h,4:l,6:y,8:Bt,9:R,10:Sn,11:ot,16:w,18:w,19:x};function Cn(e,t,r){let n=ys[t];return n?n(e):!1}function Un(e,t,r){return l(t)?t.findIndex(n=>Cn(e,n,r))>=0:Cn(e,t,r)}function Ge(e,t,r){for(let n of ct(e))if(at(n)===at(t)&&r(n,t))return!0;return!1}var Fn=(e,t,r)=>dt(e,t,r,be);var Bn=(e,t,r)=>dt(e,t,r,Ke);var Ln=(e,t,r)=>dt(e,t,r,Qe);var zn=(e,t,r)=>dt(e,t,r,Ye);var Wn=(e,t,r)=>dt(e,t,r,Ve);var qn=(e,t,r)=>dt(e,t,r,qe);var Yn="$cond expects array(3) or object with 'if-then-else' expressions",Os=(e,t,r)=>{let n,o,i;l(t)?(u(t.length===3,Yn),n=t[0],o=t[1],i=t[2]):(u(h(t),Yn),n=t.if,o=t.then,i=t.else);let s=nt(f(e,n,r),r.useStrictMode);return f(e,s?o:i,r)};var _r=(e,t,r)=>{u(l(t),"$ifNull expects an array");let n;for(let o of t)if(n=f(e,o,r),!y(n))return n;return n};var As=(e,t,r)=>{u(h(t),"$switch received invalid arguments");for(let{case:n,then:o}of t.branches)if(nt(f(e,n,r),r.useStrictMode))return f(e,o,r);return f(e,t.default,r)};var Mr=(e,t,r)=>{u(r.scriptEnabled,"$function requires 'scriptEnabled' option to be true");let n=f(e,t,r);return n.body.apply(null,n.args)};var ge=["year","quarter","month","week","day","hour","minute","second","millisecond"],bs={mon:1,tue:2,wed:3,thu:4,fri:5,sat:6,sun:7},ds=-1e9,oe=7,Dr=e=>(e&3)==0&&(e%100!=0||e%400==0);var gs=[365,366],xs=[[0,31,59,90,120,151,181,212,243,273,304,334],[0,31,60,91,121,152,182,213,244,274,305,335]],Qt=e=>xs[+Dr(e.getUTCFullYear())][e.getUTCMonth()]+e.getUTCDate(),Xe=(e,t)=>{let r=e.getUTCDay()||7,n=t.toLowerCase().substring(0,3);return(r-bs[n]+oe)%oe},Vn=e=>(e+Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400))%7,Kn=e=>52+ +(Vn(e)==4||Vn(e-1)==3);function Ht(e){let t=e.getUTCDay()||7,r=Math.floor((10+Qt(e)-t)/7);return r<1?Kn(e.getUTCFullYear()-1):r>Kn(e.getUTCFullYear())?1:r}function Ze(e){let t=Ht(e);return e.getUTCDay()>0&&e.getUTCDate()==1&&e.getUTCMonth()==0?0:e.getUTCDay()==0?t+1:t}function tr(e){return e.getUTCFullYear()-+(e.getUTCMonth()===0&&e.getUTCDate()==1&&e.getUTCDay()<1)}var _t=60,xt={week:6048e5,day:864e5,hour:36e5,minute:6e4,second:1e3,millisecond:1},er="%Y-%m-%dT%H:%M:%S.%LZ",Rr=[["year",0,9999],["month",1,12],["day",1,31],["hour",0,23],["minute",0,59],["second",0,59],["millisecond",0,999]],Pr={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12},ie={"%b":{name:"abbr_month",padding:3,re:/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i},"%B":{name:"full_month",padding:0,re:/(January|February|March|April|May|June|July|August|September|October|November|December)/i},"%Y":{name:"year",padding:4,re:/([0-9]{4})/},"%G":{name:"year",padding:4,re:/([0-9]{4})/},"%m":{name:"month",padding:2,re:/(0[1-9]|1[012])/},"%d":{name:"day",padding:2,re:/(0[1-9]|[12][0-9]|3[01])/},"%j":{name:"day_of_year",padding:3,re:/(0[0-9][1-9]|[12][0-9]{2}|3[0-5][0-9]|36[0-6])/},"%H":{name:"hour",padding:2,re:/([01][0-9]|2[0-3])/},"%M":{name:"minute",padding:2,re:/([0-5][0-9])/},"%S":{name:"second",padding:2,re:/([0-5][0-9]|60)/},"%L":{name:"millisecond",padding:3,re:/([0-9]{3})/},"%w":{name:"day_of_week",padding:1,re:/([0-6])/},"%u":{name:"day_of_week_iso",padding:1,re:/([1-7])/},"%U":{name:"week_of_year",padding:2,re:/([1-4][0-9]?|5[0-3]?)/},"%V":{name:"week_of_year_iso",padding:2,re:/([1-4][0-9]?|5[0-3]?)/},"%z":{name:"timezone",padding:2,re:/(([+-][01][0-9]|2[0-3]):?([0-5][0-9])?)/},"%Z":{name:"minute_offset",padding:3,re:/([+-][0-9]{3})/},"%%":{name:"percent_literal",padding:1,re:/%%/}},rr=/(%[bBYGmdjHMSLwuUVzZ%])/g,Gn=/%[bBYGmdjHMSLwuUVzZ%]/,hs=/^[a-zA-Z_]+\/[a-zA-Z_]+$/;function lt(e,t){if(e===void 0)return 0;if(hs.test(e)){let i=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),s=new Date(t.toLocaleString("en-US",{timeZone:e}));return Math.round((s.getTime()-i.getTime())/6e4)}let r=ie["%z"].re.exec(e);u(!!r,`timezone '${e}' is invalid or not supported.`);let n=parseInt(r[2])||0,o=parseInt(r[3])||0;return(Math.abs(n*_t)+o)*(n<0?-1:1)}function Xn(e){return(e<0?"-":"+")+Je(Math.abs(Math.floor(e/_t)),2)+Je(Math.abs(e)%_t,2)}function gt(e,t){e.setUTCMinutes(e.getUTCMinutes()+t)}function z(e,t,r){let n=f(e,t,r);if(R(n))return new Date(n);u(!!n?.date,`cannot convert ${JSON.stringify(t)} to date`);let o=new Date(n.date);return n.timezone&&gt(o,lt(n.timezone,o)),o}function Je(e,t){return new Array(Math.max(t-String(e).length+1,0)).join("0")+e.toString()}var Qn=e=>{let t=e-ds;return Math.trunc(t/4)-Math.trunc(t/100)+Math.trunc(t/400)};function js(e,t){return Math.trunc(Qn(t-1)-Qn(e-1)+(t-e)*gs[0])}var se=(e,t)=>t.getUTCFullYear()-e.getUTCFullYear(),nr=(e,t)=>t.getUTCMonth()-e.getUTCMonth()+se(e,t)*12,or=(e,t)=>{let r=Math.trunc(e.getUTCMonth()/3);return Math.trunc(t.getUTCMonth()/3)-r+se(e,t)*4},ae=(e,t)=>Qt(t)-Qt(e)+js(e.getUTCFullYear(),t.getUTCFullYear()),ir=(e,t,r)=>{let n=(r||"sun").substring(0,3);return Math.trunc((ae(e,t)+Xe(e,n)-Xe(t,n))/oe)},Jn=(e,t)=>t.getUTCHours()-e.getUTCHours()+ae(e,t)*24,Hn=(e,t)=>{let r=e.getUTCMonth()+t,n=Math.floor(r/12);if(r<0){let o=r%12+12;e.setUTCFullYear(e.getUTCFullYear()+n,o,e.getUTCDate())}else e.setUTCFullYear(e.getUTCFullYear()+n,r%12,e.getUTCDate())},ce=(e,t,r,n)=>{let o=new Date(e);switch(t){case"year":o.setUTCFullYear(o.getUTCFullYear()+r);break;case"quarter":Hn(o,3*r);break;case"month":Hn(o,r);break;default:o.setTime(o.getTime()+xt[t]*r)}return o};var xe=(e,t,r)=>{let n=f(e,t,r);return ce(n.startDate,n.unit,n.amount,n.timezone)};var $s=(e,t,r)=>{let{startDate:n,endDate:o,unit:i,timezone:s,startOfWeek:a}=f(e,t,r),c=new Date(n),p=new Date(o);switch(gt(c,lt(s,c)),gt(p,lt(s,p)),i){case"year":return se(c,p);case"quarter":return or(c,p);case"month":return nr(c,p);case"week":return ir(c,p,a);case"day":return ae(c,p);case"hour":return Jn(c,p);case"minute":return c.setUTCSeconds(0),c.setUTCMilliseconds(0),p.setUTCSeconds(0),p.setUTCMilliseconds(0),Math.round((p.getTime()-c.getTime())/xt[i]);default:return Math.round((p.getTime()-c.getTime())/xt[i])}};var Es=[31,28,31,30,31,30,31,31,30,31,30,31],Is=e=>e.month==2&&Dr(e.year)?29:Es[e.month-1],Ts=(e,t,r)=>{let n=f(e,t,r),o=lt(n.timezone,new Date);for(let i=Rr.length-1,s=0;i>=0;i--){let a=Rr[i],c=a[0],p=a[1],m=a[2],O=(n[c]||0)+s;s=0;let A=m+1;if(c=="hour"&&(O+=Math.floor(o/_t)*-1),c=="minute"&&(O+=o%_t*-1),O<p){let b=p-O;s=-1*Math.ceil(b/A),O=A-b%A}else O>m&&(O+=p,s=Math.trunc(O/A),O%=A);n[c]=O}return n.day=Math.min(n.day,Is(n)),new Date(Date.UTC(n.year,n.month-1,n.day,n.hour,n.minute,n.second,n.millisecond))};function vs(e){return e==="Z"?0:e>="A"&&e<"N"?e.charCodeAt(0)-64:77-e.charCodeAt(0)}var ws=e=>e.replace(/^\//,"").replace(/\/$/,"").replace(/\/i/,""),Ns=["^",".","-","*","?","$"];function Ss(e){for(let t of Ns)e=e.replace(t,`\\${t}`);return e}function Cs(e,t,r){let n=f(e,t,r),o=n.format||er,i=n.onNull||null,s=n.dateString;if(y(s))return i;let a=o.split(Gn);a.reverse();let c=o.match(rr),p={},m="";for(let d=0,g=c.length;d<g;d++){let E=c[d],I=ie[E];if(h(I)){let P=I.re.exec(s),U=a.pop()||"";P!==null?(p[I.name]=/^\d+$/.exec(P[0])?parseInt(P[0]):P[0],s=s.substring(P.index+P[0].length+1),m+=Ss(U)+ws(I.re.toString())):p[I.name]=null}}if(y(p.month)){let d=(p.full_month?.slice(0,3)??p.abbr_month??"").toLowerCase();Pr[d]&&(p.month=Pr[d])}if(y(p.year)||y(p.month)||y(p.day)||!new RegExp("^"+m+"[A-Z]?$").test(n.dateString))return n.onError;let O=n.dateString.match(/([A-Z])$/);u(!(O&&n.timezone),`$dateFromString: you cannot pass in a date/time string with time zone information ('${O&&O[0]}') together with a timezone argument`);let A=O?vs(O[0])*_t:lt(n.timezone,new Date),b=new Date(Date.UTC(p.year,p.month-1,p.day,0,0,0));return y(p.hour)||b.setUTCHours(p.hour),y(p.minute)||b.setUTCMinutes(p.minute),y(p.second)||b.setUTCSeconds(p.second),y(p.millisecond)||b.setUTCMilliseconds(p.millisecond),gt(b,-A),b}var ks=(e,t,r)=>{let n=f(e,t,r);return ce(n.startDate,n.unit,-n.amount,n.timezone)};var _s=(e,t,r)=>{let n=f(e,t,r),o=new Date(n.date);gt(o,lt(n.timezone,o));let i={hour:o.getUTCHours(),minute:o.getUTCMinutes(),second:o.getUTCSeconds(),millisecond:o.getUTCMilliseconds()};return n.iso8601==!0?Object.assign(i,{isoWeekYear:tr(o),isoWeek:Ht(o),isoDayOfWeek:o.getUTCDay()||7}):Object.assign(i,{year:o.getUTCFullYear(),month:o.getUTCMonth()+1,day:o.getUTCDate()})};var Ms={"%Y":e=>e.getUTCFullYear(),"%G":e=>e.getUTCFullYear(),"%m":e=>e.getUTCMonth()+1,"%d":e=>e.getUTCDate(),"%H":e=>e.getUTCHours(),"%M":e=>e.getUTCMinutes(),"%S":e=>e.getUTCSeconds(),"%L":e=>e.getUTCMilliseconds(),"%u":e=>e.getUTCDay()||7,"%U":Ze,"%V":Ht,"%j":Qt,"%w":e=>e.getUTCDay()},Ds=(e,t,r)=>{let n=f(e,t,r);if(y(n.onNull)&&(n.onNull=null),y(n.date))return n.onNull;let o=z(e,n.date,r),i=n.format??er,s=lt(n.timezone,o),a=i.match(rr);if(!a)return i;gt(o,s);for(let c=0,p=a.length;c<p;c++){let m=a[c];u(m in ie,`$dateToString: invalid format specifier ${m}`);let{name:O,padding:A}=ie[m],b=Ms[m],d="";if(b)d=Je(b(o),A);else switch(O){case"timezone":d=Xn(s);break;case"minute_offset":d=s.toString();break;case"abbr_month":case"full_month":{let g=O.startsWith("abbr")?"short":"long";d=o.toLocaleString("en-US",{month:g});break}}i=i.replace(m,d)}return i};var Zn=9466848e5,to=(e,t)=>{let r=e%t;return r<0&&(r+=t),r},Rs={day:ae,month:nr,quarter:or,year:se},Ps=/(mon(day)?|tue(sday)?|wed(nesday)?|thu(rsday)?|fri(day)?|sat(urday)?|sun(day)?)/i,Us=(e,t,r)=>{let{date:n,unit:o,binSize:i,timezone:s,startOfWeek:a}=f(e,t,r);if(y(n)||y(o))return null;let c=(a??"sun").toLowerCase().substring(0,3);u(R(n),"$dateTrunc: 'date' must resolve to a valid Date object."),u(ge.includes(o),"$dateTrunc: unit is invalid."),u(o!="week"||Ps.test(c),`$dateTrunc: startOfWeek '${c}' is not a valid.`),u(y(i)||i>0,"$dateTrunc requires 'binSize' to be greater than 0, but got value 0.");let p=i??1;switch(o){case"millisecond":case"second":case"minute":case"hour":{let m=p*xt[o],O=n.getTime()-Zn;return new Date(n.getTime()-to(O,m))}default:{u(p<=1e11,"dateTrunc unsupported binSize value");let m=new Date(n),O=new Date(Zn),A=0;if(o=="week"){let E=Xe(O,c),I=(oe-E)%oe;O.setTime(O.getTime()+I*xt.day),A=ir(O,m,c)}else A=Rs[o](O,m);let b=A-to(A,p),d=ce(O,o,b,s),g=lt(s,d);return gt(d,-g),d}}};var Fs=(e,t,r)=>z(e,t,r).getUTCDate();var Bs=(e,t,r)=>z(e,t,r).getUTCDay()+1;var Ls=(e,t,r)=>Qt(z(e,t,r));var zs=(e,t,r)=>z(e,t,r).getUTCHours();var Ws=(e,t,r)=>z(e,t,r).getUTCDay()||7;var qs=(e,t,r)=>Ht(z(e,t,r));var Ys=(e,t,r)=>tr(z(e,t,r));var Vs=(e,t,r)=>z(e,t,r).getUTCMilliseconds();var Ks=(e,t,r)=>z(e,t,r).getUTCMinutes();var Qs=(e,t,r)=>z(e,t,r).getUTCMonth()+1;var Hs=(e,t,r)=>z(e,t,r).getUTCSeconds();var Gs=(e,t,r)=>Ze(z(e,t,r));var Xs=(e,t,r)=>z(e,t,r).getUTCFullYear();var Js=(e,t,r)=>t;var Zs=(e,t,r)=>{let n=f(e,t.input,r);return hr(n,{input:"$$CURRENT",method:t.method},r)};var ta=(e,t,r)=>{let n=f(e,t,r),{field:o,input:i}=$(n)?{field:n,input:e}:{field:n.field,input:n.input??e};return i[o]};var ea=(e,t,r)=>Math.random();var ra=(e,t,r)=>Math.random()<=f(e,t,r);var Ur=(e,t,r)=>{let n=f(e,t,r);return y(n)?{}:l(n)?jr(n,t,r):j(r.failOnError,"$mergeObjects",xn.obj)};var na=(e,t,r)=>{let n=f(e,t,r);if(y(n))return null;if(!h(n))return Me(r.failOnError,"$objectToArray");let o=Object.keys(n),i=new Array(o.length),s=0;for(let a of o)i[s++]={k:a,v:n[a]};return i};var eo="$setField",Fr=(e,t,r)=>{u(h(t)&&T(t,"input","field","value"),"$setField expects object { input, field, value }");let{input:n,field:o,value:i}=f(e,t,r);if(y(n))return null;let s=r.failOnError;if(!h(n))return Me(s,`${eo} 'input'`);if(!$(o))return Z(s,`${eo} 'field'`);let a={...n};return t.value=="$$REMOVE"?delete a[o]:a[o]=i,a};var oa=(e,t,r)=>Fr(e,{...t,value:"$$REMOVE"},r);var ia=(e,t,r)=>{let n=f(e,t.input,r);return Oe(n,{...t,input:"$$CURRENT"},r)};var sa=(e,t,r)=>{if(l(t)){if(t.length===0)return!0;u(t.length===1,"$allElementsTrue expects array(1)"),t=t[0]}let n=r.failOnError,o=f(e,t,r);if(!l(o))return j(n,"$allElementsTrue argument");for(let i of o)if(!nt(i,r.useStrictMode))return!1;return!0};var aa=(e,t,r)=>{if(l(t)){if(t.length===0)return!1;u(t.length===1,"$anyElementTrue expects array(1)"),t=t[0]}let n=r.failOnError,o=f(e,t,r);if(!l(o))return j(n,"$anyElementTrue argument");for(let i of o)if(nt(i,r.useStrictMode))return!0;return!1};var ro="$setDifference",ca=(e,t,r)=>{u(l(t)&&t.length==2,`${ro} expects array(2)`);let n=f(e,t,r),o=r.failOnError,i=!0;for(let a of n){if(y(a))return null;i&&=l(a)}if(!i)return j(o,`${ro} arguments`);let s=et.init();for(let a of n[0])s.set(a,!0);for(let a of n[1])s.delete(a);return Array.from(s.keys())};var pa=(e,t,r)=>{u(l(t),"$setEquals expects array");let n=f(e,t,r),o=r.failOnError;if(!n.every(l))return j(o,"$setEquals arguments");let i=et.init(),s=n[0];for(let a=0;a<s.length;a++)i.set(s[a],a);for(let a=1;a<n.length;a++){let c=n[a],p=new Set;for(let m=0;m<c.length;m++){let O=i.get(c[m])??-1;if(O===-1)return!1;p.add(O)}if(p.size!==i.size)return!1}return!0};var no="$setIntersection",ua=(e,t,r)=>{u(l(t),`${no} expects array`);let n=f(e,t,r),o=r.failOnError,i=!0;for(let s of n){if(y(s))return null;i&&=l(s)}return i?Lt(n):j(o,`${no} arguments`)};var oo="$setIsSubset",ma=(e,t,r)=>{u(l(t)&&t.length===2,`${oo} expects array(2)`);let n=f(e,t,r);if(!n.every(l))return j(r.failOnError,`${oo} arguments`);let[o,i]=n,s=et.init();for(let a of i)s.set(a,0);for(let a of o)if(!s.has(a))return!1;return!0};var fa=(e,t,r)=>{let n=f(e,t,r),o=r.failOnError;return y(n)?null:l(n)?l(t)?n.every(l)?$t(pt(n)):j(o,"$setUnion arguments"):$t(n):j(o,"$setUnion")};var la=(e,t,r)=>{u(l(t),"$concat expects array");let n=r.failOnError,o=f(e,t,r),i=!0;for(let s of o){if(y(s))return null;i&&=$(s)}return i?o.join(""):j(n,"$concat",{type:"string"})};var he="$indexOfBytes",ya=(e,t,r)=>{u(l(t)&&t.length>1&&t.length<5,`${he} expects array(4)`);let n=f(e,t,r),o=r.failOnError,i=n[0];if(y(i))return null;if(!$(i))return Z(o,`${he} arg1 <string>`);let s=n[1];if(!$(s))return Z(o,`${he} arg2 <search>`);let a=n[2]??0,c=n[3]??i.length;if(!w(a)||a<0)return F(o,`${he} arg3 <start>`,{min:0});if(!w(c)||c<0)return F(o,`${he} arg4 <end>`,{min:0});if(a>c)return-1;let p=i.substring(a,c).indexOf(s);return p>-1?p+a:p};var Oa=[0,32,9,10,11,12,13,160,5760,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202];function pe(e,t,r,n){let o=f(e,t,r),i=o.input;if(y(i))return null;let s=y(o.chars)?Oa:o.chars.split("").map(p=>p.codePointAt(0)),a=0,c=i.length-1;for(;n.left&&a<=c&&s.indexOf(i[a].codePointAt(0))!==-1;)a++;for(;n.right&&a<=c&&s.indexOf(i[c].codePointAt(0))!==-1;)c--;return i.substring(a,c+1)}function ue(e,t,r,n){let o=f(e,t,r);if(!$(o.input))return[];let i=o.options;i&&(u(i.indexOf("x")===-1,"extended capability option 'x' not supported"),u(i.indexOf("g")===-1,"global option 'g' not supported"));let s=o.input,a=new RegExp(o.regex,i),c,p=new Array,m=0;for(;c=a.exec(s);){let O={match:c[0],idx:c.index+m,captures:[]};for(let A=1;A<c.length;A++)O.captures.push(c[A]||null);if(p.push(O),!n.global)break;m=c.index+c[0].length,s=s.substring(m)}return p}var Aa=(e,t,r)=>pe(e,t,r,{left:!0,right:!1});var ba=(e,t,r)=>{let n=ue(e,t,r,{global:!1});return l(n)&&n.length>0?n[0]:null};var da=(e,t,r)=>ue(e,t,r,{global:!0});var ga=(e,t,r)=>ue(e,t,r,{global:!1})?.length!=0;var sr="$replaceAll",xa=(e,t,r)=>{u(h(t),`${sr} expects an object argument`);let n=r.failOnError,o=f(e,t,r),{input:i,find:s,replacement:a}=o;return y(i)||y(s)||y(a)?null:$(i)?$(s)?$(a)?i.split(s).join(a):Z(n,`${sr} 'replacement'`):Z(n,`${sr} 'find'`):Z(n,`${sr} 'input'`)};var Br="$replaceOne",ha=(e,t,r)=>{let n=r.failOnError,o=f(e,t,r),{input:i,find:s,replacement:a}=o;if(y(i)||y(s)||y(a))return null;if(!$(i))return Z(n,`${Br} 'input'`);if(!$(s))return Z(n,`${Br} 'find'`);if(!$(a))return Z(n,`${Br} 'replacement'`);let c=i.indexOf(s);return c<0?i:i.slice(0,c)+a+i.slice(c+s.length)};var ja=(e,t,r)=>pe(e,t,r,{left:!1,right:!0});var $a=(e,t,r)=>{u(l(t)&&t.length===2,"$split expects array(2)");let n=f(e,t,r),o=r.failOnError;return y(n[0])?null:n.every($)?n[0].split(n[1]):j(o,"$split ",{size:2,type:"string"})};var Ea=(e,t,r)=>{u(l(t)&&t.length===2,"$strcasecmp expects array(2)");let n=f(e,t,r),o=r.failOnError,i=!0,s=!0;for(let a of n)i&&=y(a),s&&=$(a);return i?0:s?At(n[0].toLowerCase(),n[1].toLowerCase()):j(o,"$strcasecmp arguments",{type:"string"})};var Ia=(e,t,r)=>{let n=f(e,t,r);return $(n)?~-encodeURI(n).split(/%..|./).length:Z(r.failOnError,"$strLenBytes")};var Ta=(e,t,r)=>{let n=f(e,t,r);return $(n)?[...n].length:Z(r.failOnError,"$strLenCP")};var Mt="$substrBytes",Lr=(e,t,r)=>{u(l(t)&&t.length===3,`${Mt} expects array(3)`);let[n,o,i]=f(e,t,r),s=r.failOnError,a=y(n);if(!a&&!$(n))return Z(s,`${Mt} arg1 <string>`);if(!w(o)||o<0)return F(s,`${Mt} arg2 <index>`,{min:0});if(!w(i)||i<0)return F(s,`${Mt} arg3 <count>`,{min:0});if(a)return"";let c=0,p=null,m=null,O=`${Mt} UTF-8 boundary falls inside a continuation byte`;for(let A=0;A<n.length;){let b=n.codePointAt(A);if(b===void 0)return L(s,`${Mt} byte index out of range`);let d=b<128?1:b<2048?2:b<65536?3:4,g=b>65535?2:1;if(p===null){if(o>c&&o<c+d)return L(s,O);c===o&&(p=A)}let E=o+i;if(p!==null&&m===null){if(E>c&&E<c+d)return L(s,O);if(c===E){m=A;break}}c+=d,A+=g}if(p===null)return o===c?"":L(s,`${Mt} byte index out of range`);if(m===null){if(o+i!==c)return L(s,`${Mt} count extends beyond UTF-8 length`);m=n.length}return n.slice(p,m)};var va=Lr;var ar="$substrCP",wa=(e,t,r)=>{u(l(t)&&t.length===3,`${ar} expects array(3)`);let[n,o,i]=f(e,t,r),s=y(n),a=r.failOnError;if(!s&&!$(n))return Z(a,`${ar} arg1 <string>`);if(!w(o))return H(a,`${ar} arg2 <index>`);if(!w(i))return H(a,`${ar} arg3 <count>`);if(s||o<0)return"";let c=[...n];return i<0?c.slice(o).join(""):c.slice(o,o+i).join("")};var zr=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:$(n)?!0:!!n};var Wr=(e,t,r)=>{let n=f(e,t,r);if(R(n))return n;if(y(n))return null;let o=new Date(n);return u(!isNaN(o.getTime()),`cannot convert '${n}' to date`),o};var je=(e,t,r)=>{let n=f(e,t,r);if(y(n))return null;if(R(n))return n.getTime();if(n===!0)return 1;if(n===!1)return 0;let o=Number(n);return u(x(o),`cannot convert '${n}' to double/decimal`),o};var $e=2147483647,cr=-2147483648,io=9007199254740991,so=-9007199254740991;function pr(e,t,r,n,o){let i=f(e,t,r);if(i===!0)return 1;if(i===!1)return 0;if(y(i))return null;if(R(i))return i.getTime();let s=Number(i);return u(x(s)&&s>=n&&s<=o&&(!$(i)||s.toString().indexOf(".")===-1),`cannot convert '${i}' to ${o==$e?"int":"long"}`),Math.trunc(s)}var qr=(e,t,r)=>pr(e,t,r,cr,$e);var Yr=(e,t,r)=>pr(e,t,r,so,io);var Gt=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:R(n)?n.toISOString():It(n)||ot(n)?String(n):L(r.failOnError,"$toString cannot convert from object to string")};var Na=(e,t,r)=>{u(h(t)&&T(t,"input","to"),"$convert expects object { input, to, onError, onNull }");let n=f(e,t.input,r);if(y(n))return f(e,t.onNull,r)??null;let o=f(e,t.to,r);try{switch(o){case 2:case"string":return Gt(e,n,r);case 8:case"boolean":case"bool":return zr(e,n,r);case 9:case"date":return Wr(e,n,r);case 1:case 19:case"double":case"decimal":case"number":return je(e,n,r);case 16:case"int":return qr(e,n,r);case 18:case"long":return Yr(e,n,r)}}catch{}return t.onError===void 0?L(r.failOnError,`$convert cannot convert from object to ${t.to} with no onError value`):f(e,t.onError,r)};var Sa=(e,t,r)=>{let n=f(e,t,r);return x(n)};var Ca=je;var ka=(e,t,r)=>{let n=f(e,t,r);if(r.useStrictMode){if(n===void 0)return"missing";if(n===!0||n===!1)return"bool";if(x(n))return n%1!=0?"double":n>=cr&&n<=$e?"int":"long";if(ot(n))return"regex"}return at(n)};var _a=(e,t,r)=>{l(t)&&t.length===1&&(t=t[0]);let n=Gt(e,t,r);return n===null?n:n.toLowerCase()};var Ma=(e,t,r)=>{l(t)&&t.length===1&&(t=t[0]);let n=Gt(e,t,r);return n===null?n:n.toUpperCase()};var Da=(e,t,r)=>pe(e,t,r,{left:!0,right:!0});var Ra=(e,t,r)=>ft(f(e,t,r));function W(e,t,r,n,o){let i={undefined:null,null:null,NaN:NaN,Infinity:new Error,"-Infinity":new Error,...o},s=r.failOnError,a=n.name,c=f(e,t,r);if(c in i){let p=i[c];return p instanceof Error?H(s,`$${a} invalid input '${c}'`):p}return x(c)?n(c):H(s,`$${a}`)}var Pa=(e,t,r)=>W(e,t,r,Math.acos,{Infinity:1/0,0:new Error});var Ua=(e,t,r)=>W(e,t,r,Math.acosh,{Infinity:1/0,0:new Error});var Fa=(e,t,r)=>W(e,t,r,Math.asin);var Ba=(e,t,r)=>W(e,t,r,Math.asinh,{Infinity:1/0,"-Infinity":-1/0});var La=(e,t,r)=>W(e,t,r,Math.atan);var za=(e,t,r)=>{let[n,o]=f(e,t,r);return isNaN(n)||y(n)?n:isNaN(o)||y(o)?o:Math.atan2(n,o)};var Wa=(e,t,r)=>W(e,t,r,Math.atanh,{1:1/0,"-1":-1/0});var qa=(e,t,r)=>W(e,t,r,Math.cos);var Ya=(e,t,r)=>W(e,t,r,Math.cosh,{"-Infinity":1/0,Infinity:1/0});var Va=e=>e*(Math.PI/180),Ka=(e,t,r)=>W(e,t,r,Va,{Infinity:1/0,"-Infinity":1/0});var Qa=e=>e*(180/Math.PI),Ha=(e,t,r)=>W(e,t,r,Qa,{Infinity:1/0,"-Infinity":1/0});var Ga=(e,t,r)=>W(e,t,r,Math.sin);var Xa=(e,t,r)=>W(e,t,r,Math.sinh,{"-Infinity":-1/0,Infinity:1/0});var Ja=(e,t,r)=>W(e,t,r,Math.tan);var Za=(e,t,r)=>W(e,t,r,Math.tanh,{Infinity:1,"-Infinity":-1});var tc=(e,t,r)=>{let n={};for(let o of Object.keys(t.vars))n[o]=f(e,t.vars[o],r);return f(e,t.in,C.init(r).update({variables:n}))};var rn={};ht(rn,{$addFields:()=>St,$bucket:()=>ec,$bucketAuto:()=>rc,$count:()=>ac,$densify:()=>cc,$documents:()=>wr,$facet:()=>pc,$fill:()=>yc,$graphLookup:()=>Oc,$group:()=>Ie,$limit:()=>vr,$lookup:()=>Gr,$match:()=>Ac,$merge:()=>bc,$out:()=>dc,$project:()=>Kt,$redact:()=>gc,$replaceRoot:()=>Jr,$replaceWith:()=>Zr,$sample:()=>xc,$set:()=>tn,$setWindowFields:()=>Hr,$skip:()=>Cr,$sort:()=>st,$sortByCount:()=>hc,$unionWith:()=>jc,$unset:()=>en,$unwind:()=>$c});function St(e,t,r){let n=Object.keys(t);return n.length===0?e:e.map(o=>{let i={...o};for(let s of n){let a=f(o,t[s],r);a!==void 0?yt(i,s,a):Wt(i,s)}return i})}function ec(e,t,r){let n=t.boundaries.slice(),o=t.default,i=n[0],s=n[n.length-1],a=t.output||{count:{$sum:1}};u(n.length>1,"$bucket: must specify at least two boundaries.");let c=n.every((O,A)=>A===0||at(O)===at(n[A-1])&&M(O,n[A-1])>0);u(c,"$bucket: bounds must be of same type and in ascending order"),u(y(o)||at(o)!==at(i)||M(o,s)>=0||M(o,i)<0,"$bucket: 'default' expression must be out of boundaries range");let p=()=>{let O=new Map;for(let A=0;A<n.length-1;A++)O.set(n[A],[]);return y(o)||O.set(o,[]),e.each(A=>{let b=f(A,t.groupBy,r);if(y(b)||M(b,i)<0||M(b,s)>=0)u(!y(o),"$bucket require a default for out of range values"),O.get(o)?.push(A);else{u(M(b,i)>=0&&M(b,s)<0,"$bucket 'groupBy' expression must resolve to a value in range of boundaries");let d=Yt(n,b),g=n[Math.max(0,d-1)];O.get(g)?.push(A)}}),n.pop(),y(o)||(O.get(o)?.length?n.push(o):O.delete(o)),u(O.size===n.length,"bounds and groups must be of equal size."),S(n).map(A=>({...f(O.get(A),a,r),_id:A}))},m;return S(()=>(m||(m=p()),m.next()))}function rc(e,t,r){let{buckets:n,groupBy:o,output:i,granularity:s}=t,a=i??{count:{$sum:1}};u(n>0,`$bucketAuto: 'buckets' field must be greater than 0, but found: ${n}`),s&&u(/^(POWERSOF2|1-2-5|E(6|12|24|48|96|192)|R(5|10|20|40|80))$/.test(s),`$bucketAuto: invalid granularity '${s}'.`);let c=new Map,p=s?(b,d)=>{}:(b,d)=>c.set(b,d),m=e.map(b=>{let d=f(b,o,r)??null;return u(!s||x(d),"$bucketAuto: groupBy values must be numeric when granularity is specified."),p(b,d??null),[d??null,b]}).collect();m.sort((b,d)=>y(b[0])?-1:y(d[0])?1:M(b[0],d[0]));let O;s?s=="POWERSOF2"?O=oc(m,n):O=sc(m,n,s):O=nc(m,n,c);let A=!1;return S(()=>{if(A)return{done:!0};let{min:b,max:d,bucket:g,done:E}=O();A=E;let I=f(g,a,r);for(let P of Object.keys(I)){let U=I[P];l(U)&&(I[P]=U.filter(k=>!y(k)))}return{done:!1,value:{...I,_id:{min:b,max:d}}}})}function nc(e,t,r){let n=e.length,o=Math.max(1,Math.round(e.length/t)),i=0,s=0;return()=>{let a=++s==t,c=new Array;for(;i<n&&(a||c.length<o||i>0&&Q(e[i-1][0],e[i][0]));)c.push(e[i++][1]);let p=r.get(c[0]),m;return i<n?m=e[i][0]:m=r.get(c[c.length-1]),u(y(m)||y(p)||M(p,m)<=0,"error: $bucketAuto boundary must be in order."),{min:p,max:m,bucket:c,done:i>=n}}}function oc(e,t){let r=e.length,n=Math.max(1,Math.round(e.length/t)),o=c=>c===0?0:2**(Math.floor(Math.log2(c))+1),i=0,s=0,a=0;return()=>{let c=new Array,p=o(a);for(s=i>0?a:0;c.length<n&&i<r&&(a===0||e[i][0]<p);)c.push(e[i++][1]);for(a=a==0?o(e[i-1][0]):p;i<r&&e[i][0]<a;)c.push(e[i++][1]);return{min:s,max:a,bucket:c,done:i>=r}}}var ic={R5:[10,16,25,40,63],R10:[100,125,160,200,250,315,400,500,630,800],R20:[100,112,125,140,160,180,200,224,250,280,315,355,400,450,500,560,630,710,800,900],R40:[100,106,112,118,125,132,140,150,160,170,180,190,200,212,224,236,250,265,280,300,315,355,375,400,425,450,475,500,530,560,600,630,670,710,750,800,850,900,950],R80:[103,109,115,122,128,136,145,155,165,175,185,195,206,218,230,243,258,272,290,307,325,345,365,387,412,437,462,487,515,545,575,615,650,690,730,775,825,875,925,975],"1-2-5":[10,20,50],E6:[10,15,22,33,47,68],E12:[10,12,15,18,22,27,33,39,47,56,68,82],E24:[10,11,12,13,15,16,18,20,22,24,27,30,33,36,39,43,47,51,56,62,68,75,82,91],E48:[100,105,110,115,121,127,133,140,147,154,162,169,178,187,196,205,215,226,237,249,261,274,287,301,316,332,348,365,383,402,422,442,464,487,511,536,562,590,619,649,681,715,750,787,825,866,909,953],E96:[100,102,105,107,110,113,115,118,121,124,127,130,133,137,140,143,147,150,154,158,162,165,169,174,178,182,187,191,196,200,205,210,215,221,226,232,237,243,249,255,261,267,274,280,287,294,301,309,316,324,332,340,348,357,365,374,383,392,402,412,422,432,442,453,464,475,487,499,511,523,536,549,562,576,590,604,619,634,649,665,681,698,715,732,750,768,787,806,825,845,866,887,909,931,953,976],E192:[100,101,102,104,105,106,107,109,110,111,113,114,115,117,118,120,121,123,124,126,127,129,130,132,133,135,137,138,140,142,143,145,147,149,150,152,154,156,158,160,162,164,165,167,169,172,174,176,178,180,182,184,187,189,191,193,196,198,200,203,205,208,210,213,215,218,221,223,226,229,232,234,237,240,243,246,249,252,255,258,261,264,267,271,274,277,280,284,287,291,294,298,301,305,309,312,316,320,324,328,332,336,340,344,348,352,357,361,365,370,374,379,383,388,392,397,402,407,412,417,422,427,432,437,442,448,453,459,464,470,475,481,487,493,499,505,511,517,523,530,536,542,549,556,562,569,576,583,590,597,604,612,619,626,634,642,649,657,665,673,681,690,698,706,715,723,732,741,750,759,768,777,787,796,806,816,825,835,845,856,866,876,887,898,909,920,931,942,953,965,976,988]},ao=(e,t)=>{if(e==0)return 0;let r=ic[t],n=r[0],o=r[r.length-1],i=1;for(;e>=o*i;)i*=10;let s=0;for(;e<n*i;)if(s=n*i,i/=10,e>=o*i)return s;u(e>=n*i&&e<o*i,"$bucketAuto: number out of range of series.");let a=Yt(r,e,(p,m)=>(m*=i,p<m?-1:p>m?1:0)),c=r[a]*i;return e==c?r[a+1]*i:c};function sc(e,t,r){let n=e.length,o=Math.max(1,Math.round(e.length/t)),i=0,s=0,a=0,c=0;return()=>{let p=++s==t,m=new Array;for(a=i>0?c:0;i<n&&(p||m.length<o);)m.push(e[i++][1]);c=ao(e[i-1][0],r);let O=m.length;for(;i<n&&(p||e[i][0]<c);)m.push(e[i++][1]);return O!=m.length&&(c=ao(e[i-1][0],r)),u(a<c,`$bucketAuto: ${a} < ${c}.`),{min:a,max:c,bucket:m,done:i>=n}}}function ac(e,t,r){u($(t)&&t.trim().length>0&&!t.includes(".")&&t[0]!=="$","$count expression must evaluate to valid field name");let n=0;return S(()=>n++==0?{value:{[t]:e.size()},done:!1}:{done:!0})}var Dt="$densify";function cc(e,t,r){let{step:n,bounds:o,unit:i}=t.range;i?(u(ge.includes(i),`${Dt} 'range.unit' value is not supported.`),u(w(n)&&n>0,`${Dt} 'range.step' must resolve to integer if 'range.unit' is specified.`)):u(x(n),`${Dt} 'range.step' must resolve to number.`),l(o)&&(u(!!o&&o.length===2,`${Dt} 'range.bounds' must have exactly two elements.`),u((o.every(x)||o.every(R))&&o[0]<o[1],`${Dt} 'range.bounds' must be ordered lower then upper.`),i&&u(o.every(R),`${Dt} 'range.bounds' must be dates if 'range.unit' is specified.`)),t.partitionByFields&&u(l(t.partitionByFields),`${Dt} 'partitionByFields' must resolve to array of strings.`);let s=t.partitionByFields??[];e=st(e,{[t.field]:1},r);let a=v=>x(v)?v+n:xe({},{startDate:v,unit:i,amount:n},r),c=!!i&&ge.includes(i),p=v=>{let _=D(v,t.field);return u(y(_)||R(_)&&c||x(_)&&!c,`${Dt} Densify field type must be numeric with 'unit' unspecified, or a date with 'unit' specified.`),_},m=new Array,O=S(()=>{let v=e.next(),_=p(v.value);return y(_)?v:(m.push(v),{done:!0})}),A=et.init(),[b,d]=l(o)?o:[o,o],g,E=v=>{g=g===void 0||g<v?v:g},I=[],P=S(()=>{let v=m.pop()||e.next();if(v.done)return v;let _=I;l(s)&&(_=s.map(tt=>D(v.value,tt)),u(_.every($),"$densify: Partition fields must evaluate to string values.")),u(h(v.value),"$densify: collection must contain documents");let mt=p(v.value);A.has(_)||(b=="full"?(A.has(I)||A.set(I,mt),A.set(_,A.get(I))):b=="partition"?A.set(_,mt):A.set(_,b));let rt=A.get(_);if(mt<=rt||d!="full"&&d!="partition"&&rt>=d)return rt<=mt&&A.set(_,a(rt)),E(mt),v;A.set(_,a(rt)),E(rt);let V={[t.field]:rt};if(_)for(let tt=0;tt<_.length;tt++)V[s[tt]]=_[tt];return m.push(v),{done:!1,value:V}});if(b!=="full")return Nt(O,P);let U=-1,k,J=S(()=>{if(U===-1){let v=A.get(I);A.delete(I),k=Array.from(A.keys()),k.length===0&&(k.push(I),A.set(I,v)),U++}do{let v=k[U],_=A.get(v);if(_<g){A.set(v,a(_));let mt={[t.field]:_};for(let rt=0;rt<v.length;rt++)mt[s[rt]]=v[rt];return{done:!1,value:mt}}U++}while(U<k.length);return{done:!0}});return Nt(O,P,J)}function pc(e,t,r){return r.processingMode&1||(r={...C.init(r).options,processingMode:1}),e.transform(n=>{let o={};for(let i of Object.keys(t))o[i]=new bt(t[i],r).run(n);return S([o])})}var Ee=new WeakMap;function Ct(e,t,r,n){Ee.has(e)||Ee.set(e,{});let o=Ee.get(e);t.field in o||(o[t.field]=r());let i=!1;try{let s=n(o[t.field]);return i=!0,s}finally{i?t.documentNumber===e.length&&(delete o[t.field],Object.keys(o).length===0&&Ee.delete(e)):Ee.delete(e)}}function ur(e,t,r,n,o){return Ct(t,r,()=>{let i="$"+Object.keys(r.parentExpr.sortBy)[0],s=N(t,i,n),a=zt(s,((m,O)=>s[O])),c=0,p=0;for(let m of a.keys()){let O=a.get(m).length;a.set(m,[c++,p]),p+=O}return{values:s,groups:a}},({values:i,groups:s})=>{if(s.size==t.length)return r.documentNumber;let a=i[r.documentNumber-1],[c,p]=s.get(a);return(o?c:p)+1})}var uc=(e,t,r,n,o)=>t+(o-e)*((n-t)/(r-e)),Kr=(e,t,r,n)=>Ct(t,r,()=>{let o="$"+Object.keys(r.parentExpr.sortBy)[0],i=N(t,[o,r.inputExpr],n).filter((([c,p])=>x(+c))),s=-1,a=0;for(;a<i.length;){for(;s+1<i.length&&x(i[s+1][1]);)s++,a=s;for(;a+1<i.length&&!x(i[a+1][1]);)a++;if(a+1>=i.length)break;for(a++;s+1<a;)i[s+1][1]=uc(i[s][0],i[s][1],i[a][0],i[a][1],i[s+1][0]),s++;s=a}return i.map(([c,p])=>p)},o=>o[r.documentNumber-1]);var Qr=(e,t,r,n)=>Ct(t,r,()=>{let o=N(t,r.inputExpr,n);for(let i=1;i<o.length;i++)y(o[i])&&(o[i]=o[i-1]);return o},o=>o[r.documentNumber-1]);var mr="_id";function Ie(e,t,r){u(T(t,mr),"$group specification must include an '_id'");let n=t[mr],o=C.init(r),i=Object.keys(t).filter(s=>s!=mr);return e.transform(s=>{let a=zt(s,m=>f(m,n,r)),c=-1,p=Array.from(a.keys());return S(()=>{if(++c===a.size)return{done:!0};let m=p[c],O={};m!==void 0&&(O[mr]=m);for(let A of i)O[A]=f(a.get(m),t[A],o.update({root:null,groupId:m}));return{value:O,done:!1}})})}var mc=["$denseRank","$documentNumber","$first","$last","$linearFill","$rank","$shift"],fc=["$denseRank","$expMovingAvg","$linearFill","$locf","$rank","$shift"],co=e=>{let t=e?.documents||e?.range;return!t||t[0]==="unbounded"&&t[1]==="unbounded"},fr="$setWindowFields";function Hr(e,t,r){r=C.init(r),r.context.addExpressionOps({$function:Mr});let n={},o=Object.keys(t.output);for(let i of o){let s=t.output[i],a=Object.keys(s),c=a.find(ut),p=r.context;if(u(c&&(!!p.getOperator("window",c)||!!p.getOperator("accumulator",c)),`${fr} '${c}' is not a valid window operator`),u(a.length>0&&a.length<=2&&(a.length==1||a.includes("window")),`${fr} 'output' option should have a single window operator.`),s?.window){let{documents:m,range:O}=s.window;u((+!!m^+!!O)===1,"'window' option supports only one of 'documents' or 'range'.")}n[i]=c}return t.sortBy&&(e=st(e,t.sortBy,r)),e=Ie(e,{_id:t.partitionBy,items:{$push:"$$CURRENT"}},r),e.transform(i=>{let s=[],a=[];for(let c of o){let p=t.output[c],m=n[c],O={operatorName:m,func:{left:r.context.getOperator("accumulator",m),right:r.context.getOperator("window",m)},args:p[m],field:c,window:p.window},A=co(O.window);if(A==!1||mc.includes(m)){let b=A?`'${m}'`:"bounded window operations";u(t.sortBy,`${fr} 'sortBy' is required for ${b}.`)}u(A||!fc.includes(m),`${fr} cannot use bounded window for operator '${m}'.`),a.push(O)}for(let c of i){let p=c.items,m=S(p),O={};for(let A of a){let{func:b,args:d,field:g,window:E}=A,I=P=>{let U=-1;return k=>{if(++U,b.left)return b.left(P(k,U),d,r);if(b.right)return b.right(k,P(k,U),{parentExpr:t,inputExpr:d,documentNumber:U+1,field:g},r)}};if(E){let{documents:P,range:U,unit:k}=E,J=P||U;if(!co(E)){let[v,_]=J,mt=tt=>v=="current"?tt:v=="unbounded"?0:Math.max(v+tt,0),rt=tt=>_=="current"?tt+1:_=="unbounded"?p.length:_+tt+1,V=(tt,Zt)=>{if(P||J?.every($))return p.slice(mt(Zt),rt(Zt));let te=Object.keys(t.sortBy)[0],lr,yr;if(k){let Ut=new Date(tt[te]),me=Po=>xe(tt,{startDate:Ut,unit:k,amount:Po},r).getTime();lr=x(v)?me(v):-1/0,yr=x(_)?me(_):1/0}else{let Ut=tt[te];lr=x(v)?Ut+v:-1/0,yr=x(_)?Ut+_:1/0}let ln=v=="current"?Zt:0,Ro=_=="current"?Zt+1:p.length,yn=new Array;for(;ln<Ro;){let Ut=p[ln++],me=+Ut[te];me>=lr&&me<=yr&&yn.push(Ut)}return yn};O[g]=I(V)}}O[g]||(O[g]=I(P=>p)),m=St(m,{[g]:{$function:{body:P=>O[g](P),args:["$$CURRENT"]}}},r)}s.push(m)}return Nt(...s)})}var lc={locf:"$locf",linear:"$linearFill"};function yc(e,t,r){u(!t.sortBy||h(t.sortBy),"sortBy must be an object."),u(!!t.sortBy||Object.values(t.output).every(s=>T(s,"value")),"sortBy required if any output field specifies a 'method'."),u(!(t.partitionBy&&t.partitionByFields),"specify either partitionBy or partitionByFields."),u(!t.partitionByFields||t?.partitionByFields?.every(s=>s[0]!=="$"),"fields in partitionByFields cannot begin with '$'."),r.context.addExpressionOps({$ifNull:_r}),r.context.addWindowOps({$locf:Qr,$linearFill:Kr});let n=t.partitionBy||t?.partitionByFields?.map(s=>"$"+s),o={},i={};for(let s of Object.keys(t.output)){let a=t.output[s];if(T(a,"value")){let c=a;o[s]={$ifNull:[`$$CURRENT.${s}`,c.value]}}else{let c=a,p=lc[c.method];u(!!p,`invalid fill method '${c.method}'.`),i[s]={[p]:"$"+s}}}return Object.keys(i).length>0&&(e=Hr(e,{sortBy:t.sortBy||{},partitionBy:n,output:i},r)),Object.keys(o).length>0&&(e=St(e,o,r)),e}function Gr(e,t,r){let{let:n,foreignField:o,localField:i}=t,s=A=>[!0,[]],a=$(t.from)?Et("$lookup",t.from,r):t.from,{documents:c,pipeline:p}=ze(t.pipeline??[],r);if(u(!a!=!c,"$lookup: must specify single join input with `expr.from` or `expr.pipeline`."),a=a??c,u(l(a),"$lookup: join collection must resolve to an array."),o&&i){let A=et.init();for(let b of a)for(let d of ct(D(b,o)??null)){let g=A.get(d),E=g??[];E.push(b),E!==g&&A.set(d,E)}if(s=b=>{let d=D(b,i)??null;if(l(d)){if(p?.length)return[d.some(I=>A.has(I)),[]];let E=Array.from(new Set(pt(d.map(I=>A.get(I)))));return[E.length>0,E]}let g=A.get(d)??null;return[g!==null,g??[]]},p?.length===0)return e.map(b=>({...b,[t.as]:s(b)[1]}))}let m=new bt(p??[],r),O=C.init(r);return e.map(A=>{let b=f(A,n,r);O.update({root:null,variables:b});let[d,g]=s(A);return{...A,[t.as]:d?m.run(a,O):g}})}function Oc(e,t,r){let n=Et("$graphLookup",t.from,r);u(l(n),"$graphLookup: expression 'from' must resolve to array");let{connectFromField:o,connectToField:i,as:s,maxDepth:a,depthField:c,restrictSearchWithMatch:p}=t,m=new G(p??{},r);return e.map(O=>{let A={};yt(A,o,f(O,t.startWith,r));let b=[A],d=-1,g=et.init();do{d++,b=pt(Gr(S(b),{from:n,localField:o,foreignField:i,as:s},r).map(U=>U[s]).collect()),b=b.filter(U=>m.test(U));let P=g.size;for(let U of b)g.set(U,g.get(U)??d);if(P==g.size)break}while(y(a)||d<a);let E=new Array(g.size),I=0;for(let[P,U]of g.entries())E[I++]=Object.assign(c?{[c]:U}:{},P);return{...O,[s]:E}})}function Ac(e,t,r){let n=new G(t,r);return e.filter(o=>n.test(o))}function bc(e,t,r){let n=Et("$merge",t.into,r);u(l(n),"$merge: expression 'into' must resolve to an array");let o=t.on||r.idKey,i=$(o)?c=>ft(D(c,o)):c=>ft(o.map(p=>D(c,p))),s=et.init();for(let c=0;c<n.length;c++){let p=n[c],m=i(p);u(!s.has(m),"$merge: 'into' collection must have unique entries for the 'on' field."),s.set(m,[p,c])}let a=C.init(r);return e.map(c=>{let p=i(c);if(s.has(p)){let[m,O]=s.get(p),A=f(m,t.let||{new:"$$ROOT"},a.update({root:c}));if(l(t.whenMatched)){let b=new bt(t.whenMatched,a.update({root:null,variables:A}));n[O]=b.run([m])[0]}else switch(t.whenMatched){case"replace":n[O]=c;break;case"fail":throw new kt("$merge: failed due to matching as specified by 'whenMatched' option.");case"keepExisting":break;default:n[O]=Ur(m,[m,c],a.update({root:c,variables:A}));break}}else switch(t.whenNotMatched){case"discard":break;case"fail":throw new kt("$merge: failed due to matching as specified by 'whenMatched' option.");default:n.push(c);break}return c})}function dc(e,t,r){let n=Et("$out",t,r);return u(l(n),"$out: expression must resolve to an array"),e.map(o=>(n.push(it(o)),o))}function gc(e,t,r){let n=C.init(r);return e.map(o=>Xr(o,t,n.update({root:o})))}function Xr(e,t,r){let n=f(e,t,r);switch(n){case"$$KEEP":return e;case"$$PRUNE":return;case"$$DESCEND":{if(!T(t,"$cond"))return e;let o={};for(let i of Object.keys(e)){let s=e[i];if(l(s)){let a=new Array;for(let c of s)h(c)&&(c=Xr(c,t,r.update({root:c}))),y(c)||a.push(c);o[i]=a}else if(h(s)){let a=Xr(s,t,r.update({root:s}));y(a)||(o[i]=a)}else o[i]=s}return o}default:return n}}function Jr(e,t,r){return e.map(n=>(n=f(n,t.newRoot,r),u(h(n),"$replaceRoot expression must return an object"),n))}function Zr(e,t,r){return e.map(n=>(n=f(n,t,r),u(h(n),"$replaceWith expression must return an object"),n))}function xc(e,t,r){return e.transform(n=>{let o=n.length,i=-1;return S(()=>{if(++i===t.size)return{done:!0};let s=Math.floor(Math.random()*o);return{value:n[s],done:!1}})})}var tn=St;function hc(e,t,r){return st(Ie(e,{_id:t,count:{$sum:1}},r),{count:-1},r)}function jc(e,t,r){let{coll:n,pipeline:o}=$(t)||l(t)?{coll:t}:t,i=$(n)?Et("$unionWith",n,r):n,{documents:s,pipeline:a}=ze(o,r);u(i||s,"$unionWith must specify single collection input with `expr.coll` or `expr.pipeline`.");let c=i??s;return Nt(e,a?new bt(a,r).stream(c):S(c))}function en(e,t,r){t=ct(t);let n={};for(let o of t)n[o]=0;return Kt(e,n,r)}function $c(e,t,r){$(t)&&(t={path:t});let o=t.path.substring(1),i=t?.includeArrayIndex||!1,s=t.preserveNullAndEmptyArrays||!1,a=(p,m)=>(i!==!1&&(p[i]=m),p),c;return S(()=>{for(;;){if(c instanceof ye){let O=c.next();if(!O.done)return O}let p=e.next();if(p.done)return p;let m=p.value;if(c=D(m,o),l(c)){if(c.length===0&&s===!0)return c=null,Wt(m,o),{value:a(m,null),done:!1};c=S(c).map(((O,A)=>{let b=Tt(m,o,{preserveKeys:!0});return yt(b,o,O),a(b,A)}))}else if(!le(c)||s===!0)return{value:a(m,null),done:!1}}})}var nn={};ht(nn,{$elemMatch:()=>Ec,$slice:()=>Ic});var Ec=(e,t,r,n)=>{let o=D(e,r),i=new G(t,n);if(!l(o))return;let s=[];for(let a=0;a<o.length;a++)if(i.test(o[a])){if(n.useStrictMode)return[o[a]];s.push(o[a])}return s.length>0?s:void 0};var Ic=(e,t,r,n)=>{let o=D(e,r);return l(o)?Tr(e,l(t)?[o,...t]:[o,t],n):o};var Xt={};ht(Xt,{$all:()=>Tc,$and:()=>Rc,$bitsAllClear:()=>po,$bitsAllSet:()=>uo,$bitsAnyClear:()=>mo,$bitsAnySet:()=>fo,$elemMatch:()=>vc,$eq:()=>lo,$exists:()=>Nc,$expr:()=>Cc,$gt:()=>yo,$gte:()=>Oo,$in:()=>Ao,$jsonSchema:()=>kc,$lt:()=>bo,$lte:()=>go,$mod:()=>_c,$ne:()=>xo,$nin:()=>ho,$nor:()=>Pc,$not:()=>Uc,$or:()=>on,$regex:()=>Mc,$size:()=>wc,$type:()=>Sc,$where:()=>Dc});var Tc=(e,t,r)=>B(e,t,r,Rn);var vc=(e,t,r)=>B(e,t,r,He);var wc=(e,t,r)=>B(e,t,r,Pn);var Rt=(e,t,r,n)=>B(e,t,r,(o,i,s)=>{let a=0;if(l(i))for(let c of i)a=a|1<<c;else a=i;return n(o&a,a)});var po=(e,t,r)=>Rt(e,t,r,(n,o)=>n==0);var uo=(e,t,r)=>Rt(e,t,r,(n,o)=>n==o);var mo=(e,t,r)=>Rt(e,t,r,(n,o)=>n<o);var fo=(e,t,r)=>Rt(e,t,r,(n,o)=>n>0);var lo=(e,t,r)=>B(e,t,r,be);var yo=(e,t,r)=>B(e,t,r,Ke);var Oo=(e,t,r)=>B(e,t,r,Qe);var Ao=(e,t,r)=>B(e,t,r,kr);var bo=(e,t,r)=>B(e,t,r,Ye);var go=(e,t,r)=>B(e,t,r,Ve);var xo=(e,t,r)=>B(e,t,r,qe);var ho=(e,t,r)=>B(e,t,r,_n);var Nc=(e,t,r)=>{let n=e.includes("."),o=!!t;if(!n||e.match(/\.\d+$/))return a=>D(a,e)!==void 0===o;let i=e.substring(0,e.lastIndexOf(".")),s={preserveIndex:"default"};return a=>{let c=Tt(a,e,s),p=D(c,i);return l(p)?p.some(m=>m!==void 0)===o:p!==void 0===o}};var Sc=(e,t,r)=>B(e,t,r,Un);function Cc(e,t,r){return n=>nt(f(n,t,r),r.useStrictMode)}function kc(e,t,r){u(!!r?.jsonSchemaValidator,"$jsonSchema requires 'jsonSchemaValidator' option to be defined.");let n=r.jsonSchemaValidator(t);return o=>n(o)}var _c=(e,t,r)=>B(e,t,r,Mn);var Mc=(e,t,r)=>B(e,t,r,Dn);function Dc(e,t,r){u(r.scriptEnabled,"$where requires 'scriptEnabled' option to be true");let n=t;return u(br(n),"$where only accepts a Function objects"),o=>nt(n.call(o),r?.useStrictMode)}var Rc=(e,t,r)=>{u(l(t),"$and expects value to be an Array.");let n=t.map(o=>new G(o,r));return o=>n.every(i=>i.test(o))};function on(e,t,r){u(l(t),"Invalid expression. $or expects value to be an Array");let n=t.map(o=>new G(o,r));return o=>n.some(i=>i.test(o))}function Pc(e,t,r){u(l(t),"Invalid expression. $nor expects value to be an array.");let n=on("$or",t,r);return o=>!n(o)}function Uc(e,t,r){let n={};n[e]=qt(t);let o=new G(n,r);return i=>!o.test(i)}var sn={};ht(sn,{$denseRank:()=>Fc,$derivative:()=>Bc,$documentNumber:()=>Lc,$expMovingAvg:()=>zc,$integral:()=>Wc,$linearFill:()=>Kr,$locf:()=>Qr,$minMaxScaler:()=>qc,$rank:()=>Yc,$shift:()=>Vc});var Fc=(e,t,r,n)=>ur(e,t,r,n,!0);var Bc=(e,t,r,n)=>{if(t.length<2)return null;let{input:o,unit:i}=r.inputExpr,s="$"+Object.keys(r.parentExpr.sortBy)[0],a=[t[0],t[t.length-1]],c=N(a,[s,o],n).filter((([d,g])=>x(+d)&&x(+g)));u(c.length===2,"$derivative arguments must resolve to numeric");let[[p,m],[O,A]]=c,b=(O-p)/xt[i??"millisecond"];return(A-m)/b};var Lc=(e,t,r,n)=>r.documentNumber;var zc=(e,t,r,n)=>{let{input:o,N:i,alpha:s}=r.inputExpr;return u(!(i&&s),"$expMovingAvg: must provide either 'N' or 'alpha' field."),u(!i||x(i)&&i>0,`$expMovingAvg: 'N' must be greater than zero. Got ${i}.`),u(!s||x(s)&&s>0&&s<1,`$expMovingAvg: 'alpha' must be between 0 and 1 (exclusive), found alpha: ${s}`),Ct(t,r,()=>{let a=i!=null?2/(i+1):s,c=N(t,o,n);for(let p=0;p<c.length;p++){if(p===0){x(c[p])||(c[p]=null);continue}if(!x(c[p])){c[p]=c[p-1];continue}x(c[p-1])&&(c[p]=c[p]*a+c[p-1]*(1-a))}return c},a=>a[r.documentNumber-1])};var Wc=(e,t,r,n)=>{let{input:o,unit:i}=r.inputExpr,s="$"+Object.keys(r.parentExpr.sortBy)[0],a=N(t,[s,o],n).filter((([m,O])=>x(+m)&&x(+O))),c=a.length;u(t.length===c,"$integral expects an array of numeric values");let p=0;for(let m=1;m<c;m++){let[O,A]=a[m-1],[b,d]=a[m],g=(b-O)/xt[i??"millisecond"];p+=.5*(A+d)*g}return p};var qc=(e,t,r,n)=>Ct(t,r,()=>{let o=r.inputExpr,i=o.min||0,s=o.max||1,a=N(t,o.input||r.inputExpr,n);u(l(a)&&a.length>0&&a.every(x),"$minMaxScaler: input must be a numeric array");let c=a[0],p=a[0];for(let A of a)A<c?c=A:A>p&&(p=A);let m=s-i,O=p-c;return u(O!==0,"$minMaxScaler: input range must not be zero"),{min:i,scale:m,rmin:c,range:O,nums:a}},o=>{let{min:i,rmin:s,scale:a,range:c,nums:p}=o;return(p[r.documentNumber-1]-s)/c*a+i});var Yc=(e,t,r,n)=>ur(e,t,r,n,!1);var Vc=(e,t,r,n)=>{let o=r.inputExpr,i=r.documentNumber-1+o.by;return i<0||i>t.length-1?f(e,o.default,n)??null:f(t[i],o.output,n)};var mn={};ht(mn,{$addToSet:()=>Qc,$bit:()=>Gc,$currentDate:()=>Xc,$inc:()=>Jc,$max:()=>Zc,$min:()=>tp,$mul:()=>ep,$pop:()=>rp,$pull:()=>pn,$pullAll:()=>np,$push:()=>op,$rename:()=>sp,$set:()=>un,$unset:()=>ap});var Y=C.init({context:Ot.init().addQueryOps(Xt).addExpressionOps(Ae).addExpressionOps(de)}).update({updateConfig:{cloneMode:"copy"}}),Pt=(e,t)=>{switch(t?.local?.updateConfig?.cloneMode){case"deep":return it(e);case"copy":return R(e)?new Date(e):l(e)?e.slice():h(e)?Object.assign({},e):ot(e)?new RegExp(e):e}return e},an="$",$o="$[]",q=(e,t,r,n,o)=>{let{selector:i,position:s,next:a}=t;if(!s){let m=!1;return re(e,i,(A,b)=>m=!!n(A,b)||m,o),m}let c=D(e,i);if(!l(c)||!c.length)return!1;if(s===an){let m=c.findIndex(O=>r[i].test({[i]:[O]}));return u(m>-1,"BUG: positional operator found no match for "+i),a?q(c[m],a,r,n,o):n(c,m)}let p=!1;for(let m=0;m<c.length;m++){let O=c[m];s!==$o&&r[s]&&!r[s].test({[s]:[O]})||(p=a?q(O,a,r,n,o)||p:n(c,m)||p)}return p},jo="You must include the array field for '.$' as part of the query document.",Kc=(e,t)=>`Performing an update on the path '${e}' would modify the immutable field '${t}'.`;function K(e,t,r,n){let o=r,i=o.local.updateParams??cn([e],t,o),s=[];for(let a of Object.keys(e)){let{node:c,queries:p}=i[a];n(e[a],c,p)&&s.push(c.selector)}return s.sort()}function cn(e,t,r){let n={};t||=[];let o=t.reduce((c,p)=>{for(let m of Object.keys(p)){let O=m.substring(0,m.indexOf("."))||m;c[O]?c[O][m]=p[m]:c[O]={[m]:p[m]}}return c},{}),{condition:i}=r.local;i=i??{};let s=Object.keys(i),a=new vt;for(let c of e)for(let p of Object.keys(c)){fe(p),u(!p.startsWith("$"),`Dollar ($) prefixed field paths is not allowed in update operations: '${p}'.`);let m=[],O=p.includes("$")?{selector:""}:{selector:p};O.selector||p.split(".").reduce((d,g)=>{if(g===an||g===$o)d.position=g;else if(g.startsWith("$[")&&g.endsWith("]")){let E=g.slice(2,-1);u(/^[a-z]+\w*$/.test(E),`The filter <identifier> must begin with a lowercase letter and contain only alphanumeric characters. '${g}' is invalid.`),m.push(E),d.position=E}else if(!d.selector)d.selector=g;else if(!d.position)d.selector+="."+g;else return d.next={selector:g},d.next;return d},O);let A={};if(m.length){let d={};for(let g of m)d[g]=o[g];for(let g of Object.keys(d))A[g]=new G(d[g],r)}if(O.position===an){let d=O.selector;u(s&&s.length,jo);let g=s.filter(I=>I===d||I.startsWith(d+"."));u(g.length===1,jo);let E=g[0];A[d]=new G({[E]:i[E]},r)}let b=r.idKey;u(O.selector!==b&&!O.selector.startsWith(`${b}.`),Kc(O.selector,b)),u(a.add(O.selector),`updating the path '${O.selector}' would create a conflict at '${O.selector}'`),n[p]={node:O,queries:A}}return n}function Qc(e,t=[],r=Y){for(let n of Object.values(e))u(!(h(n)&&T(n,"$each"))||l(n.$each),"The argument to $each in $addToSet must be an array.");return n=>K(e,t,r,(o,i,s)=>{let a={$each:[o]};return h(o)&&T(o,"$each")&&Object.assign(a,o),q(n,i,s,(c,p)=>{let m=c[p];if(l(m)){let O=$t(m.concat(a.$each));if(O.length===m.length)return!1;c[p]=Pt(O,r)}else if(m===void 0)c[p]=Pt(a.$each,r);else return!1;return!0},{buildGraph:!0})})}var Hc=["and","or","xor"];function Gc(e,t=[],r=Y){for(let n of Object.values(e)){u(h(n),"$bit operator expression must be an object.");let o=Object.keys(n);u(o.length===1&&Hc.includes(o[0]),`$bit spec is invalid '${o[0]}'. Must be one of 'and', 'or', or 'xor'.`),u(w(n[o[0]]),`$bit expression value must be an integer. Got ${at(n[o[0]])}`)}return n=>K(e,t,r,(o,i,s)=>{let a=Object.keys(o);return q(n,i,s,(c,p)=>{let m=c[p],O=o[a[0]],A=m===void 0;if(!A&&!w(m))return!1;switch(m=m||0,a[0]){case"and":return(c[p]=m&O)!==m||A;case"or":return(c[p]=m|O)!==m||A;case"xor":return(c[p]=m^O)!==m||A}},{buildGraph:!0})})}function Xc(e,t=[],r=Y){for(let n of Object.values(e))u(n===!0||h(n)&&(n.$type==="date"||n.$type==="timestamp"),"Invalid type for $currentDate. Please use a boolean ('true') or a $type expression ({$type: 'timestamp/date'}).");return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>(a[c]=o===!0||o.$type==="date"?r.now:r.now.getTime(),!0),{buildGraph:!0}))}function Jc(e,t=[],r=Y){for(let n of Object.values(e))u(x(n),"Cannot increment with non-numeric argument.");return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>{if(x(a[c])||a[c]===void 0){let p=a[c];return a[c]||=0,a[c]+=o,a[c]!==p}return!1},{buildGraph:!0}))}function Zc(e,t=[],r=Y){return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>M(a[c],o)>-1?!1:(a[c]=o,!0),{buildGraph:!0}))}function tp(e,t=[],r=Y){return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>a[c]!==void 0&&M(a[c],o)<1?!1:(a[c]=o,!0),{buildGraph:!0}))}function ep(e,t=[],r=Y){for(let n of Object.values(e))u(x(n),"Cannot multiply with non-numeric argument.");return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>{let p=a[c];return x(a[c])?a[c]=a[c]*o:a[c]===void 0&&(a[c]=0),a[c]!==p},{buildGraph:!0}))}function rp(e,t=[],r=Y){for(let n of Object.values(e))u(n===1||n===-1,"$pop argument must be 1 or -1.");return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>{let p=a[c];return!l(p)||!p.length?!1:(o===-1?p.splice(0,1):p.pop(),!0)}))}function pn(e,t=[],r=Y){let n=Object.values(e).map(o=>{let i=!h(o)||Object.keys(o).some(ut),s=new G(i?{k:o}:o,r);return i?a=>s.test({k:a}):a=>s.test(a)});return o=>{let i=0;return K(e,t,r,(s,a,c)=>{let p=n[i++];return q(o,a,c,(m,O)=>{let A=m[O];if(!l(A)||!A.length)return!1;let b=new Array,d=!1;for(let g of A){let E=p(g);E||b.push(g),d||=E}return d?(m[O]=b,!0):!1})})}}function np(e,t=[],r=Y){let n={};for(let o of Object.keys(e))u(l(e[o]),"$pullAll requires an array argument."),n[o]={$in:e[o]};return pn(n,t,r)}var Eo=["$each","$slice","$sort","$position"];function op(e,t=[],r=Y){for(let n of Object.values(e))h(n)&&Eo.some(o=>T(n,o))&&(u(l(n.$each),"The argument to $each in $push must be an array."),u(n.$slice===void 0||w(n.$slice),"The value for $slice must be an integer value."),u(n.$position===void 0||w(n.$position),"The value for $position must be an integer value."));return n=>K(e,t,r,(o,i,s)=>{let a={$each:[o]};return h(o)&&Eo.some(c=>T(o,c))&&Object.assign(a,o),q(n,i,s,(c,p)=>{let m=c[p];if(!l(m))return m===void 0?(c[p]=Pt(a.$each,r),!0):!1;let O=m.slice(0,a.$slice||m.length),A=m.length,b=x(a.$position)?a.$position:m.length;if(m.splice(b,0,...Pt(a.$each,r)),a.$sort){let d=h(a.$sort)?Object.keys(a.$sort)[0]:"",g=d?a.$sort[d]:a.$sort,E=d?I=>D(I,d):I=>I;m.sort((I,P)=>g*M(E(I),E(P)))}return x(a.$slice)&&(a.$slice<0?m.splice(0,m.length+a.$slice):m.splice(a.$slice)),A!=m.length||!Q(O,m)},{descendArray:!0,buildGraph:!0})})}function un(e,t=[],r=Y){return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>Q(a[c],o)?!1:(a[c]=Pt(o,r),!0),{buildGraph:!0}))}var ip=(e,t)=>e===t||e.startsWith(`${t}.`);function sp(e,t=[],r=Y){let n=r.idKey;for(let o of Object.values(e))u(typeof o=="string","$rename target must be a string."),u(!ip(o,n),`Performing an update on the path '${o}' would modify the immutable field '${n}'.`);return o=>{let i=[],s=K(e,t,r,(a,c,p)=>q(o,c,p,(m,O)=>T(m,O)?(Array.prototype.push.apply(i,un({[a]:m[O]},t,r)(o)),delete m[O],!0):!1));return Array.from(new Set(s.concat(i)))}}function ap(e,t=[],r=Y){return n=>K(e,t,r,(o,i,s)=>q(n,i,s,(a,c)=>{if(!T(a,c))return!1;let p=a[c];return l(a)?a[c]=null:delete a[c],!Q(p,a[c])}))}var Io=mn,To={$addFields:St,$set:tn,$project:Kt,$unset:en,$replaceRoot:Jr,$replaceWith:Zr};function vo(e,t,r,n,o){return fn([e],n||{},t,{arrayFilters:r,cloneMode:o?.cloneMode??"copy"},o?.queryOptions).modifiedFields??[]}function wo(e,t,r,n={},o){let{modifiedCount:i,matchedCount:s}=No(e,t,r,n,o);return{modifiedCount:i,matchedCount:s}}function fn(e,t,r,n={},o){return No(e,t,r,n,{...o,firstOnly:!0})}function No(e,t,r,n={},o){o||={};let i=o?.firstOnly??!1,s=C.init({...o,collation:Object.assign({},o?.collation,n?.collation)}).update({condition:t,updateConfig:{cloneMode:"copy",...n},variables:n.let,updateParams:{}});s.context.addExpressionOps(Ae).addExpressionOps(de).addQueryOps(Xt).addPipelineOps(To);let a=Object.keys(t).length>0,c=new Map,p=S(e);if(a){let k=new G(t,s);p=p.filter((J,v)=>k.test(J)?(c.set(J,v),!0):!1)}let m=-1;if(i){let k=new Map;n.sort&&(a||(p=p.map((v,_)=>(k.set(v,_),v))),p=st(p,n.sort,s)),p=p.take(1);let J=p.collect()[0];m=c.get(J)??k.get(J)??0}let O=p.collect();if(O.length===0)return{matchedCount:0,modifiedCount:0};if(l(r)){let k=i?[m]:Array.from(c.values()),J=k.length?k.map(V=>ft(e[V])):O.map(V=>ft(V)),v={matchedCount:J.length,modifiedCount:0},_=i?it(e[k[0]]):void 0,mt=S(O);for(let V of r){let[tt,Zt]=Object.entries(V)[0],te=To[tt];u(te,`Unknown pipeline operator: '${tt}'.`),mt=te(mt,Zt,s)}let rt=mt.collect();if(k.length){u(k.length===rt.length,"bug: indexes and result size must match.");for(let V=0;V<k.length;V++)ft(rt[V])!==J[V]&&(e[k[V]]=rt[V],v.modifiedCount++)}else for(let V=0;V<e.length;V++)ft(rt[V])!==J[V]&&(e[V]=rt[V],v.modifiedCount++);if(i&&v.modifiedCount&&_){let V=e[k[0]],tt=cp(r,_,V);u(tt.length,"bug: failed to retrieve modified fields"),Object.assign(v,{modifiedFields:tt,modifiedIndex:m})}return v}let A=Object.keys(r).find(k=>!Io[k]);u(!A,`Unknown update operator: '${A}'.`);let b=n?.arrayFilters??[],d=Object.values(r.$rename??{}).map(k=>({[k]:!0})),g=[...Object.values(r),...d];s.update({updateParams:cn(g,b,s)});let I={matchedCount:O.length,modifiedCount:0},P=[],U=[];for(let k of Object.keys(r)){let J=Io[k],v=r[k];U.push(J(v,b,s))}for(let k of O){let J=!1;for(let v of U){let _=v(k);_.length&&(J=!0,i&&Array.prototype.push.apply(P,_))}I.modifiedCount+=+J}return i&&P.length&&(P.sort(),Object.assign(I,{modifiedFields:P,modifiedIndex:m})),I}function cp(e,t,r){let n=[];for(let c of e){let p=Object.keys(c)[0],m=c[p];switch(p){case"$addFields":case"$set":case"$project":case"$replaceWith":n.push(...Object.keys(m));break;case"$unset":n.push(...ct(m));break;case"$replaceRoot":n.length=0,n.push(...Object.keys(m?.newRoot));break}}let o=new Set(n.sort()),i=new vt,s=[];for(let c of o)i.add(c)&&!Q(D(r,c),D(t,c))&&s.push(c);for(let c of Object.keys(t))o.has(c)||(!i.add(c)||!Q(r[c],t[c]))&&s.push(c);let a=new vt;return s.sort().filter(c=>a.add(c))}var So=Ot.init({accumulator:Er,expression:Vr,pipeline:rn,projection:nn,query:Xt,window:sn}),Jt=e=>Object.assign({...e,context:e?.context?Ot.from(So,e?.context):So}),Te=class extends G{constructor(t,r){super(t,Jt(r))}},ve=class extends bt{constructor(t,r){super(t,Jt(r))}};function Co(e,t,r,n){return new Te(t,Jt(n)).find(e,r)}function ko(e,t,r){return new ve(t,Jt(r)).run(e)}function _o(e,t,r,n,o){return vo(e,t,r,n,{cloneMode:o?.cloneMode,queryOptions:Jt(o?.queryOptions)})}function Mo(e,t,r,n={},o){return wo(e,t,r,n,Jt(o))}function Do(e,t,r,n={},o){return fn(e,t,r,n,Jt(o))}var up={Aggregator:ve,Context:Ot,ProcessingMode:wt,Query:Te,aggregate:ko,find:Co,update:_o,updateMany:Mo,updateOne:Do};return zo(mp);})();