/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{ArcadeModule as e}from"./ArcadeModule.js";import{ArcadeModuleLoader as t}from"./ArcadeModuleLoader.js";import n from"./Dictionary.js";import{ArcadeExecutionError as r,ExecutionErrorCodes as o,ensureArcadeExecutionError as a}from"./executionError.js";import i from"./Feature.js";import{NativeFunction as l,ArcadeFunction as s,ScopeMarshalledFunction as c,wrapModuleScopedResponse as u}from"./FunctionWrapper.js";import{y as f,a as p,s as w,p as d,v as h,o as m,i as g,c as y,R as v,m as b,x as S,w as x,T as I,b as F,g as C,I as R,j as A,r as M,u as j,q as L}from"../chunks/languageUtils.js";import{addFunctionDeclaration as k}from"./treeAnalysis.js";import{A as N}from"../chunks/array.js";import{registerFunctions as O}from"./functions/date.js";import{registerFunctions as E}from"./functions/geomasync.js";import{registerFunctions as B,geometryMember as P}from"./functions/geometry.js";import{registerFunctions as K}from"./functions/maths.js";import{registerFunctions as D}from"./functions/stats.js";import{registerFunctions as G}from"./functions/string.js";import T from"../geometry/Geometry.js";import U from"../geometry/SpatialReference.js";function q(e){return e&&"function"==typeof e.then}const W=100;async function V(e,t){const n=[];for(let r=0;r{const n={spatialReference:this.context.spatialReference,console:this.context.console,lrucache:this.context.lrucache,exports:this.context.exports,libraryResolver:this.context.libraryResolver,interceptor:this.context.interceptor,localScope:{},depthCounter:{depth:e.depthCounter+1},globalScope:this.context.globalScope};if(n.depthCounter.depth>64)throw new r(e,o.MaximumCallDepth,null);return Pe(this.definition,n,t,null)}}call(e,t){return H(e,t,((n,a,i)=>{const l={spatialReference:e.spatialReference,services:e.services,console:e.console,libraryResolver:e.libraryResolver,exports:e.exports,lrucache:e.lrucache,interceptor:e.interceptor,localScope:{},abortSignal:e.abortSignal,globalScope:e.globalScope,depthCounter:{depth:e.depthCounter.depth+1}};if(l.depthCounter.depth>64)throw new r(e,o.MaximumCallDepth,t);return Pe(this.definition,l,i,t)}))}marshalledCall(e,t,n,r){return r(e,t,(async(o,a,i)=>{const l={spatialReference:e.spatialReference,globalScope:n.globalScope,depthCounter:{depth:e.depthCounter.depth+1},libraryResolver:e.libraryResolver,exports:e.exports,console:e.console,abortSignal:e.abortSignal,lrucache:e.lrucache,interceptor:e.interceptor,localScope:{}};return i=i.map((t=>!g(t)||t instanceof c?t:u(t,e,r))),u(await Pe(this.definition,l,i,t),n,r)}))}}class z extends e{constructor(e){super(e)}async global(e){const t=this.executingContext.globalScope[e.toLowerCase()];if(t.valueset||(t.value=await J(this.executingContext,t.node),t.valueset=!0),g(t.value)&&!(t.value instanceof c)){const e=new c;e.fn=t.value,e.parameterEvaluator=H,e.context=this.executingContext,t.value=e}return t.value}setGlobal(e,t){if(g(t))throw new r(null,o.AssignModuleFunction,null);this.executingContext.globalScope[e.toLowerCase()]={value:t,valueset:!0,node:null}}hasGlobal(e){return void 0===this.executingContext.exports[e]&&(e=e.toLowerCase()),void 0!==this.executingContext.exports[e]}async loadModule(e){let n=e.spatialReference;null==n&&(n=new U({wkid:102100})),this.moduleScope=Ge({},e.customfunctions),this.executingContext={spatialReference:n,services:e.services,libraryResolver:new t(e.libraryResolver._moduleSingletons,this.source.syntax.loadedModules),exports:{},abortSignal:void 0===e.abortSignal||null===e.abortSignal?{aborted:!1}:e.abortSignal,globalScope:this.moduleScope,console:e.console?e.console:Te,lrucache:e.lrucache,interceptor:e.interceptor,localScope:null,depthCounter:{depth:1}},await J(this.executingContext,this.source.syntax)}}async function H(e,t,n){if(!0===t.preparsed){const r=n(e,null,t.arguments);return q(r),r}const r=n(e,t,await V(e,t));return q(r),r}async function J(e,t,n){if(t.breakpoint&&!0!==n){const n=t.breakpoint();return await n,J(e,t,!0)}try{switch(t?.type){case"VariableDeclarator":return await xe(e,t);case"ImportDeclaration":return await ve(e,t);case"ExportNamedDeclaration":return await be(e,t);case"VariableDeclaration":return await Se(e,t,0);case"BlockStatement":case"Program":return await he(e,t);case"FunctionDeclaration":return await ye(e,t);case"ReturnStatement":return await ge(e,t);case"IfStatement":return await de(e,t);case"ExpressionStatement":return await we(e,t);case"UpdateExpression":return await ue(e,t);case"AssignmentExpression":return await pe(e,t);case"ForStatement":return await ne(e,t);case"WhileStatement":return await Z(e,t);case"ForInStatement":return await ce(e,t);case"BreakStatement":return m;case"EmptyStatement":return h;case"ContinueStatement":return d;case"TemplateElement":return await Le(e,t);case"TemplateLiteral":return await Ne(e,t);case"Identifier":return await Me(e,t);case"MemberExpression":return await Ie(e,t);case"Literal":return t.value;case"CallExpression":return await je(e,t);case"UnaryExpression":return await Fe(e,t);case"BinaryExpression":return await Re(e,t);case"LogicalExpression":return await Ae(e,t);case"ArrayExpression":return await Ce(e,t);case"ObjectExpression":return await Q(e,t);case"Property":return await X(e,t);default:throw new r(e,o.Unrecognised,t)}}catch(i){throw a(e,t,i)}}async function Q(e,t){const a=[];for(let n=0;n{try{!0===n.testResult?++a>W?(a=0,setTimeout((()=>{te(e,t,n,r,o,a)}),0)):te(e,t,n,r,o,a):n.lastAction instanceof v?r(n.lastAction):r(h)}catch(i){o(i)}}),(e=>{o(e)}))}catch(i){o(i)}}function ne(e,t){try{return null!==t.init?J(e,t.init).then((()=>new Promise(((n,r)=>{te(e,t,{testResult:!0,lastAction:h},(e=>{n(e)}),(e=>{r(e)}),0)})))):new Promise(((n,r)=>{te(e,t,{testResult:!0,lastAction:h},(e=>{n(e)}),(e=>{r(e)}),0)}))}catch(n){return Promise.reject(n)}}function re(e,t,n,r,o,a,i,l,s,c){try{if(r<=a)return void l(h);o.value="k"===i?n[a]:a,J(e,t.body).then((u=>{try{u instanceof v?l(u):u===m?l(h):++c>W?(c=0,setTimeout((()=>{re(e,t,n,r,o,a+1,i,l,s,c)}),0)):re(e,t,n,r,o,a+1,i,l,s,c)}catch(f){s(f)}}),(e=>{s(e)}))}catch(u){s(u)}}function oe(e,t,n,r,o,a,i,l,s){try{if(n.length()<=o)return void i(h);r.value="k"===a?n.get(o):o,J(e,t.body).then((c=>{c instanceof v?i(c):c===m?i(h):++s>W?(s=0,setTimeout((()=>{oe(e,t,n,r,o+1,a,i,l,s)}),0)):oe(e,t,n,r,o+1,a,i,l,s)}),(e=>{l(e)}))}catch(c){l(c)}}function ae(e,t,n,r,o,a){try{if(void 0===a&&(a="i"),0===n.length)return void r.resolve(h);re(e,t,n,n.length,o,0,a,(e=>{r.resolve(e)}),(e=>{r.reject(e)}),0)}catch(i){r.reject(i)}}function ie(e,t,n,r,o,a){try{if(void 0===a&&(a="i"),0===n.length)return void r.resolve(h);oe(e,t,n,o,0,a,(e=>{r.resolve(e)}),(e=>{r.reject(e)}),0)}catch(i){r.reject(i)}}function le(e,t,n,r,o){try{ae(e,t,n.keys(),r,o,"k")}catch(a){r.reject(a)}}function se(e,t,n,r,o,a,l,s){try{e.next().then((c=>{try{if(null===c)a(h);else{const u=i.createFromGraphicLikeObject(c.geometry,c.attributes,r);u._underlyingGraphic=c,o.value=u;J(t,n.body).then((i=>{try{i===m?a(h):i instanceof v?a(i):++s>W?(s=0,setTimeout((()=>{se(e,t,n,r,o,a,l,s)}),0)):se(e,t,n,r,o,a,l,s)}catch(c){l(c)}}),(e=>{l(e)}))}}catch(u){l(u)}}),(e=>{l(e)}))}catch(c){l(c)}}async function ce(e,t){return new Promise(((a,i)=>{J(e,t.right).then((l=>{try{let s=null;s="VariableDeclaration"===t.left.type?J(e,t.left):Promise.resolve(),s.then((()=>{try{let s="";if("VariableDeclaration"===t.left.type){const e=t.left.declarations[0].id;"Identifier"===e.type&&(s=e.name)}else"Identifier"===t.left.type&&(s=t.left.name);if(!s)throw new r(e,o.InvalidIdentifier,t);s=s.toLowerCase();let c=null;if(null!=e.localScope&&void 0!==e.localScope[s]&&(c=e.localScope[s]),null===c&&void 0!==e.globalScope[s]&&(c=e.globalScope[s]),null===c)return void i(new r(e,o.InvalidIdentifier,t));b(l)||y(l)?ae(e,t,l,{reject:i,resolve:a},c):S(l)?ie(e,t,l,{reject:i,resolve:a},c):l instanceof n||x(l)?le(e,t,l,{reject:i,resolve:a},c):I(l)?se(l.iterator(e.abortSignal),e,t,l,c,(e=>{a(e)}),(e=>{i(e)}),0):ae(e,t,[],{reject:i,resolve:a},c)}catch(s){i(s)}}),i)}catch(s){i(s)}}),i)}))}async function ue(e,t){const a=t.argument;if("MemberExpression"===a.type){const i={t:null},l=await J(e,a.object);let s=null;i.t=l,!0===a.computed?s=await J(e,a.property):"Identifier"===a.property.type&&(s=a.property.name);const c=i.t;let u;if(b(c)){if(!F(s))throw new r(e,o.ArrayAccessorMustBeNumber,t);if(s<0&&(s=c.length+s),s<0||s>=c.length)throw new r(e,o.OutOfBounds,t);u=C(c[s]),c[s]="++"===t.operator?u+1:u-1}else if(c instanceof n){if(!1===y(s))throw new r(e,o.KeyAccessorMustBeString,t);if(!0!==c.hasField(s))throw new r(e,o.FieldNotFound,t,{key:s});u=C(c.field(s)),c.setField(s,"++"===t.operator?u+1:u-1)}else if(c instanceof z){if(!1===y(s))throw new r(e,o.ModuleAccessorMustBeString,t);if(!0!==c.hasGlobal(s))throw new r(e,o.ModuleExportNotFound,t);u=C(await c.global(s)),c.setGlobal(s,"++"===t.operator?u+1:u-1)}else{if(!x(c))throw S(c)?new r(e,o.Immutable,t):new r(e,o.InvalidParameter,t);if(!1===y(s))throw new r(e,o.KeyAccessorMustBeString,t);if(!0!==c.hasField(s))throw new r(e,o.FieldNotFound,t,{key:s});u=C(c.field(s)),c.setField(s,"++"===t.operator?u+1:u-1)}return!1===t.prefix?u:"++"===t.operator?u+1:u-1}const i="Identifier"===t.argument.type?t.argument.name.toLowerCase():"";if(!i)throw new r(e,o.InvalidIdentifier,t);let l;if(null!=e.localScope&&void 0!==e.localScope[i])return l=C(e.localScope[i].value),e.localScope[i]={value:"++"===t.operator?l+1:l-1,valueset:!0,node:t},!1===t.prefix?l:"++"===t.operator?l+1:l-1;if(void 0!==e.globalScope[i])return l=C(e.globalScope[i].value),e.globalScope[i]={value:"++"===t.operator?l+1:l-1,valueset:!0,node:t},!1===t.prefix?l:"++"===t.operator?l+1:l-1;throw new r(e,o.InvalidIdentifier,t)}function fe(e,t,n,a,i){switch(t){case"=":return e===h?null:e;case"/=":return C(n)/C(e);case"*=":return C(n)*C(e);case"-=":return C(n)-C(e);case"+=":return y(n)||y(e)?A(n)+A(e):C(n)+C(e);case"%=":return C(n)%C(e);default:throw new r(i,o.UnsupportedOperator,a)}}async function pe(e,t){const a=t.left;if("MemberExpression"===a.type){const i=await J(e,t.right),l=await J(e,a.object);let s=null;if(!0===a.computed)s=await J(e,a.property);else{if("Identifier"!==a.property.type)throw new r(e,o.InvalidIdentifier,t);s=a.property.name}if(b(l)){if(!F(s))throw new r(e,o.ArrayAccessorMustBeNumber,t);if(s<0&&(s=l.length+s),s<0||s>l.length)throw new r(e,o.OutOfBounds,t);if(s===l.length){if("="!==t.operator)throw new r(e,o.OutOfBounds,t);l[s]=fe(i,t.operator,l[s],t,e)}else l[s]=fe(i,t.operator,l[s],t,e)}else if(l instanceof n){if(!1===y(s))throw new r(e,o.KeyAccessorMustBeString,t);if(!0===l.hasField(s))l.setField(s,fe(i,t.operator,l.field(s),t,e));else{if("="!==t.operator)throw new r(e,o.FieldNotFound,t,{key:s});l.setField(s,fe(i,t.operator,null,t,e))}}else if(l instanceof z){if(!1===y(s))throw new r(e,o.KeyAccessorMustBeString,t);if(!0!==l.hasGlobal(s))throw new r(e,o.ModuleExportNotFound,t);l.setGlobal(s,fe(i,t.operator,await l.global(s),t,e))}else{if(!x(l))throw S(l)?new r(e,o.Immutable,t):new r(e,o.InvalidParameter,t);if(!1===y(s))throw new r(e,o.KeyAccessorMustBeString,t);if(!0===l.hasField(s))l.setField(s,fe(i,t.operator,l.field(s),t,e));else{if("="!==t.operator)throw new r(e,o.FieldNotFound,t,{key:s});l.setField(s,fe(i,t.operator,null,t,e))}}return h}const i=a.name.toLowerCase();if(null!=e.localScope&&void 0!==e.localScope[i]){const n=await J(e,t.right);return e.localScope[i]={value:fe(n,t.operator,e.localScope[i].value,t,e),valueset:!0,node:t.right},h}if(void 0!==e.globalScope[i]){const n=await J(e,t.right);return e.globalScope[i]={value:fe(n,t.operator,e.globalScope[i].value,t,e),valueset:!0,node:t.right},h}throw new r(e,o.InvalidIdentifier,t)}async function we(e,t){if("AssignmentExpression"===t.expression.type)return J(e,t.expression);if("CallExpression"===t.expression.type){const n=await J(e,t.expression);return n===h?h:new R(n)}const n=await J(e,t.expression);return n===h?h:new R(n)}async function de(e,t){const n=await J(e,t.test);if(!0===n)return J(e,t.consequent);if(!1===n)return null!==t.alternate?J(e,t.alternate):h;throw new r(e,o.BooleanConditionRequired,t)}async function he(e,t){return me(e,t,0)}async function me(e,t,n){if(n>=t.body.length)return h;const r=await J(e,t.body[n]);return r instanceof v||r===m||r===d||n===t.body.length-1?r:me(e,t,n+1)}async function ge(e,t){if(null===t.argument)return new v(h);const n=await J(e,t.argument);return new v(n)}async function ye(e,t){const n=t.id.name.toLowerCase();return e.globalScope[n]={valueset:!0,node:null,value:new Y(t,e)},h}async function ve(e,t){const n=t.specifiers[0].local.name.toLowerCase(),r=e.libraryResolver.loadLibrary(n);let o=null;return e.libraryResolver._moduleSingletons?.has(r.uri)?o=e.libraryResolver._moduleSingletons.get(r.uri):(o=new z(r),await o.loadModule(e),e.libraryResolver._moduleSingletons?.set(r.uri,o)),e.globalScope[n]={value:o,valueset:!0,node:t},h}async function be(e,t){if(await J(e,t.declaration),"FunctionDeclaration"===t.declaration.type)e.exports[t.declaration.id.name.toLowerCase()]="function";else if("VariableDeclaration"===t.declaration.type)for(const n of t.declaration.declarations)e.exports[n.id.name.toLowerCase()]="variable";return h}async function Se(e,t,n){return n>=t.declarations.length?h:(await J(e,t.declarations[n]),n===t.declarations.length-1||await Se(e,t,n+1),h)}async function xe(e,t){let n=null;if(n=null===t.init?null:await J(e,t.init),null!==e.localScope){if(n===h&&(n=null),"Identifier"!==t.id.type)throw new r(e,o.InvalidIdentifier,t);const a=t.id.name.toLowerCase();return null!=e.localScope&&(e.localScope[a]={value:n,valueset:!0,node:t.init}),h}if("Identifier"!==t.id.type)throw new r(e,o.InvalidIdentifier,t);const a=t.id.name.toLowerCase();return n===h&&(n=null),e.globalScope[a]={value:n,valueset:!0,node:t.init},h}async function Ie(e,t){const a=await J(e,t.object);if(null===a)throw new r(e,o.MemberOfNull,t);if(!1===t.computed){if("Identifier"===t.property.type){if(a instanceof n||x(a))return a.field(t.property.name);if(a instanceof T)return P(a,t.property.name,e,t);if(a instanceof z){if(!a.hasGlobal(t.property.name))throw new r(e,o.InvalidIdentifier,t);return a.global(t.property.name)}throw new r(e,o.InvalidMemberAccessKey,t)}throw new r(e,o.InvalidMemberAccessKey,t)}let i=await J(e,t.property);if(a instanceof n||x(a)){if(y(i))return a.field(i);throw new r(e,o.InvalidMemberAccessKey,t)}if(a instanceof z){if(y(i))return a.global(i);throw new r(e,o.InvalidMemberAccessKey,t)}if(a instanceof T){if(y(i))return P(a,i,e,t);throw new r(e,o.InvalidMemberAccessKey,t)}if(b(a)){if(F(i)&&isFinite(i)&&Math.floor(i)===i){if(i<0&&(i=a.length+i),i>=a.length||i<0)throw new r(e,o.OutOfBounds,t);return a[i]}throw new r(e,o.InvalidMemberAccessKey,t)}if(S(a)){if(F(i)&&isFinite(i)&&Math.floor(i)===i){if(i<0&&(i=a.length()+i),i>=a.length()||i<0)throw new r(e,o.OutOfBounds,t);return a.get(i)}throw new r(e,o.InvalidMemberAccessKey,t)}if(y(a)){if(F(i)&&isFinite(i)&&Math.floor(i)===i){if(i<0&&(i=a.length+i),i>=a.length||i<0)throw new r(e,o.OutOfBounds,t);return a[i]}throw new r(e,o.InvalidMemberAccessKey,t)}throw new r(e,o.InvalidMemberAccessKey,t)}async function Fe(e,t){const n=await J(e,t.argument);if(p(n)){if("!"===t.operator)return!n;if("-"===t.operator)return-1*C(n);if("+"===t.operator)return 1*C(n);if("~"===t.operator)return~C(n);throw new r(e,o.UnsupportedUnaryOperator,t)}if("-"===t.operator)return-1*C(n);if("+"===t.operator)return 1*C(n);if("~"===t.operator)return~C(n);throw new r(e,o.UnsupportedUnaryOperator,t)}async function Ce(e,t){const n=[];for(let r=0;r>":case">>>":case"^":case"&":return j(C(a),C(i),t.operator);case"==":return w(a,i);case"!=":return!w(a,i);case"<":case">":case"<=":case">=":return M(a,i,t.operator);case"+":return y(a)||y(i)?A(a)+A(i):C(a)+C(i);case"-":return C(a)-C(i);case"*":return C(a)*C(i);case"/":return C(a)/C(i);case"%":return C(a)%C(i);default:throw new r(e,o.UnsupportedOperator,t)}}async function Ae(e,t){const n=await J(e,t.left);let a=null;if(!p(n))throw new r(e,o.LogicalExpressionOnlyBoolean,t);switch(t.operator){case"||":if(!0===n)return n;if(a=await J(e,t.right),p(a))return a;throw new r(e,o.LogicExpressionOrAnd,t);case"&&":if(!1===n)return n;if(a=await J(e,t.right),p(a))return a;throw new r(e,o.LogicExpressionOrAnd,t);default:throw new r(e,o.LogicExpressionOrAnd,t)}}async function Me(e,t){const n=t.name.toLowerCase();if(null!=e.localScope&&void 0!==e.localScope[n]){const t=e.localScope[n];if(!0===t.valueset)return t.value;if(null!==t.d)return t.d;t.d=J(e,t.node);const r=await t.d;return t.value=r,t.valueset=!0,r}if(void 0!==e.globalScope[n]){const t=e.globalScope[n];if(!0===t.valueset)return t.value;if(null!==t.d)return t.d;t.d=J(e,t.node);const r=await t.d;return t.value=r,t.valueset=!0,r}throw new r(e,o.InvalidIdentifier,t)}async function je(e,t){if("MemberExpression"===t.callee.type){const n=await J(e,t.callee.object);if(!(n instanceof z))throw new r(e,o.FuncionNotFound,t);const a=!1===t.callee.computed?t.callee.property.name:await J(e,t.callee.property);if(!n.hasGlobal(a))throw new r(e,o.FuncionNotFound,t);const i=await n.global(a);if(!g(i))throw new r(e,o.CallNonFunction,t);return i.call(e,t)}if("Identifier"!==t.callee.type)throw new r(e,o.FuncionNotFound,t);if(null!=e.localScope&&void 0!==e.localScope[t.callee.name.toLowerCase()]){const n=e.localScope[t.callee.name.toLowerCase()];if(g(n.value))return n.value.call(e,t);throw new r(e,o.CallNonFunction,t)}if(void 0!==e.globalScope[t.callee.name.toLowerCase()]){const n=e.globalScope[t.callee.name.toLowerCase()];if(g(n.value))return n.value.call(e,t);throw new r(e,o.CallNonFunction,t)}throw new r(e,o.FuncionNotFound,t)}async function Le(e,t){return t.value?t.value.cooked:""}function ke(e,t,n){if(g(e))throw new r(t,o.NoFunctionInTemplateLiteral,n);return e}async function Ne(e,t){const n=[];for(let a=0;a