/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../core/Error.js";import r from"../../core/Logger.js";import i from"../../core/Version.js";import t from"../../layers/support/FieldsIndex.js";import{createDictionaryExpression as o,ArcadeFeatureReader as n}from"../../support/ArcadeExpression.js";class s{static async from(t,n,c){const l=t.dictionary_version?i.parse(t.dictionary_version):null,d=new Set(t.itemsNames),u={};if(n)for(const e in n)u[e]=n[e];if(t.authoringInfo.configuration)for(const e of t.authoringInfo.configuration)u.hasOwnProperty(e.name)||(u[e.name]=e.value);const p=new Set(t.authoringInfo.symbol);for(const e of Object.keys(c))p.delete(e);p.size&&r.getLogger("esri.renderers.support.DictionaryScriptEvaluator").warnOnce("missing-fields: fieldMap entries for the following symbol fields are missing",{symbolFields:p});const f=await o(t.expression,null,u);if(!f)throw new e("dictionary-renderer:expression-error","Unable to create dictionary renderer expression");const g=!l||!l.greaterEqual(4,0);g&&r.getLogger("esri.renderers.support.DictionaryScriptEvaluator").warnOnce("Dictionary script does not support native field types. Applying fallback",{version:l});const m=new a(c,g);return new s(g,d,f,m)}constructor(e,r,i,t){this._requiresFieldCoercionToString=e,this._itemNames=r,this._compiled=i,this._reader=t}get itemNames(){return this._itemNames}evaluate(e,i,t,o){try{return this._reader.bind(e,t,o),this._compiled.evaluate(this._reader,{$view:{scale:i}})}catch(n){r.getLogger("esri.renderers.support.DictionaryScriptEvaluator").warnOnce("arcade: dictionary script evaluation failed",{error:n})}return null}createDictionaryFieldsIndex(e){if(!this._requiresFieldCoercionToString)return new t(e);const r=e.map(e=>({...e,type:"esriFieldTypeString"}));return new t(r)}}class a extends n{constructor(e,r){super(),this._fieldMap=e,this._requiresFieldCoercionToString=r}_getField(e){const r=this._fieldMap[e]??e;return this._boundSchema.fieldsIndex.get(r)}field(e){if(!this._requiresFieldCoercionToString)return super.field(e,!1);const r=this._getField(e);if(null==r)return"";return null==this._boundTarget.attributes[r.name]?"":""+this._boundTarget.attributes[r.name]}}export{s as DictionaryScriptEvaluator};
