/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../core/maybe","./attributeSupport","../../../statistics/utils"],(function(t,e,i){"use strict";return function(){function s(t,i,s){this._fieldDataCache=new Map,this._returnDistinctMap=new Map,this.returnDistinctValues=t.returnDistinctValues,this.fieldsIndex=s,this.featureAdapter=i;const a=t.outFields;if(a&&!a.includes("*")){this.outFields=a;let t=0;for(const i of a){const a=e.getExpressionFromFieldName(i),n=this.fieldsIndex.get(a),r=n?null:e.getWhereClause(a,s),u=n?n.name:e.getAliasFromFieldName(i)||"FIELD_EXP_"+t++;this._fieldDataCache.set(i,{alias:u,clause:r})}}}var a=s.prototype;return a.countDistinctValues=function(t){return this.returnDistinctValues?(t.forEach((t=>this.getAttributes(t))),this._returnDistinctMap.size):t.length},a.getAttributes=function(t){const e=this._processAttributesForOutFields(t);return this._processAttributesForDistinctValues(e)},a.getFieldValue=function(t,i,s){const a=s?s.name:i;let n=null;return this._fieldDataCache.has(a)?n=this._fieldDataCache.get(a).clause:s||(n=e.getWhereClause(i,this.fieldsIndex),this._fieldDataCache.set(a,{alias:a,clause:n})),s?this.featureAdapter.getAttribute(t,a):n.calculateValue(t,this.featureAdapter)},a.getNormalizedValue=function(t,e){const s=e.normalizationType,a=e.normalizationTotal;let n=this.getFieldValue(t,e.field,e.fieldInfo);if(s&&Number.isFinite(n)){const r=this.getFieldValue(t,e.normalizationField,e.normalizationFieldInfo);n=i.getNormalizedValue(n,s,r,a)}return n},a.getExpressionValue=function(t,e,i,s){const a={attributes:this.featureAdapter.getAttributes(t),layer:{fields:this.fieldsIndex.fields}},n=s.createExecContext(a,i);return s.executeFunction(e,n)},a.getExpressionValues=function(t,e,i,s){const a={fields:this.fieldsIndex.fields};return t.map((t=>{const n={attributes:this.featureAdapter.getAttributes(t),layer:a},r=s.createExecContext(n,i);return s.executeFunction(e,r)}))},a.validateItem=function(t,i){return this._fieldDataCache.has(i)||this._fieldDataCache.set(i,{alias:i,clause:e.getWhereClause(i,this.fieldsIndex)}),this._fieldDataCache.get(i).clause.testFeature(t,this.featureAdapter)},a.validateItems=function(t,i){return this._fieldDataCache.has(i)||this._fieldDataCache.set(i,{alias:i,clause:e.getWhereClause(i,this.fieldsIndex)}),this._fieldDataCache.get(i).clause.testSet(t,this.featureAdapter)},a._processAttributesForOutFields=function(t){const e=this.outFields;if(!e||!e.length)return this.featureAdapter.getAttributes(t);const i={};for(const s of e){const{alias:e,clause:a}=this._fieldDataCache.get(s);i[e]=a?a.calculateValue(t,this.featureAdapter):this.featureAdapter.getAttribute(t,e)}return i},a._processAttributesForDistinctValues=function(e){if(t.isNone(e)||!this.returnDistinctValues)return e;const i=this.outFields,s=[];if(i)for(const t of i){const{alias:i}=this._fieldDataCache.get(t);s.push(e[i])}else for(const t in e)s.push(e[t]);const a=`${(i||["*"]).join(",")}=${s.join(",")}`;let n=this._returnDistinctMap.get(a)||0;return this._returnDistinctMap.set(a,++n),n>1?null:e},s}()}));
