/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{timeOnlyToMilliseconds as t}from"../../../core/timeUtils.js";import{getExpressionFromFieldName as e,getWhereClause as i,getAliasFromFieldName as s}from"./attributeSupport.js";import{getGeometry as a}from"./geometryUtils.js";import{isDateOnlyField as l,isTimestampOffsetField as r,isTimeOnlyField as n}from"../../support/fieldUtils.js";import{processNullValue as u,getNormalizedValue as o}from"../../../statistics/utils.js";import{loadArcade as d}from"../../../support/loadArcade.js";class c{constructor(t,a,l){this._fieldDataCache=new Map,this._returnDistinctMap=new Map,this.returnDistinctValues=t.returnDistinctValues??!1,this.fieldsIndex=l,this.featureAdapter=a;const r=t.outFields;if(r&&!r.includes("*")){this.outFields=r;let t=0;for(const a of r){const r=e(a),n=this.fieldsIndex.get(r),u=n?null:i(r,l),o=n?n.name:s(a)||"FIELD_EXP_"+t++;this._fieldDataCache.set(a,{alias:o,clause:u})}}}countDistinctValues(t){return this.returnDistinctValues?(t.forEach(t=>this.getAttributes(t)),this._returnDistinctMap.size):t.length}getAttributes(t){const e=this._processAttributesForOutFields(t);return this._processAttributesForDistinctValues(e)}getFieldValue(t,e,s){if(s)return this.featureAdapter.getAttribute(t,s.name);const a=e;let l=null;return this._fieldDataCache.has(a)?l=this._fieldDataCache.get(a)?.clause:s||(l=i(e,this.fieldsIndex),this._fieldDataCache.set(a,{alias:a,clause:l})),l?.calculateValue(t,this.featureAdapter)}getDataValues(e,i,s=!0){const a=i.normalizationType,d=i.normalizationTotal,c=this.fieldsIndex.get(i.field),f=l(c)||r(c),h=n(c);return e.map(e=>{let l=i.field&&this.getFieldValue(e,i.field,this.fieldsIndex.get(i.field));if(i.field2?(l=`${u(l)}${i.fieldDelimiter}${u(this.getFieldValue(e,i.field2,this.fieldsIndex.get(i.field2)))}`,i.field3&&(l=`${l}${i.fieldDelimiter}${u(this.getFieldValue(e,i.field3,this.fieldsIndex.get(i.field3)))}`)):"string"==typeof l&&s&&(f?l=l?new Date(l).getTime():null:h&&(l=l?t(l):null)),a&&Number.isFinite(l)){const t="field"===a&&i.normalizationField?this.getFieldValue(e,i.normalizationField,this.fieldsIndex.get(i.normalizationField)):null;l=o(l,a,t,d)}return l})}async getExpressionValues(t,e,i,s,l){const{arcadeUtils:r}=await d(),n=r.hasGeometryOperations(e);n&&await r.enableGeometryOperations();const u=r.createFunction(e),o=r.getViewInfo(i),c={fields:this.fieldsIndex.fields};return t.map(t=>{const e={attributes:this.featureAdapter.getAttributes(t),layer:c,geometry:n?{...a(s.geometryType,this.featureAdapter.getGeometry(t)),spatialReference:i.spatialReference}:null},d=r.createExecContext(e,o,l);return r.executeFunction(u,d)})}validateItem(t,e){return this._fieldDataCache.has(e)||this._fieldDataCache.set(e,{alias:e,clause:i(e,this.fieldsIndex)}),this._fieldDataCache.get(e)?.clause?.testFeature(t,this.featureAdapter)??!1}validateItems(t,e){return this._fieldDataCache.has(e)||this._fieldDataCache.set(e,{alias:e,clause:i(e,this.fieldsIndex)}),this._fieldDataCache.get(e)?.clause?.testSet(t,this.featureAdapter)??!1}_processAttributesForOutFields(t){const e=this.outFields;if(!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}_processAttributesForDistinctValues(t){if(null==t||!this.returnDistinctValues)return t;const e=this.outFields,i=[];if(e)for(const l of e){const{alias:e}=this._fieldDataCache.get(l);i.push(t[e])}else for(const l in t)i.push(t[l]);const s=`${(e||["*"]).join(",")}=${i.join(",")}`;let a=this._returnDistinctMap.get(s)||0;return this._returnDistinctMap.set(s,++a),a>1?null:t}}export{c as default};
