/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{DateOnly as n}from"./DateOnly.js";import{SqlError as l}from"./errorSupport.js";import{SqlTimeStampOffset as t}from"./SqlTimestampOffset.js";import{TimeOnly as e}from"./TimeOnly.js";import{DateTime as a}from"luxon";function r(n,t){const e=m[n.toLowerCase()];if(null==e)throw new l("FunctionNotRecognized");if(t.length<e.minParams||t.length>e.maxParams)throw new l("InvalidParameterCount",{name:n.toUpperCase()});return e.evaluate(t)}function u(n,l){const t=m[n.toLowerCase()];return null!=t&&l>=t.minParams&&l<=t.maxParams}const m={min:{minParams:1,maxParams:1,evaluate:n=>s(n[0],"min")},max:{minParams:1,maxParams:1,evaluate:n=>s(n[0],"max")},avg:{minParams:1,maxParams:1,evaluate:n=>o(n[0])},sum:{minParams:1,maxParams:1,evaluate:n=>f(n[0])},stddev:{minParams:1,maxParams:1,evaluate:n=>c(n[0])},count:{minParams:1,maxParams:1,evaluate:n=>null==n[0]?null:n[0].length},var:{minParams:1,maxParams:1,evaluate:n=>g(n[0])}};function o(n){if(null===n)return null;let t=0,e=0;for(let a=0;a<n.length;a++){const r=n[a];if(null!==r){if(!i(r))throw new l("InvalidValueForAggregateFunction");e++,t+=r}}return 0===e?null:t/n.length}function i(n){return"number"==typeof n}function s(l,r){if(null===l)return null;let u=null,m=null;for(const o of l){let l=o;l=n.isDateOnly(o)||e.isTimeOnly(o)?o.toNumber():a.isDateTime(o)?o.toMillis():t.isTimestampOffset(o)?o.toMilliseconds():o,(null===u||"max"===r&&null!==m&&null!==l&&m<=l||"min"===r&&null!==m&&null!==l&&m>=l)&&(u=o,m=l)}return u}function f(n){if(null===n)return null;let t=0;for(let e=0;e<n.length;e++){const a=n[e];if(null!==a){if(!i(a))throw new l("InvalidValueForAggregateFunction");t+=a}}return t}function c(n){if(null===n)return null;const l=g(n);return null===l?null:Math.sqrt(l)}function g(n){if(null===n)return null;if(0===(n=n.filter(n=>null!==n)).length)return null;const t=o(n);if(null===t)return null;let e=0;for(const a of n){if(!i(a))throw new l("InvalidValueForAggregateFunction");e+=(t-a)**2}return e/(n.length-1)}export{r as aggregateFunction,u as isAggregate};
