/* 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 l}from"./executionError.js";import a from"./Feature.js";import{NativeFunction as i,ArcadeFunction as s,ScopeMarshalledFunction as u,wrapModuleScopedResponse as c}from"./FunctionWrapper.js";import{y as f,a as p,s as d,p as h,o as w,v as m,i as g,c as v,m as y,R as b,x,w as S,b as I,g as F,I as C,j as R,r as A,u as M,q as E}from"../chunks/languageUtils.js";import{addFunctionDeclaration as L}from"./treeAnalysis.js";import{A as N}from"../chunks/array.js";import{registerFunctions as O}from"./functions/date.js";import{registerFunctions as B,geometryMember as j}from"./functions/geometry.js";import{registerFunctions as k}from"./functions/geomsync.js";import{registerFunctions as D}from"./functions/maths.js";import{registerFunctions as K}from"./functions/stats.js";import{registerFunctions as U}from"./functions/string.js";import q from"../geometry/Geometry.js";import G from"../geometry/SpatialReference.js";class P extends s{constructor(e,t){super(),this.definition=null,this.context=null,this.definition=e,this.context=t}createFunction(e){return(...t)=>{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.depth+1},globalScope:this.context.globalScope};if(n.depthCounter.depth>64)throw new r(e,o.MaximumCallDepth,null);return Ie(this.definition,n,t,null)}}call(e,t){return V(e,t,((n,l,a)=>{const i={spatialReference:e.spatialReference,globalScope:e.globalScope,depthCounter:{depth:e.depthCounter.depth+1},libraryResolver:e.libraryResolver,exports:e.exports,console:e.console,lrucache:e.lrucache,interceptor:e.interceptor,localScope:{}};if(i.depthCounter.depth>64)throw new r(e,o.MaximumCallDepth,t);return Ie(this.definition,i,a,t)}))}marshalledCall(e,t,n,r){return r(e,t,((o,l,a)=>{const i={spatialReference:e.spatialReference,globalScope:n.globalScope,depthCounter:{depth:e.depthCounter.depth+1},libraryResolver:e.libraryResolver,exports:e.exports,console:e.console,lrucache:e.lrucache,interceptor:e.interceptor,localScope:{}};return a=a.map((t=>!g(t)||t instanceof u?t:c(t,e,r))),c(Ie(this.definition,i,a,t),n,r)}))}}class T extends e{constructor(e){super(e)}global(e){const t=this.executingContext.globalScope[e.toLowerCase()];if(t.valueset||(t.value=_(this.executingContext,t.node),t.valueset=!0),g(t.value)&&!(t.value instanceof u)){const e=new u;e.fn=t.value,e.parameterEvaluator=V,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]}loadModule(e){let n=e.spatialReference;null==n&&(n=new G({wkid:102100})),this.moduleScope=Ce({},e.customfunctions),this.executingContext={spatialReference:n,globalScope:this.moduleScope,localScope:null,libraryResolver:new t(e.libraryResolver._moduleSingletons,this.source.syntax.loadedModules),exports:{},console:e.console?e.console:Ae,lrucache:e.lrucache,interceptor:e.interceptor,depthCounter:{depth:1}},_(this.executingContext,this.source.syntax)}}function W(e,t){const n=[];for(let r=0;r=a.length)throw new r(e,o.OutOfBounds,t);l=F(a[i]),a[i]="++"===t.operator?l+1:l-1}else if(a instanceof n){if(!1===v(i))throw new r(e,o.KeyAccessorMustBeString,t);if(!0!==a.hasField(i))throw new r(e,o.FieldNotFound,t);l=F(a.field(i)),a.setField(i,"++"===t.operator?l+1:l-1)}else if(S(a)){if(!1===v(i))throw new r(e,o.KeyAccessorMustBeString,t);if(!0!==a.hasField(i))throw new r(e,o.FieldNotFound,t);l=F(a.field(i)),a.setField(i,"++"===t.operator?l+1:l-1)}else{if(x(a))throw new r(e,o.Immutable,t);if(!(a instanceof T))throw new r(e,o.InvalidParameter,t);if(!1===v(i))throw new r(e,o.ModuleAccessorMustBeString,t);if(!0!==a.hasGlobal(i))throw new r(e,o.ModuleExportNotFound,t);l=F(a.global(i)),a.setGlobal(i,"++"===t.operator?l+1:l-1)}return!1===t.prefix?l:"++"===t.operator?l+1:l-1}if(a="Identifier"===t.argument.type?t.argument.name.toLowerCase():"",!a)throw new r(e,o.InvalidIdentifier,t);if(null!=e.localScope&&void 0!==e.localScope[a])return l=F(e.localScope[a].value),e.localScope[a]={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[a])return l=F(e.globalScope[a].value),e.globalScope[a]={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 $(e,t,n,l,a){switch(t){case"=":return e===m?null:e;case"/=":return F(n)/F(e);case"*=":return F(n)*F(e);case"-=":return F(n)-F(e);case"+=":return v(n)||v(e)?R(n)+R(e):F(n)+F(e);case"%=":return F(n)%F(e);default:throw new r(a,o.UnsupportedOperator,l)}}function ee(e,t){const l=_(e,t.right);let a=null,i="";if("MemberExpression"===t.left.type){if(a=_(e,t.left.object),!0===t.left.computed)i=_(e,t.left.property);else{if("Identifier"!==t.left.property.type)throw new r(e,o.InvalidIdentifier,t);i=t.left.property.name}if(y(a)){if(!I(i))throw new r(e,o.ArrayAccessorMustBeNumber,t);if(i<0&&(i=a.length+i),i<0||i>a.length)throw new r(e,o.OutOfBounds,t);if(i===a.length){if("="!==t.operator)throw new r(e,o.OutOfBounds,t);a[i]=$(l,t.operator,a[i],t,e)}else a[i]=$(l,t.operator,a[i],t,e)}else if(a instanceof n){if(!1===v(i))throw new r(e,o.KeyAccessorMustBeString,t);if(!0===a.hasField(i))a.setField(i,$(l,t.operator,a.field(i),t,e));else{if("="!==t.operator)throw new r(e,o.FieldNotFound,t,{key:i});a.setField(i,$(l,t.operator,null,t,e))}}else if(S(a)){if(!1===v(i))throw new r(e,o.KeyAccessorMustBeString,t);if(!0===a.hasField(i))a.setField(i,$(l,t.operator,a.field(i),t,e));else{if("="!==t.operator)throw new r(e,o.FieldNotFound,t,{key:i});a.setField(i,$(l,t.operator,null,t,e))}}else{if(x(a))throw new r(e,o.Immutable,t);if(!(a instanceof T))throw new r(e,o.InvalidIdentifier,t);if(!1===v(i))throw new r(e,o.ModuleAccessorMustBeString,t);if(!0!==a.hasGlobal(i))throw new r(e,o.ModuleExportNotFound,t);a.setGlobal(i,$(l,t.operator,a.global(i),t,e))}return m}if(a=t.left.name.toLowerCase(),null!=e.localScope&&void 0!==e.localScope[a])return e.localScope[a]={value:$(l,t.operator,e.localScope[a].value,t,e),valueset:!0,node:t.right},m;if(void 0!==e.globalScope[a])return e.globalScope[a]={value:$(l,t.operator,e.globalScope[a].value,t,e),valueset:!0,node:t.right},m;throw new r(e,o.InvalidIdentifier,t)}function te(e,t){if("AssignmentExpression"===t.expression.type||"UpdateExpression"===t.expression.type)return _(e,t.expression);if("CallExpression"===t.expression.type){const n=_(e,t.expression);return n===m?m:new C(n)}{const n=_(e,t.expression);return n===m?m:new C(n)}}function ne(e,t){const n=_(e,t.test);if(!0===n)return _(e,t.consequent);if(!1===n)return null!==t.alternate?_(e,t.alternate):m;throw new r(e,o.BooleanConditionRequired,t)}function re(e,t){let n=m;for(let r=0;r=l.length||a<0)throw new r(e,o.OutOfBounds,t);return l[a]}throw new r(e,o.InvalidMemberAccessKey,t)}if(v(l)){if(I(a)&&isFinite(a)&&Math.floor(a)===a){if(a<0&&(a=l.length+a),a>=l.length||a<0)throw new r(e,o.OutOfBounds,t);return l[a]}throw new r(e,o.InvalidMemberAccessKey,t)}if(x(l)){if(I(a)&&isFinite(a)&&Math.floor(a)===a){if(a<0&&(a=l.length()+a),a>=l.length()||a<0)throw new r(e,o.OutOfBounds,t);return l.get(a)}throw new r(e,o.InvalidMemberAccessKey,t)}throw new r(e,o.InvalidMemberAccessKey,t)}}catch(l){throw l}}function fe(e,t){try{const n=_(e,t.argument);if(p(n)){if("!"===t.operator)return!n;if("-"===t.operator)return-1*F(n);if("+"===t.operator)return 1*F(n);if("~"===t.operator)return~F(n);throw new r(e,o.UnsupportedUnaryOperator,t)}if("~"===t.operator)return~F(n);if("-"===t.operator)return-1*F(n);if("+"===t.operator)return 1*F(n);throw new r(e,o.UnsupportedUnaryOperator,t)}catch(n){throw n}}function pe(e,t){try{const n=[];for(let l=0;l>":case">>>":case"^":case"&":return M(F(l),F(a),t.operator);case"==":return d(l,a);case"!=":return!d(l,a);case"<":case">":case"<=":case">=":return A(l,a,t.operator);case"+":return v(l)||v(a)?R(l)+R(a):F(l)+F(a);case"-":return F(l)-F(a);case"*":return F(l)*F(a);case"/":return F(l)/F(a);case"%":return F(l)%F(a);default:throw new r(e,o.UnsupportedOperator,t)}}catch(n){throw n}}function he(e,t){try{const n=_(e,t.left);if(p(n))switch(t.operator){case"||":if(!0===n)return n;{const n=_(e,t.right);if(p(n))return n;throw new r(e,o.LogicExpressionOrAnd,t)}case"&&":if(!1===n)return n;{const n=_(e,t.right);if(p(n))return n;throw new r(e,o.LogicExpressionOrAnd,t)}default:throw new r(e,o.LogicExpressionOrAnd,t)}throw new r(e,o.LogicalExpressionOnlyBoolean,t)}catch(n){throw n}}function we(e,t){return t.value?t.value.cooked:""}function me(e,t,n){if(g(e))throw new r(t,o.NoFunctionInTemplateLiteral,n);return e}function ge(e,t){let n="",r=0;for(const o of t.quasis)if(n+=o.value?o.value.cooked:"",!1===o.tail){n+=t.expressions[r]?R(me(_(e,t.expressions[r]),e,t)):"",r++}return n}function ve(e,t){let n;try{const l=t.name.toLowerCase();if(null!=e.localScope&&void 0!==e.localScope[l])return n=e.localScope[l],!0===n.valueset||(n.value=_(e,n.node),n.valueset=!0),n.value;if(void 0!==e.globalScope[l])return n=e.globalScope[l],!0===n.valueset||(n.value=_(e,n.node),n.valueset=!0),n.value;throw new r(e,o.InvalidIdentifier,t)}catch(l){throw l}}function ye(e,t){try{if("MemberExpression"===t.callee.type){const n=_(e,t.callee.object);if(!(n instanceof T))throw new r(e,o.FuncionNotFound,t);const l=!1===t.callee.computed?t.callee.property.name:_(e,t.callee.property);if(!n.hasGlobal(l))throw new r(e,o.FuncionNotFound,t);const a=n.global(l);if(!g(a))throw new r(e,o.CallNonFunction,t);return a.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)}catch(n){throw n}}const be={};function xe(e,t,n,r){try{const o=_(e,t.arguments[n]);if(d(o,r))return _(e,t.arguments[n+1]);{const o=t.arguments.length-n;return 1===o?_(e,t.arguments[n]):2===o?null:3===o?_(e,t.arguments[n+2]):xe(e,t,n+2,r)}}catch(o){throw o}}function Se(e,t,n,l){try{if(!0===l)return _(e,t.arguments[n+1]);if(3===t.arguments.length-n)return _(e,t.arguments[n+2]);{const l=_(e,t.arguments[n+2]);if(!1===p(l))throw new r(e,o.BooleanConditionRequired,t.arguments[n+2]);return Se(e,t,n+2,l)}}catch(a){throw a}}function Ie(e,t,n,l){try{const a=e.body;if(n.length!==e.params.length)throw new r(t,o.WrongNumberOfParameters,l);if(null!=t.localScope)for(let r=0;r