/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{FeatureSetError as t}from"./errorsupport.js";import{isNumber as n,isString as e,equalityTest as r}from"./shared.js";import{isSingleField as a,predictType as s}from"./sqlUtils.js";import{DateOnly as c}from"../../../core/sql/DateOnly.js";import{SqlTimeStampOffset as o}from"../../../core/sql/SqlTimestampOffset.js";import{TimeOnly as i}from"../../../core/sql/TimeOnly.js";function l(t){return t=+t,isFinite(t)?t-t%1||(t<0?-0:0===t?t:0):t}function u(t){let n=0;for(let e=0;e<t.length;e++)n+=t[e];return n/t.length}function f(t){const n=u(t);let e=0;for(let r=0;r<t.length;r++)e+=(n-t[r])**2;return e/t.length}function h(t){const n=u(t);let e=0;for(let r=0;r<t.length;r++)e+=(n-t[r])**2;return e/(t.length-1)}function m(t){let n=0;for(let e=0;e<t.length;e++)n+=t[e];return n}function g(t,a){const s=[],c={},o=[];for(let i=0;i<t.length;i++){if(void 0!==t[i]&&null!==t[i]){const a=t[i];if(n(a)||e(a))void 0===c[a]&&(s.push(a),c[a]=1);else{let t=!1;for(let n=0;n<o.length;n++)!0===r(o[n],a)&&(t=!0);!1===t&&(o.push(a),s.push(a))}}if(s.length>=a&&-1!==a)return s}return s}function w(t){switch(t.toLowerCase()){case"distinct":return"distinct";case"avg":case"mean":return"avg";case"min":return"min";case"sum":return"sum";case"max":return"max";case"stdev":case"stddev":return"stddev";case"var":case"variance":return"var";case"count":return"count"}return null}function d(t,n,e=1e3){switch(t.toLowerCase()){case"distinct":return g(n,e);case"avg":case"mean":return u(n);case"min":return Math.min.apply(Math,n);case"sum":return m(n);case"max":return Math.max.apply(Math,n);case"stdev":case"stddev":return Math.sqrt(f(n));case"var":case"variance":return f(n);case"count":return n.length}return 0}async function p(t,n,e){const r=await k(t,n,e);return 0===r.length?null:Math.min.apply(Math,r)}async function y(t,n,e){const r=await k(t,n,e);return 0===r.length?null:Math.max.apply(Math,r)}async function v(t,n,e){let r="";n&&!a(n)&&(r=s(n,t.fields));const c=await k(t,n,e);if(0===c.length)return null;const o=u(c);return null===o?o:"integer"===r?l(o):o}async function M(t,n,e){const r=await k(t,n,e);return 0===r.length?null:h(r)}async function q(t,n,e){const r=await k(t,n,e);return 0===r.length?null:Math.sqrt(h(r))}async function T(t,n,e){const r=await k(t,n,e);return 0===r.length?null:m(r)}async function k(n,e,r){const a=await n.queryAll(r),s=[],l={ticker:0};for await(const u of a){if(l.ticker+=u.length,l.ticker>=100&&(l.ticker=0,await new Promise(t=>{setTimeout(t,0)})),r?.aborted)throw new t("Cancelled");for(const t of u){const n=e?.calculateValue(t);null===n||(s[s.length]=n instanceof c||n instanceof i?n.toNumber():n instanceof o?n.toMilliseconds():n)}}return s}async function S(n,e,r=1e3,a=null){if(null==e)return[];const s=await n.queryAll(a),l=[],u=new Set,f={ticker:0};for await(const h of s){if(f.ticker+=h.length,f.ticker>=100&&(f.ticker=0,await new Promise(t=>{setTimeout(t,0)})),a?.aborted)throw new t("Cancelled");for(const t of h){const n=e.calculateValue(t);let a=n;if(n instanceof c?a="!!DATEONLY!!-"+n.toString():n instanceof o?a="!!TSOFFSETONLY!!-"+n.toString():n instanceof i?a="!!TIMEONLY!!-"+n.toString():n instanceof Date&&(a="!!DATE!!-"+n.toString()),null!=n&&(u.has(a)||(l.push(n),u.add(a))),l.length>=r&&-1!==r)return l}}return l}export{d as calculateStat,w as decodeStatType,S as distinct,y as max,v as mean,p as min,q as stdev,T as sum,M as variance};
