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