/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{ArcadeDate as t}from"./ArcadeDate.js";import{deepClone as e}from"./deepClone.js";import{ArcadeExecutionError as s}from"./executionError.js";import{i as r,c as i,a as n,b as o,d as a,e as u,f as l,g as c,h as b}from"../chunks/languageUtils.js";import h from"../geometry/Geometry.js";import{isNumber as f,isBoolean as m,isString as d,isArray as y}from"../support/guards.js";function w(t,e,s=!1,r=!1){if(null==t)return null;if(f(t)||m(t)||d(t)||u(t)||l(t)||c(t))return t;if(y(t)){const i=[];for(const n of t)i.push(w(n,e,s,r));return i}if(r&&b(t))return t;const i=new p;i.immutable=!1;for(const[n,o]of Object.entries(t))void 0!==o&&i.setField(n,w(o,e,s,r));return i.immutable=s,i}class p{constructor(t){this.declaredClass="esri.arcade.Dictionary",this.plain=!1,this.immutable=!0,t instanceof p?this.attributes=t.attributes:null==t?this.attributes=Object.create(null):null!=Object.getPrototypeOf(t)?this.attributes={__proto__:null,...t}:this.attributes=t}static containerEntry(t,e){return new p({__proto__:null,key:t,value:e})}static textFormatting(){const t=new p({__proto__:null,newline:"\n",tab:"\t",singlequote:"'",doublequote:'"',forwardslash:"/",backwardslash:"\\"});return t.immutable=!1,t}field(t){const e=t.toLowerCase(),r=this.attributes[t];if(void 0!==r)return r;for(const s in this.attributes)if(s.toLowerCase()===e)return this.attributes[s];throw new s(null,"FieldNotFound",null,{key:t})}setField(e,i){if(this.immutable)throw new s(null,"Immutable",null);if(r(i))throw new s(null,"NoFunctionInDictionary",null);const n=e.toLowerCase();i instanceof Date&&(i=t.dateJSToArcadeDate(i));if(void 0===this.attributes[e]){for(const t in this.attributes)if(t.toLowerCase()===n)return void(this.attributes[t]=i);this.attributes[e]=i}else this.attributes[e]=i}eraseField(t){if(this.immutable)throw new s(null,"Immutable",null);if(t in this.attributes)return void delete this.attributes[t];const e=t.toLowerCase();for(const s in this.attributes)if(s.toLowerCase()===e)return void delete this.attributes[s]}hasField(t){const e=t.toLowerCase();if(void 0!==this.attributes[t])return!0;for(const s in this.attributes)if(s.toLowerCase()===e)return!0;return!1}keys(){let t=[];for(const e in this.attributes)t.push(e);return t=t.sort(),t}values(){return this.keys().map(t=>this.attributes[t])}entryCount(){return Object.keys(this.attributes).length}isEmpty(){return this.entryCount()<=0}castToText(t=!1){return i(this.attributes,{useNumbersForDates:t})}static convertObjectToArcadeDictionary(t,e,s=!0,r=!1){const i=new p;i.immutable=!1;for(const n in t){const o=t[n];void 0!==o&&i.setField(n.toString(),w(o,e,s,r))}return i.immutable=s,i}static convertJsonToArcade(t,e,s=!1,r=!1){return w(t,e,s,r)}castAsJson(t=null){const e=Object.create(null);for(let s in this.attributes){const r=this.attributes[s];void 0!==r&&(t?.keyTranslate&&(s=t.keyTranslate(s)),e[s]=n(r,t))}return e}async castDictionaryValueAsJsonAsync(t,e,s,r=null,i){const n=await o(s,r,i);return t[e]=n,n}async castAsJsonAsync(e=null,s=null){const r=Object.create(null),i=[];for(let o in this.attributes){const u=this.attributes[o];s?.keyTranslate&&(o=s.keyTranslate(o)),void 0!==u&&(a(u)||u instanceof h||u instanceof t?r[o]=n(u,s):i.push(this.castDictionaryValueAsJsonAsync(r,o,u,e,s)))}return i.length>0&&await Promise.all(i),r}deepClone(){const t=new p;t.immutable=!1;for(const s of this.keys())t.setField(s,e(this.field(s)));return t}}export{p as default};
