/* 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 r from"./Attachment.js";import t 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 l,m as c,N as u,x as f,w as p,v as h,o as w,b as m,l as E,R as d,n as g,s as N,a as y,f as I,I as v,h as T,q as R,r as O,j as S,y as b,z as A,A as U}from"../chunks/languageUtils.js";import{nodeErrorMessage as C,addFunctionDeclaration as x,findFieldLiterals as M,findExpectedFieldLiterals as F,validateScript as P,referencesMember as D,referencesFunction as L,findFunctionCalls as j}from"./treeAnalysis.js";import{A as _}from"../chunks/array.js";import{registerFunctions as k}from"./functions/date.js";import{registerFunctions as V}from"./functions/geometry.js";import{registerFunctions as B}from"./functions/geomsync.js";import{registerFunctions as Y}from"./functions/maths.js";import{registerFunctions as G}from"./functions/stats.js";import{registerFunctions as z}from"./functions/string.js";import q from"../geometry/Extent.js";import Z from"../geometry/Geometry.js";import H from"../geometry/Multipoint.js";import W from"../geometry/Point.js";import K from"../geometry/Polygon.js";import X from"../geometry/Polyline.js";import J from"../geometry/SpatialReference.js";function Q(e,r){const t=[];for(let n=0;n=o.length)throw new Error("Assignment outside of array bounds");n=I(o[a]),o[a]="++"===r.operator?n+1:n-1}else if(o instanceof t){if(!1===m(a))throw new Error("Dictionary accessor must be a string");if(!0!==o.hasField(a))throw new Error("Invalid Parameter");n=I(o.field(a)),o.setField(a,"++"===r.operator?n+1:n-1)}else{if(!N(o))throw g(o)?new Error("Array is Immutable"):new Error("Invalid Parameter");if(!1===m(a))throw new Error("Feature accessor must be a string");if(!0!==o.hasField(a))throw new Error("Invalid Parameter");n=I(o.field(a)),o.setField(a,"++"===r.operator?n+1:n-1)}return!1===r.prefix?n:"++"===r.operator?n+1:n-1}if(o="Identifier"===r.argument.type?r.argument.name.toLowerCase():"",!o)throw new Error("Invalid identifier");if(null!==e.localScope&&void 0!==e.localScope[o])return n=I(e.localScope[o].value),e.localScope[o]={value:"++"===r.operator?n+1:n-1,valueset:!0,node:r},!1===r.prefix?n:"++"===r.operator?n+1:n-1;if(void 0!==e.globalScope[o])return n=I(e.globalScope[o].value),e.globalScope[o]={value:"++"===r.operator?n+1:n-1,valueset:!0,node:r},!1===r.prefix?n:"++"===r.operator?n+1:n-1;throw new Error("Variable not recognised")}function se(e,r,t,n){switch(r){case"=":return e===h?null:e;case"/=":return I(t)/I(e);case"*=":return I(t)*I(e);case"-=":return I(t)-I(e);case"+=":return m(t)||m(e)?T(t)+T(e):I(t)+I(e);case"%=":return I(t)%I(e);default:throw new Error(C(n,"RUNTIME","OPERATORNOTRECOGNISED"))}}function le(e,r){const n=ee(e,r.right);let o=null,a="";if("MemberExpression"===r.left.type){if(o=ee(e,r.left.object),!0===r.left.computed?a=ee(e,r.left.property):"Identifier"===r.left.property.type&&(a=r.left.property.name),E(o)){if(!y(a))throw new Error("Invalid Parameter");if(a<0&&(a=o.length+a),a<0||a>o.length)throw new Error("Assignment outside of array bounds");if(a===o.length){if("="!==r.operator)throw new Error("Invalid Parameter");o[a]=se(n,r.operator,o[a],r)}else o[a]=se(n,r.operator,o[a],r)}else if(o instanceof t){if(!1===m(a))throw new Error("Dictionary accessor must be a string");if(!0===o.hasField(a))o.setField(a,se(n,r.operator,o.field(a),r));else{if("="!==r.operator)throw new Error("Invalid Parameter");o.setField(a,se(n,r.operator,null,r))}}else{if(!N(o))throw g(o)?new Error("Array is Immutable"):new Error("Invalid Parameter");if(!1===m(a))throw new Error("Feature accessor must be a string");if(!0===o.hasField(a))o.setField(a,se(n,r.operator,o.field(a),r));else{if("="!==r.operator)throw new Error("Invalid Parameter");o.setField(a,se(n,r.operator,null,r))}}return h}if(o=r.left.name.toLowerCase(),null!==e.localScope&&void 0!==e.localScope[o])return e.localScope[o]={value:se(n,r.operator,e.localScope[o].value,r),valueset:!0,node:r.right},h;if(void 0!==e.globalScope[o])return e.globalScope[o]={value:se(n,r.operator,e.globalScope[o].value,r),valueset:!0,node:r.right},h;throw new Error("Variable not recognised")}function ce(e,r){if("AssignmentExpression"===r.expression.type||"UpdateExpression"===r.expression.type)return ee(e,r.expression);if("CallExpression"===r.expression.type){const t=ee(e,r.expression);return t===h?h:new v(t)}{const t=ee(e,r.expression);return t===h?h:new v(t)}}function ue(e,r){if("AssignmentExpression"===r.test.type||"UpdateExpression"===r.test.type)throw new Error(C(r.test,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));const t=ee(e,r.test);if(!0===t)return ee(e,r.consequent);if(!1===t)return null!==r.alternate?ee(e,r.alternate):h;throw new Error(C(r,"RUNTIME","CANNOT_USE_NONBOOLEAN_IN_CONDITION"))}function fe(e,r){let t=h;for(let n=0;n=n.length||o<0)throw new Error(C(r,"RUNTIME","OUTOFBOUNDS"));return n[o]}throw new Error(C(r,"RUNTIME","INVALIDTYPE"))}if(m(n)){if(y(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length+o),o>=n.length||o<0)throw new Error(C(r,"RUNTIME","OUTOFBOUNDS"));return n[o]}throw new Error(C(r,"RUNTIME","INVALIDTYPE"))}if(g(n)){if(y(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length()+o),o>=n.length()||o<0)throw new Error(C(r,"RUNTIME","OUTOFBOUNDS"));return n.get(o)}throw new Error(C(r,"RUNTIME","INVALIDTYPE"))}throw new Error(C(r,"RUNTIME","INVALIDTYPE"))}}catch(n){throw n}}function Ne(e,r){try{const t=ee(e,r.argument);if(l(t)){if("!"===r.operator)return!t;if("-"===r.operator)return-1*I(t);if("+"===r.operator)return 1*I(t);if("~"===r.operator)return~I(t);throw new Error(C(r,"RUNTIME","NOTSUPPORTEDUNARYOPERATOR"))}if("~"===r.operator)return~I(t);if("-"===r.operator)return-1*I(t);if("+"===r.operator)return 1*I(t);throw new Error(C(r,"RUNTIME","NOTSUPPORTEDUNARYOPERATOR"))}catch(t){throw t}}function ye(e,r){try{const t=[];for(let n=0;n>":case">>>":case"^":case"&":return O(I(n),I(o),r.operator);case"==":return c(n,o);case"!=":return!c(n,o);case"<":case">":case"<=":case">=":return R(n,o,r.operator);case"+":return m(n)||m(o)?T(n)+T(o):I(n)+I(o);case"-":return I(n)-I(o);case"*":return I(n)*I(o);case"/":return I(n)/I(o);case"%":return I(n)%I(o);default:throw new Error(C(r,"RUNTIME","OPERATORNOTRECOGNISED"))}}catch(t){throw t}}function ve(e,r){try{if("AssignmentExpression"===r.left.type||"UpdateExpression"===r.left.type)throw new Error(C(r.left,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));if("AssignmentExpression"===r.right.type||"UpdateExpression"===r.right.type)throw new Error(C(r.right,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));const t=ee(e,r.left);if(l(t))switch(r.operator){case"||":if(!0===t)return t;{const t=ee(e,r.right);if(l(t))return t;throw new Error(C(r,"RUNTIME","ONLYORORAND"))}case"&&":if(!1===t)return t;{const t=ee(e,r.right);if(l(t))return t;throw new Error(C(r,"RUNTIME","ONLYORORAND"))}default:throw new Error(C(r,"RUNTIME","ONLYORORAND"))}throw new Error(C(r,"RUNTIME","ONLYBOOLEAN"))}catch(t){throw t}}function Te(e,r){return r.value?r.value.cooked:""}function Re(e,r){let t="",n=0;for(const o of r.quasis)if(t+=o.value?o.value.cooked:"",!1===o.tail){t+=r.expressions[n]?T(ee(e,r.expressions[n])):"",n++}return t}function Oe(e,r){let t;try{const n=r.name.toLowerCase();if(null!==e.localScope&&void 0!==e.localScope[n])return t=e.localScope[n],!0===t.valueset||(t.value=ee(e,t.node),t.valueset=!0),t.value;if(void 0!==e.globalScope[n])return t=e.globalScope[n],!0===t.valueset||(t.value=ee(e,t.node),t.valueset=!0),t.value;throw new Error(C(r,"RUNTIME","VARIABLENOTFOUND"))}catch(n){throw n}}function Se(e,r){try{if("Identifier"!==r.callee.type)throw new Error(C(r,"RUNTIME","ONLYNODESSUPPORTED"));if(null!==e.localScope&&void 0!==e.localScope[r.callee.name.toLowerCase()]){const t=e.localScope[r.callee.name.toLowerCase()];if(t.value instanceof u)return t.value.fn(e,r);if(t.value instanceof o)return Pe(e,r,t.value.definition);throw new Error(C(r,"RUNTIME","NOTAFUNCTION"))}if(void 0!==e.globalScope[r.callee.name.toLowerCase()]){const t=e.globalScope[r.callee.name.toLowerCase()];if(t.value instanceof u)return t.value.fn(e,r);if(t.value instanceof o)return Pe(e,r,t.value.definition);throw new Error(C(r,"RUNTIME","NOTAFUNCTION"))}throw new Error(C(r,"RUNTIME","NOTFOUND"))}catch(t){throw t}}const be={};function Ae(n){return null==n?"":E(n)||g(n)?"Array":S(n)?"Date":m(n)?"String":l(n)?"Boolean":y(n)?"Number":n instanceof r?"Attachment":n instanceof e?"Portal":n instanceof t?"Dictionary":N(n)?"Feature":n instanceof W?"Point":n instanceof K?"Polygon":n instanceof X?"Polyline":n instanceof H?"Multipoint":n instanceof q?"Extent":w(n)?"Function":b(n)?"FeatureSet":A(n)?"FeatureSetCollection":n===h?"":"number"==typeof n&&isNaN(n)?"Number":"Unrecognised Type"}function Ue(e,r,t,n){try{const o=ee(e,r.arguments[t]);if(c(o,n))return ee(e,r.arguments[t+1]);{const o=r.arguments.length-t;return 1===o?ee(e,r.arguments[t]):2===o?null:3===o?ee(e,r.arguments[t+2]):Ue(e,r,t+2,n)}}catch(o){throw o}}function Ce(e,r,t,n){try{if(!0===n)return ee(e,r.arguments[t+1]);if(3===r.arguments.length-t)return ee(e,r.arguments[t+2]);{const n=ee(e,r.arguments[t+2]);if(!1===l(n))throw new Error("WHEN needs boolean test conditions");return Ce(e,r,t+2,n)}}catch(o){throw o}}function xe(e,r){const t=e.length,n=Math.floor(t/2);return 0===t?[]:1===t?[e[0]]:Me(xe(e.slice(0,n),r),xe(e.slice(n,t),r),r)}function Me(e,r,t){const n=[];for(;e.length>0||r.length>0;)if(e.length>0&&r.length>0){let o=t(e[0],r[0]);isNaN(o)&&(o=0),o<=0?(n.push(e[0]),e=e.slice(1)):(n.push(r[0]),r=r.slice(1))}else e.length>0?(n.push(e[0]),e=e.slice(1)):r.length>0&&(n.push(r[0]),r=r.slice(1));return n}function Fe(e,r,t){try{const n=e.body;if(t.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 Fe(t,a,o)}))}function De(e){const r=function(){const r={spatialReference:e.context.spatialReference,console:e.context.console,lrucache:e.context.lrucache,interceptor:e.context.interceptor,localScope:{},depthCounter:e.context.depthCounter+1,globalScope:e.context.globalScope};if(r.depthCounter>64)throw new Error("Exceeded maximum function depth");return Fe(e.definition,r,arguments)};return r}k(be,$),z(be,$),Y(be,$),V(be,$),G(be,$),B(be,$),be.typeof=function(e,r){return $(e,r,(function(e,r,t){s(t,1,1);const n=Ae(t[0]);if("Unrecognised Type"===n)throw new Error("Unrecognised Type");return n}))},be.iif=function(e,r){try{s(null===r.arguments?[]:r.arguments,3,3);const t=ee(e,r.arguments[0]);if(!1===l(t))throw new Error("IF Function must have a boolean test condition");const n=ee(e,r.arguments[1]),o=ee(e,r.arguments[2]);return!0===t?n:o}catch(t){throw t}},be.decode=function(e,r){try{if(r.arguments.length<2)throw new Error("Missing Parameters");if(2===r.arguments.length)return ee(e,r.arguments[1]);{if((r.arguments.length-1)%2==0)throw new Error("Must have a default value result.");const t=ee(e,r.arguments[0]);return Ue(e,r,1,t)}}catch(t){throw t}},be.when=function(e,r){try{if(r.arguments.length<3)throw new Error("Missing Parameters");if(r.arguments.length%2==0)throw new Error("Must have a default value result.");const t=ee(e,r.arguments[0]);if(!1===l(t))throw new Error("WHEN needs boolean test conditions");return Ce(e,r,0,t)}catch(t){throw t}},be.top=function(e,r){return $(e,r,(function(e,r,t){if(s(t,2,2),E(t[0]))return I(t[1])>=t[0].length?t[0].slice(0):t[0].slice(0,I(t[1]));if(g(t[0]))return I(t[1])>=t[0].length()?t[0].slice(0):t[0].slice(0,I(t[1]));throw new Error("Top cannot accept this parameter type")}))},be.first=function(e,r){return $(e,r,(function(e,r,t){return s(t,1,1),E(t[0])?0===t[0].length?null:t[0][0]:g(t[0])?0===t[0].length()?null:t[0].get(0):null}))},be.sort=function(e,r){return $(e,r,(function(e,r,t){s(t,1,2);let n=t[0];if(g(n)&&(n=n.toArray()),!1===E(n))throw new Error("Illegal Argument");if(t.length>1){if(!1===w(t[1]))throw new Error("Illegal Argument");let e=n;const r=De(t[1]);return e=xe(e,(function(e,t){return r(e,t)})),e}{let e=n;if(0===e.length)return[];const r={};for(let n=0;n1||"String"===o?xe(e,(function(e,r){if(null==e||e===h)return null==r||r===h?0:1;if(null==r||r===h)return-1;const t=T(e),n=T(r);return t