/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.24/esri/copyright.txt for details. */ import e from"./ArcadePortal.js";import t from"./Attachment.js";import r from"./Dictionary.js";import n from"./Feature.js";import o from"./FunctionWrapper.js";import a from"./ImmutablePathArray.js";import i from"./ImmutablePointArray.js";import{p as s,i as c,m as l,N as u,x as f,v as p,w,o as h,b as d,l as m,n as g,s as E,y,a as N,f as v,I,R as T,h as S,q as b,r as R,j as O,z as A,A as U}from"../chunks/languageUtils.js";import{nodeErrorMessage as C,addFunctionDeclaration as F,findFieldLiterals as x,validateScript as M,referencesMember as P,referencesFunction as D,findFunctionCalls as j}from"./treeAnalysis.js";import{A as L}from"../chunks/array.js";import{registerFunctions as k}from"./functions/date.js";import{registerFunctions as _}from"./functions/geomasync.js";import{registerFunctions as V}from"./functions/geometry.js";import{registerFunctions as Y}from"./functions/maths.js";import{registerFunctions as B}from"./functions/stats.js";import{registerFunctions as G}from"./functions/string.js";import z from"../geometry/Extent.js";import q from"../geometry/Geometry.js";import H from"../geometry/Multipoint.js";import Z from"../geometry/Point.js";import W from"../geometry/Polygon.js";import K from"../geometry/Polyline.js";import X from"../geometry/SpatialReference.js";function J(e){return e&&"function"==typeof e.then}const Q=100;async function $(e,t){const r=[];for(let n=0;n{try{!0===r.testResult?++a>Q?(a=0,setTimeout((()=>{se(e,t,r,n,o,a)}),0)):se(e,t,r,n,o,a):r.lastAction instanceof T?n(r.lastAction):n(p)}catch(i){o(i)}}),(e=>{o(e)}))}catch(i){o(i)}}function ce(e,t){try{return null!==t.init?re(e,t.init).then((()=>new Promise(((r,n)=>{se(e,t,{testResult:!0,lastAction:p},(e=>{r(e)}),(e=>{n(e)}),0)})))):new Promise(((r,n)=>{se(e,t,{testResult:!0,lastAction:p},(e=>{r(e)}),(e=>{n(e)}),0)}))}catch(r){return Promise.reject(r)}}function le(e,t,r,n,o,a,i,s,c,l){try{if(n<=a)return void s(p);o.value="k"===i?r[a]:a,re(e,t.body).then((u=>{try{u instanceof T?s(u):u===w?s(p):++l>Q?(l=0,setTimeout((()=>{le(e,t,r,n,o,a+1,i,s,c,l)}),0)):le(e,t,r,n,o,a+1,i,s,c,l)}catch(f){c(f)}}),(e=>{c(e)}))}catch(u){c(u)}}function ue(e,t,r,n,o,a,i,s,c){try{if(r.length()<=o)return void i(p);n.value="k"===a?r.get(o):o,re(e,t.body).then((l=>{l instanceof T?i(l):l===w?i(p):++c>Q?(c=0,setTimeout((()=>{ue(e,t,r,n,o+1,a,i,s,c)}),0)):ue(e,t,r,n,o+1,a,i,s,c)}),(e=>{s(e)}))}catch(l){s(l)}}function fe(e,t,r,n,o,a){try{if(void 0===a&&(a="i"),0===r.length)return void n.resolve(p);le(e,t,r,r.length,o,0,a,(e=>{n.resolve(e)}),(e=>{n.reject(e)}),0)}catch(i){n.reject(i)}}function pe(e,t,r,n,o,a){try{if(void 0===a&&(a="i"),0===r.length)return void n.resolve(p);ue(e,t,r,o,0,a,(e=>{n.resolve(e)}),(e=>{n.reject(e)}),0)}catch(i){n.reject(i)}}function we(e,t,r,n,o){try{fe(e,t,r.keys(),n,o,"k")}catch(a){n.reject(a)}}function he(e,t,r,o,a,i,s,c){try{e.next().then((l=>{try{if(null===l)i(p);else{const u=n.createFromGraphicLikeObject(l.geometry,l.attributes,o);u._underlyingGraphic=l,a.value=u;re(t,r.body).then((n=>{try{n===w?i(p):n instanceof T?i(n):++c>Q?(c=0,setTimeout((()=>{he(e,t,r,o,a,i,s,c)}),0)):he(e,t,r,o,a,i,s,c)}catch(l){s(l)}}),(e=>{s(e)}))}}catch(u){s(u)}}),(e=>{s(e)}))}catch(l){s(l)}}async function de(e,t){return new Promise(((n,o)=>{re(e,t.right).then((a=>{try{let i=null;i="VariableDeclaration"===t.left.type?re(e,t.left):Promise.resolve(),i.then((()=>{try{let i="";if("VariableDeclaration"===t.left.type){const e=t.left.declarations[0].id;"Identifier"===e.type&&(i=e.name)}else"Identifier"===t.left.type&&(i=t.left.name);if(!i)throw new Error(C(t,"RUNTIME","INVALIDVARIABLE"));i=i.toLowerCase();let s=null;if(null!==e.localScope&&void 0!==e.localScope[i]&&(s=e.localScope[i]),null===s&&void 0!==e.globalScope[i]&&(s=e.globalScope[i]),null===s)return void o(new Error(C(t,"RUNTIME","VARIABLENOTDECLARED")));m(a)||d(a)?fe(e,t,a,{reject:o,resolve:n},s):g(a)?pe(e,t,a,{reject:o,resolve:n},s):a instanceof r||E(a)?we(e,t,a,{reject:o,resolve:n},s):y(a)?he(a.iterator(e.abortSignal),e,t,a,s,(e=>{n(e)}),(e=>{o(e)}),0):fe(e,t,[],{reject:o,resolve:n},s)}catch(i){o(i)}}),o)}catch(i){o(i)}}),o)}))}async function me(e,t){const n=t.argument;if("MemberExpression"===n.type){const o={t:null},a=await re(e,n.object);let i=null;o.t=a,!0===n.computed?i=await re(e,n.property):"Identifier"===n.property.type&&(i=n.property.name);const s=o.t;let c;if(m(s)){if(!N(i))throw new Error("Invalid Parameter");if(i<0&&(i=s.length+i),i<0||i>=s.length)throw new Error("Assignment outside of array bounds");c=v(s[i]),s[i]="++"===t.operator?c+1:c-1}else if(s instanceof r){if(!1===d(i))throw new Error("Dictionary accessor must be a string");if(!0!==s.hasField(i))throw new Error("Invalid Parameter");c=v(s.field(i)),s.setField(i,"++"===t.operator?c+1:c-1)}else{if(!E(s))throw g(s)?new Error("Array is Immutable"):new Error("Invalid Parameter");if(!1===d(i))throw new Error("Feature accessor must be a string");if(!0!==s.hasField(i))throw new Error("Invalid Parameter");c=v(s.field(i)),s.setField(i,"++"===t.operator?c+1:c-1)}return!1===t.prefix?c:"++"===t.operator?c+1:c-1}const o="Identifier"===t.argument.type?t.argument.name.toLowerCase():"";if(!o)throw new Error("Invalid identifier");let a;if(null!==e.localScope&&void 0!==e.localScope[o])return a=v(e.localScope[o].value),e.localScope[o]={value:"++"===t.operator?a+1:a-1,valueset:!0,node:t},!1===t.prefix?a:"++"===t.operator?a+1:a-1;if(void 0!==e.globalScope[o])return a=v(e.globalScope[o].value),e.globalScope[o]={value:"++"===t.operator?a+1:a-1,valueset:!0,node:t},!1===t.prefix?a:"++"===t.operator?a+1:a-1;throw new Error("Variable not recognised")}function ge(e,t,r,n){switch(t){case"=":return e===p?null:e;case"/=":return v(r)/v(e);case"*=":return v(r)*v(e);case"-=":return v(r)-v(e);case"+=":return d(r)||d(e)?S(r)+S(e):v(r)+v(e);case"%=":return v(r)%v(e);default:throw new Error(C(n,"RUNTIME","OPERATORNOTRECOGNISED"))}}async function Ee(e,t){const n=t.left;if("MemberExpression"===n.type){const o=await re(e,t.right),a=await re(e,n.object);let i=null;if(!0===n.computed)i=await re(e,n.property);else{if("Identifier"!==n.property.type)throw new Error("Expected computed or identifier for assignemnt target");i=n.property.name}if(m(a)){if(!N(i))throw new Error("Invalid Parameter");if(i<0&&(i=a.length+i),i<0||i>a.length)throw new Error("Assignment outside of array bounds");if(i===a.length){if("="!==t.operator)throw new Error("Invalid Parameter");a[i]=ge(o,t.operator,a[i],t)}else a[i]=ge(o,t.operator,a[i],t)}else if(a instanceof r){if(!1===d(i))throw new Error("Dictionary accessor must be a string");if(!0===a.hasField(i))a.setField(i,ge(o,t.operator,a.field(i),t));else{if("="!==t.operator)throw new Error("Invalid Parameter");a.setField(i,ge(o,t.operator,null,t))}}else{if(!E(a))throw g(a)?new Error("Array is Immutable"):new Error("Invalid Parameter");if(!1===d(i))throw new Error("Feature accessor must be a string");if(!0===a.hasField(i))a.setField(i,ge(o,t.operator,a.field(i),t));else{if("="!==t.operator)throw new Error("Invalid Parameter");a.setField(i,ge(o,t.operator,null,t))}}return p}const o=n.name.toLowerCase();if(null!==e.localScope&&void 0!==e.localScope[o]){const r=await re(e,t.right);return e.localScope[o]={value:ge(r,t.operator,e.localScope[o].value,t),valueset:!0,node:t.right},p}if(void 0!==e.globalScope[o]){const r=await re(e,t.right);return e.globalScope[o]={value:ge(r,t.operator,e.globalScope[o].value,t),valueset:!0,node:t.right},p}throw new Error("Cannot assign undeclared variable")}async function ye(e,t){if("AssignmentExpression"===t.expression.type)return re(e,t.expression);if("CallExpression"===t.expression.type){const r=await re(e,t.expression);return r===p?p:new I(r)}const r=await re(e,t.expression);return r===p?p:new I(r)}async function Ne(e,t){if("AssignmentExpression"===t.test.type||"UpdateExpression"===t.test.type)throw new Error(C(t.test,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));const r=await re(e,t.test);if(!0===r)return re(e,t.consequent);if(!1===r)return null!==t.alternate?re(e,t.alternate):p;throw new Error(C(t.test,"RUNTIME","CANNOT_USE_NONBOOLEAN_IN_CONDITION"))}async function ve(e,t){return Ie(e,t,0)}async function Ie(e,t,r){if(r>=t.body.length)return p;const n=await re(e,t.body[r]);return n instanceof T||n===w||n===f||r===t.body.length-1?n:Ie(e,t,r+1)}async function Te(e,t){if(null===t.argument)return new T(p);const r=await re(e,t.argument);return new T(r)}async function Se(e,t){const r=t.id.name.toLowerCase();return e.globalScope[r]={valueset:!0,node:null,value:new o(t,e)},p}async function be(e,t,r){return r>=t.declarations.length?p:(await re(e,t.declarations[r]),r===t.declarations.length-1||await be(e,t,r+1),p)}async function Re(e,t){let r=null;if(r=null===t.init?null:await re(e,t.init),null!==e.localScope){if(r===p&&(r=null),"Identifier"!==t.id.type)throw new Error("Can only assign a regular variable");const n=t.id.name.toLowerCase();return e.localScope[n]={value:r,valueset:!0,node:t.init},p}if("Identifier"!==t.id.type)throw new Error("Can only assign a regular variable");const n=t.id.name.toLowerCase();return r===p&&(r=null),e.globalScope[n]={value:r,valueset:!0,node:t.init},p}let Oe=0;function Ae(e,t,n,o){let s;switch(t=t.toLowerCase()){case"hasz":{const t=e.hasZ;return void 0!==t&&t}case"hasm":{const t=e.hasM;return void 0!==t&&t}case"spatialreference":{let t=e.spatialReference._arcadeCacheId;if(void 0===t){let r=!0;Object.freeze&&Object.isFrozen(e.spatialReference)&&(r=!1),r&&(Oe++,e.spatialReference._arcadeCacheId=Oe,t=Oe)}const n=new r({wkt:e.spatialReference.wkt,wkid:e.spatialReference.wkid});return void 0!==t&&(n._arcadeCacheId="SPREF"+t.toString()),n}}switch(e.type){case"extent":switch(t){case"xmin":case"xmax":case"ymin":case"ymax":case"zmin":case"zmax":case"mmin":case"mmax":{const r=e[t];return void 0!==r?r:null}case"type":return"Extent"}break;case"polygon":switch(t){case"rings":s=e.cache._arcadeCacheId,void 0===s&&(Oe++,s=Oe,e.cache._arcadeCacheId=s);return new a(e.rings,e.spatialReference,!0===e.hasZ,!0===e.hasM,s);case"type":return"Polygon"}break;case"point":switch(t){case"x":case"y":case"z":case"m":return void 0!==e[t]?e[t]:null;case"type":return"Point"}break;case"polyline":switch(t){case"paths":s=e.cache._arcadeCacheId,void 0===s&&(Oe++,s=Oe,e.cache._arcadeCacheId=s);return new a(e.paths,e.spatialReference,!0===e.hasZ,!0===e.hasM,s);case"type":return"Polyline"}break;case"multipoint":switch(t){case"points":s=e.cache._arcadeCacheId,void 0===s&&(Oe++,s=Oe,e.cache._arcadeCacheId=s);return new i(e.points,e.spatialReference,!0===e.hasZ,!0===e.hasM,s,1);case"type":return"Multipoint"}}throw new Error(C(o,"RUNTIME","PROPERTYNOTFOUND"))}async function Ue(e,t){const n=await re(e,t.object);if(null===n)throw new Error(C(t,"RUNTIME","NOTFOUND"));if(!1===t.computed){if("Identifier"===t.property.type){if(n instanceof r||E(n))return n.field(t.property.name);if(n instanceof q)return Ae(n,t.property.name,e,t);throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}let o=await re(e,t.property);if(n instanceof r||E(n)){if(d(o))return n.field(o);throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(n instanceof q){if(d(o))return Ae(n,o,e,t);throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(m(n)){if(N(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length+o),o>=n.length||o<0)throw new Error(C(t,"RUNTIME","OUTOFBOUNDS"));return n[o]}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(g(n)){if(N(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length()+o),o>=n.length()||o<0)throw new Error(C(t,"RUNTIME","OUTOFBOUNDS"));return n.get(o)}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(d(n)){if(N(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length+o),o>=n.length||o<0)throw new Error(C(t,"RUNTIME","OUTOFBOUNDS"));return n[o]}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}async function Ce(e,t){const r=await re(e,t.argument);if(c(r)){if("!"===t.operator)return!r;if("-"===t.operator)return-1*v(r);if("+"===t.operator)return 1*v(r);if("~"===t.operator)return~v(r);throw new Error(C(t,"RUNTIME","NOTSUPPORTEDUNARYOPERATOR"))}if("-"===t.operator)return-1*v(r);if("+"===t.operator)return 1*v(r);if("~"===t.operator)return~v(r);throw new Error(C(t,"RUNTIME","NOTSUPPORTEDUNARYOPERATOR"))}async function Fe(e,t){const r=[];for(let n=0;n>":case">>>":case"^":case"&":return R(v(n),v(o),t.operator);case"==":return l(n,o);case"!=":return!l(n,o);case"<":case">":case"<=":case">=":return b(n,o,t.operator);case"+":return d(n)||d(o)?S(n)+S(o):v(n)+v(o);case"-":return v(n)-v(o);case"*":return v(n)*v(o);case"/":return v(n)/v(o);case"%":return v(n)%v(o);default:throw new Error(C(t,"RUNTIME","OPERATORNOTRECOGNISED"))}}async function Me(e,t){if("AssignmentExpression"===t.left.type||"UpdateExpression"===t.left.type)throw new Error(C(t.left,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));if("AssignmentExpression"===t.right.type||"UpdateExpression"===t.right.type)throw new Error(C(t.right,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));const r=await re(e,t.left);let n=null;if(!c(r))throw new Error(C(t,"RUNTIME","ONLYBOOLEAN"));switch(t.operator){case"||":if(!0===r)return r;if(n=await re(e,t.right),c(n))return n;throw new Error(C(t,"RUNTIME","ONLYORORAND"));case"&&":if(!1===r)return r;if(n=await re(e,t.right),c(n))return n;throw new Error(C(t,"RUNTIME","ONLYORORAND"));default:throw new Error(C(t,"RUNTIME","ONLYORORAND"))}}async function Pe(e,t){const r=t.name.toLowerCase();if(null!==e.localScope&&void 0!==e.localScope[r]){const t=e.localScope[r];if(!0===t.valueset)return t.value;if(null!==t.d)return t.d;t.d=re(e,t.node);const n=await t.d;return t.value=n,t.valueset=!0,n}if(void 0!==e.globalScope[r]){const t=e.globalScope[r];if(!0===t.valueset)return t.value;if(null!==t.d)return t.d;t.d=re(e,t.node);const n=await t.d;return t.value=n,t.valueset=!0,n}throw new Error(C(t,"RUNTIME","VARIABLENOTFOUND"))}async function De(e,t){if("Identifier"!==t.callee.type)throw new Error(C(t,"RUNTIME","ONLYNODESSUPPORTED"));if(null!==e.localScope&&void 0!==e.localScope[t.callee.name.toLowerCase()]){const r=e.localScope[t.callee.name.toLowerCase()];if(r.value instanceof u)return r.value.fn(e,t);if(r.value instanceof o)return Ze(e,t,r.value.definition);throw new Error(C(t,"RUNTIME","NOTAFUNCTION"))}if(void 0!==e.globalScope[t.callee.name.toLowerCase()]){const r=e.globalScope[t.callee.name.toLowerCase()];if(r.value instanceof u)return r.value.fn(e,t);if(r.value instanceof o)return Ze(e,t,r.value.definition);throw new Error(C(t,"RUNTIME","NOTAFUNCTION"))}throw new Error(C(t,"RUNTIME","NOTFOUND"))}async function je(e,t){return t.value?t.value.cooked:""}async function Le(e,t){const r=[];for(let a=0;a0||t.length>0))return n;if(e.length>0&&t.length>0){let a=await r(e[0],t[0]);return isNaN(a)&&(a=1),a<=0?(o.push(e[0]),e=e.slice(1)):(o.push(t[0]),t=t.slice(1)),Ge(e,t,r,n)}return e.length>0?(o.push(e[0]),Ge(e=e.slice(1),t,r,n)):t.length>0?(o.push(t[0]),Ge(e,t=t.slice(1),r,n)):void 0}function ze(e,t){const r=e.length,n=Math.floor(r/2);return t||(t=function(e,t){return e0||t.length>0;)if(e.length>0&&t.length>0){let o=r(e[0],t[0]);isNaN(o)&&(o=1),o<=0?(n.push(e[0]),e=e.slice(1)):(n.push(t[0]),t=t.slice(1))}else e.length>0?(n.push(e[0]),e=e.slice(1)):t.length>0&&(n.push(t[0]),t=t.slice(1));return n}async function He(e,t,r){const n=e.body;if(r.length!==e.params.length)throw new Error("Invalid Parameter calls to function.");for(let a=0;a64)throw new Error("Exceeded maximum function depth");return He(r,a,o)}))}function We(e){const t=function(){const t={abortSignal:e.context.abortSignal,spatialReference:e.context.spatialReference,console:e.context.console,lrucache:e.context.lrucache,interceptor:e.context.interceptor,services:e.context.services,localScope:{},globalScope:e.context.globalScope,depthCounter:e.context.depthCounter+1};if(t.depthCounter>64)throw new Error("Exceeded maximum function depth");return He(e.definition,t,arguments)};return t}k(ke,ee),G(ke,ee),Y(ke,ee),V(ke,ee),B(ke,ee),_({functions:ke,compiled:!1,signatures:null,evaluateIdentifier:null,arcadeCustomFunctionHandler:null,mode:"async",standardFunction:ee,standardFunctionAsync:te}),ke.typeof=function(e,t){return ee(e,t,(function(e,t,r){s(r,1,1);const n=_e(r[0]);if("Unrecognised Type"===n)throw new Error("Unrecognised Type");return n}))},ke.iif=async function(e,t){s(null===t.arguments?[]:t.arguments,3,3);const r=await re(e,t.arguments[0]);if(!1===c(r))throw new Error("IF Function must have a boolean test condition");const n=[];return n[0]=await re(e,t.arguments[1]),n[1]=await re(e,t.arguments[2]),r?n[0]:n[1]},ke.decode=async function(e,t){if(t.arguments.length<2)throw new Error("Missing Parameters");if(2===t.arguments.length)return re(e,t.arguments[1]);if((t.arguments.length-1)%2==0)throw new Error("Must have a default value result.");return Ve(e,t,1,await re(e,t.arguments[0]))},ke.when=async function(e,t){if(t.arguments.length<3)throw new Error("Missing Parameters");if(t.arguments.length%2==0)throw new Error("Must have a default value result.");const r=await re(e,t.arguments[0]);if(!1===c(r))throw new Error("WHEN needs boolean test conditions");return Ye(e,t,0,r)},ke.sort=function(e,t){return te(e,t,(async function(e,t,r){s(r,1,2);let n=r[0];if(g(n)&&(n=n.toArray()),!1===m(n))throw new Error("Illegal Argument");if(r.length>1){if(!1===h(r[1]))throw new Error("Illegal Argument");return Be(n,We(r[1]))}let o=n;if(0===o.length)return[];const a={};for(let s=0;s1||"String"===c?o=ze(o,(function(e,t){if(null==e||e===p)return null==t||t===p?0:1;if(null==t||t===p)return-1;const r=S(e),n=S(t);return r