/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.24/esri/copyright.txt for details. */ import n from"../arcade/FunctionWrapper.js";import r from"../arcade/ImmutableArray.js";import{p as t,f as o,B as e,C as i,n as a,l as u,v as c,m as s,i as f,N as l,S as d}from"./languageUtils.js";import{isPromiseLike as h}from"../core/promiseUtils.js";function w(w){function m(r,t){if(t instanceof n)return w.arcadeCustomFunctionHandler(t);if(t instanceof l)return(...n)=>t.fn(r,{preparsed:!0,arguments:n});if(t instanceof d)return(...n)=>{if(n.length!==t.paramCount)throw new Error("Invalid parameters");return t.fn(...n)};throw new Error("Invalid Parameter")}function g(n){if(n instanceof r)return n.toArray();if(u(n))return n;throw new Error("Invalid Parameter")}w.functions.array=function(n,r){return w.standardFunction(n,r,(function(n,r,a){t(a,1,2);const u=o(a[0]);if(isNaN(u)||!1===e(u))throw new Error("Invalid Parameter");const c=i(a[1],null),s=new Array(u);return s.fill(c),s}))},w.functions.front=function(n,r){return w.standardFunction(n,r,(function(n,r,o){if(t(o,1,1),a(o[0])){if(o[0].length()<=0)throw new Error("Array is empty");return o[0].get(0)}if(u(o[0])){if(o[0].length<=0)throw new Error("Array is empty");return o[0][0]}throw new Error("Invalid Parameter")}))},w.functions.back=function(n,r){return w.standardFunction(n,r,(function(n,r,o){if(t(o,1,1),a(o[0])){if(o[0].length()<=0)throw new Error("Array is empty");return o[0].get(o[0].length()-1)}if(u(o[0])){if(o[0].length<=0)throw new Error("Array is empty");return o[0][o[0].length-1]}throw new Error("Invalid Parameter")}))},w.functions.push=function(n,r){return w.standardFunction(n,r,(function(n,r,o){if(t(o,1,2),u(o[0]))return o[0][o[0].length]=o[1],o[0].length;throw new Error("Invalid Parameter")}))},w.functions.pop=function(n,r){return w.standardFunction(n,r,(function(n,r,o){if(t(o,1,1),u(o[0])){if(o[0].length<=0)throw new Error("Array is empty");const n=o[0][o[0].length-1];return o[0].length=o[0].length-1,n}throw new Error("Invalid Parameter")}))},w.functions.erase=function(n,r){return w.standardFunction(n,r,(function(n,r,i){if(t(i,2,2),u(i[0])){let n=o(i[1]);if(isNaN(n)||!1===e(n))throw new Error("Invalid Parameter");const r=i[0];if(r.length<=0)throw new Error("Array is empty");if(n<0&&(n=r.length+n),n<0)throw new Error("Element not found");if(n>=r.length)throw new Error("Index is greater than array");return r.splice(n,1),c}throw new Error("Invalid Parameter")}))},w.functions.insert=function(n,r){return w.standardFunction(n,r,(function(n,r,i){if(t(i,3,3),u(i[0])){const n=o(i[1]);if(isNaN(n)||!1===e(n))throw new Error("Invalid Parameter");const r=i[2],t=i[0];if(n>t.length)throw new Error("Index is greater than array");if(n<0&&n<-1*t.length)throw new Error("Index is greater than array");return n===t.length?(t[n]=r,c):(t.splice(n,0,r),c)}throw new Error("Invalid Parameter")}))},w.functions.resize=function(n,r){return w.standardFunction(n,r,(function(n,r,a){if(t(a,2,3),u(a[0])){const n=o(a[1]);if(isNaN(n)||!1===e(n))throw new Error("Invalid Parameter");if(n<0)throw new Error("Size cannot be negative");const r=i(a[2],null),t=a[0];if(t.length>=n)return t.length=n,c;const u=t.length;t.length=n;for(let o=u;os(r,n)))>-1}if(a(o[0])){const n=o[1];return o[0].toArray().findIndex((r=>s(r,n)))>-1}throw new Error("Invalid Parameter")}))},w.functions.slice=function(n,r){return w.standardFunction(n,r,(function(n,r,c){if(t(c,1,3),u(c[0])){const n=o(i(c[1],0)),r=o(i(c[2],c[0].length));if(isNaN(n)||!1===e(n))throw new Error("Invalid Parameter");if(isNaN(r)||!1===e(r))throw new Error("Invalid Parameter");return c[0].slice(n,r)}if(a(c[0])){const n=c[0],r=o(i(c[1],0)),t=o(i(c[2],n.length()));if(isNaN(r)||!1===e(r))throw new Error("Invalid Parameter");if(isNaN(t)||!1===e(t))throw new Error("Invalid Parameter");return n.toArray().slice(r,t)}throw new Error("Invalid Parameter")}))},w.functions.splice=function(n,r){return w.standardFunction(n,r,(function(n,r,t){const o=[];for(let e=0;e{const t=i(n,r);return n=void 0!==t&&t!==c?t:null})):a.reduce(((n,r)=>{const t=i(n,r);return n=void 0!==t&&t!==c?t:null}),e[2])}))},w.functions.map=function(n,r){return w.standardFunction(n,r,(function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]),u=[];for(const n of a){const r=i(n);void 0!==r&&r!==c?u.push(r):u.push(null)}return u}))},w.functions.filter=function(n,r){return w.standardFunction(n,r,(function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]),u=[];for(const n of a){!0===i(n)&&u.push(n)}return u}))}),"async"===w.mode&&(w.functions.any=function(n,r){return w.standardFunctionAsync(n,r,(async function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]);for(const n of a){const r=await i(n);let t=null;if(t=h(t)?await r:r,f(t)&&!0===t)return!0}return!1}))},w.functions.all=function(n,r){return w.standardFunctionAsync(n,r,(async function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]);for(const n of a){const r=await i(n);let t=null;if(t=h(t)?await r:r,!0!==t)return!1}return!0}))},w.functions.none=function(n,r){return w.standardFunctionAsync(n,r,(async function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]);for(const n of a){const r=await i(n);let t=null;if(t=h(t)?await r:r,!0===t)return!1}return!0}))},w.functions.filter=function(n,r){return w.standardFunctionAsync(n,r,(async function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]),u=[];for(const n of a){const r=await i(n);let t=null;t=h(t)?await r:r,!0===t&&u.push(n)}return u}))},w.functions.reduce=function(n,r){return w.standardFunctionAsync(n,r,(function(r,o,e){t(e,2,3);const a=m(n,e[1]),u=g(e[0]);let s=null;if(e.length>2){const n=i(e[2],null);s=u.reduce((async(n,r)=>{let t=await n;return void 0!==t&&t!==c||(t=null),a(t,r)}),Promise.resolve(n))}else{if(0===u.length)return null;s=u.reduce((async(n,r,t)=>{if(t<=1)return a(n,r);let o=await n;return void 0!==o&&o!==c||(o=null),a(o,r)}))}return s.then((n=>void 0!==n&&n!==c?n:null))}))},w.functions.map=function(n,r){return w.standardFunctionAsync(n,r,(async function(r,o,e){t(e,2,2);const i=m(n,e[1]),a=g(e[0]),u=[];for(const n of a){const r=await i(n);let t=null;t=h(t)?await r:r,void 0!==t&&t!==c?u.push(t):u.push(null)}return u}))})}const m=Object.freeze(Object.defineProperty({__proto__:null,registerFunctions:w},Symbol.toStringTag,{value:"Module"}));export{m as A,w as r};