/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["./shared","./sqlUtils","../../../core/sql/WhereClause"],(function(e,r,t){"use strict";function n(n){if("function"===n.parseTree.type){if(0===n.parseTree.args.value.length)return{name:n.parseTree.name,expr:null};if(n.parseTree.args.value.length>1)throw new Error("Statistic does not have 1 or 0 Parameters");const a=t.WhereClause.create(r.toWhereClauseFromTree(n.parseTree.args.value[0],e.FeatureServiceDatabaseType.Standardised,n.parameters),n.fieldsIndex);return{name:n.parseTree.name,expr:a}}return null}return function(){function e(){}var r=e.prototype;return r.clone=function(){const r=new e;return r.field=this.field,r.tofieldname=this.tofieldname,r.typeofstat=this.typeofstat,r.workingexpr=this.workingexpr,r},e.parseStatField=function(r,a,i){const s=new e;s.field=r;const o=t.WhereClause.create(a,i),l=n(o);if(null===l)throw new Error("Invalid Statistic Function");const u=l.name.toUpperCase().trim();if("MIN"===u){if(s.typeofstat="MIN",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}else if("MAX"===u){if(s.typeofstat="MAX",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}else if("COUNT"===u)s.typeofstat="COUNT",s.workingexpr=l.expr;else if("STDEV"===u){if(s.typeofstat="STDDEV",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}else if("SUM"===u){if(s.typeofstat="SUM",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}else if("MEAN"===u){if(s.typeofstat="AVG",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}else if("AVG"===u){if(s.typeofstat="AVG",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}else{if("VAR"!==u)throw new Error("Invalid Statistic Function");if(s.typeofstat="VAR",s.workingexpr=l.expr,null===o)throw new Error("Invalid Statistic Function Parameters")}return s},r.toStatisticsName=function(){switch(this.typeofstat.toUpperCase()){case"MIN":return"min";case"MAX":return"max";case"SUM":return"sum";case"COUNT":default:return"count";case"VAR":return"var";case"STDDEV":return"stddev";case"AVG":return"avg"}},e}()}));
