/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","./chunks/_rollupPluginBabelHelpers","./Graphic","./arcade/Dictionary","./arcade/Feature","./arcade/ImmutableArray","./arcade/parser","./core/Error","./layers/FeatureLayer","./rest/support/FeatureSet","./support/arcadeOnDemand"],(function(e,t,r,a,n,c,s,o,i,u,l){"use strict";function p(e,t,r,a={}){const n=t.elementType,s="value",o="array"===n.type?[{name:s,type:n.type,elementType:n.elementType}]:"dictionary"===n.type?[{name:s,type:n.type,properties:n.properties}]:[{name:s,type:n.type}];return new c(e.map((e=>{const t={};return m(t,o,{[s]:e},r,a),t[s]})))}function y(e,t,r={}){const a=e instanceof u?new i({source:e.features,geometryType:e.geometryType,fields:e.fields,spatialReference:e.spatialReference}):e;return t.constructFeatureSet(a,r.spatialReference,null,!0,r.lruCache,r.interceptor)}function f(e,t,r={}){const{spatialReference:a,interceptor:n,lruCache:c}=r;return"string"==typeof e?t.createFeatureSetCollectionFromService(e,a,c,n):t.createFeatureSetCollectionFromMap(e,a,c,n)}function d(e,t,r,n={}){const c={};return m(c,t.properties,e,r,n),new a(c)}function m(e,t,r,a,c={}){const s={};for(const n of Object.keys(r))s[n.toLowerCase()]=r[n];for(const o of t){const t=o.name.toLowerCase();if(c.variablesPreProcessed)e[t]=s[t];else switch(o.type){case"array":{const r=s[t];e[t]=p(r,o,a,c);break}case"feature":{const r=s[t];e[t]=null==r?null:n.createFromGraphic(r);break}case"featureSet":{const r=s[t];e[t]=a?y(r,a,c):null;break}case"featureSetCollection":{const r=s[t];e[t]=a?f(r,a,c):null;break}case"dictionary":{const r=s[t];e[t]=d(r,o,a,c);break}case"geometry":case"boolean":case"date":case"text":case"number":e[t]=s[t]}}}function b(e,t){for(const r of e)t.push(r),"dictionary"===r.type&&b(r.properties,t);return t}function h(e,t,r,a,n){const{spatialReference:c,interceptor:s,lruCache:o,services:i,console:u}=r,l={vars:{},spatialReference:c,interceptor:s,lrucache:o,useAsync:n,services:i,console:u};return t?(m(l.vars,e.variables,t,a,r),l):l}function v(e,t){switch(t.getArcadeType(e)){case"number":case"text":case"boolean":case"point":case"polygon":case"polyline":case"multipoint":case"extent":return e;case"date":return new Date(e.getTime());case"feature":{const t=(e.type,e),a="geometry"in t?t.geometry():null,n="readAttributes"in t?t.readAttributes():t.attributes;return new r({geometry:a,attributes:n})}case"dictionary":{const r=e,a=r.attributes,n={};for(const e of Object.keys(a))n[e]=v(r.field(e),t);return n}case"array":return("toArray"in e?e.toArray():e).map((e=>v(e,t)))}return e}function w(e,t){return S.apply(this,arguments)}function S(){return S=t._asyncToGenerator((function*(e,r,a={}){const{arcade:n,arcadeUtils:c}=yield l.loadArcade(),{loadScriptDependencies:i,referencesMember:u,scriptIsAsync:p}=n,y=b(r.variables,[]),f=y.filter((e=>"featureSet"===e.type||"featureSetCollection"===e.type)).map((e=>e.name.toLowerCase())),d=s.parseScript(e,f);if(!d)throw new o("arcade:invalid-script","Unable to create SyntaxTree");const m=c.extractFieldNames(d),w=y.map((e=>e.name.toLowerCase())).filter((e=>u(d,e))),S=p(d,f);yield i(d,S,f);const C={vars:{},spatialReference:null,useAsync:S};for(const t of w)C.vars[t]="any";const{lruCache:g}=a,A=n.compileScript(d,C),x=n.featureSetUtils(),T=(e,t={})=>{if(S)throw new o("arcade:invalid-execution-mode","Cannot execute the script in synchronous mode");const a=A(h(r,e,{lruCache:g,...t},x,S));return t.rawOutput?a:v(a,c)},F=function(){var e=t._asyncToGenerator((function*(e,t={}){const a=yield A(h(r,e,{lruCache:g,...t},x,S));return t.rawOutput?a:v(a,c)}));return function(t){return e.apply(this,arguments)}}();return{execute:T,executeAsync:F,isAsync:S,variablesUsed:w,fieldsUsed:m,syntaxTree:d}})),S.apply(this,arguments)}e.createArcadeExecutor=w,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
