/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{toWhereClauseFromTree as e}from"./sqlUtils.js";import{SqlError as t}from"../../../core/sql/errorSupport.js";import r from"../../../core/sql/WhereClause.js";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 t("MissingStatisticParameters");const i=r.create(e(n.parseTree.args.value[0],0,n.parameters),{fieldsIndex:n.fieldsIndex,timeZone:n.timeZone,currentUser:n.currentUser});return{name:n.parseTree.name,expr:i}}return null}class i{constructor(){this.field="",this.tofieldname="",this.typeofstat="MIN",this.workingexpr=null}clone(){const e=new i;return e.field=this.field,e.tofieldname=this.tofieldname,e.typeofstat=this.typeofstat,e.workingexpr=this.workingexpr,e}static parseStatField(e,s,a,o){const l=new i;l.field=e;const u=r.create(s,{fieldsIndex:a,timeZone:o}),f=n(u);if(null===f)throw new t("UnsupportedSqlFunction",{function:""});const p=f.name.toUpperCase().trim();if("MIN"===p){if(l.typeofstat="MIN",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:"min"})}else if("MAX"===p){if(l.typeofstat="MAX",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:"max"})}else if("COUNT"===p)l.typeofstat="COUNT",l.workingexpr=f.expr;else if("STDEV"===p){if(l.typeofstat="STDDEV",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:"stdev"})}else if("SUM"===p){if(l.typeofstat="SUM",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:"sum"})}else if("MEAN"===p){if(l.typeofstat="AVG",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:p})}else if("AVG"===p){if(l.typeofstat="AVG",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:"avg"})}else{if("VAR"!==p)throw new t("UnsupportedSqlFunction",{function:p});if(l.typeofstat="VAR",l.workingexpr=f.expr,null===u)throw new t("InvalidFunctionParameters",{function:"var"})}return l}toStatisticsName(){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"}}}export{i as default};
