/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import t from"../../request.js";import{clone as e}from"../../core/lang.js";import{LRUCache as r}from"../../core/LRUCache.js";import{getJsonType as o}from"../../geometry/support/jsonTypeUtils.js";import{DictionaryControlString as s}from"./DictionaryControlString.js";import{DictionaryScriptEvaluator as i}from"./DictionaryScriptEvaluator.js";import{deriveFields as a}from"../../support/ArcadeExpression.js";import l from"../../symbols/CIMSymbol.js";import{getTextCasing as n}from"../../symbols/cim/utils.js";const m={type:"CIMSimpleLineCallout",lineSymbol:{type:"CIMLineSymbol",symbolLayers:[{type:"CIMSolidStroke",enable:!0,width:.5,color:[0,0,0,255]}]}};class c{constructor(t,e,o,s){this.url=t,this.fieldMap=o,this.dictionaryInfo=s,this._symbolPartCache=new r(100);const a=async()=>i.from(await s,e,o);this._evaluator=a()}async getSymbolAsync(t,e,r=!0){const i=await this._evaluator,l=e?.fields??a(t.attributes),n=i.createDictionaryFieldsIndex(l),m=i.evaluate(t,e?.scale??0,n,e?.spatialReference);if(null==m)return null;const c=o(t.geometry),y=s.parse(m,i.itemNames,c),f="esriGeometryPoint"===c&&!t.geometry?.hasZ;return await this._cimPartsToCIMSymbol(t,y.partNames,y.overrides,f,e,r,n)}async getSymbolForControlString(t,e,r){const o=await this._evaluator,i=s.parse(t,o.itemNames,e),a="esriGeometryPoint"===e&&!r,n=y(await Promise.all(i.partNames.map(t=>this._getSymbolPart(t))),i.overrides,a);return new l({data:n})}async _cimPartsToCIMSymbol(t,r,o,s,i,a,m){const c=r.map(t=>this._getSymbolPart(t,i));let f=await Promise.all(c);if(a&&this.fieldMap&&f.length>0){const{OverrideHelper:r}=await import("../../symbols/cim/OverrideHelper.js");f=e(f);for(const e of f)r.applyDictionaryTextOverrides(e,t,this.fieldMap,m,n(e))}const p=y(f,o,s);return new l({data:p})}async _fetchSymbolPart(e,r){const o=await this.dictionaryInfo,s=(this.url+"/"+o.cimRefTemplateUrl).replaceAll(/\{itemName\}/gi,e),{data:i}=await t(s,{responseType:"json",query:{f:"json"},...r});return i}async _getSymbolPart(t,e){let r=this._symbolPartCache.get(t);return r||(r=this._fetchSymbolPart(t,e),this._symbolPartCache.put(t,r)),r}}function y(t,e,r){if(!t||0===t.length)return null;const o={...t[0]};if(t.length>1){o.effects=null,o.symbolLayers=[];for(const e of t){const t=e;if(null!=t.effects)for(const e of t.symbolLayers)null==e.effects?e.effects=t.effects:e.effects.unshift(...t.effects);o.symbolLayers.unshift(...t.symbolLayers)}}return r&&(o.callout=m),{type:"CIMSymbolReference",symbol:o,primitiveOverrides:e}}export{c as DictionaryLoader};
