/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import t from"./Error.js";import{once as n}from"./events.js";import r from"./Logger.js";import{removeMaybe as e}from"./maybe.js";async function o(t,n){const r=t.slice(),e=await Promise.all(t.map((t,r)=>n(t,r)));return r.filter((t,n)=>e[n])}function i(n="Aborted"){return new t("AbortError",n)}function u(t,n="Aborted"){if(c(t))throw i(n)}function s(t,n="Aborted"){if(t?.destroyed)throw i(n)}function l(t){return t instanceof AbortSignal?t:t?.signal??void 0}function c(t){const n=l(t);return null!=n&&n.aborted}function a(t){if(p(t))throw t}function f(t){if(!p(t))throw t}function m(t,r){const e=l(t);if(null!=e){if(!e.aborted)return n(e,"abort",()=>r());r()}}function w(t,r){const e=l(t);if(null!=e)return u(e),n(e,"abort",()=>r(i()))}function h(t,n){return null==l(n)?t:new Promise((r,o)=>{let u=m(n,()=>o(i()));const s=()=>{u=e(u)};t.then(s,s),t.then(r,o)})}function b(n,r,e){return Promise.race([n,A(r).then(()=>{throw new t("timeout",`Did not resolve within ${r} milliseconds (${e??"timeout"})`)})])}function p(t){return null!=t&&"object"==typeof t&&"name"in t&&"AbortError"===t.name}async function d(t){try{return await t}catch(n){if(!p(n))throw n;return}}async function y(t,n=r.getLogger("esri")){try{return await t}catch(e){p(e)||n.error(e)}}async function v(t){if(!t)return;if("function"!=typeof t.forEach){const n=Object.keys(t),r=n.map(n=>t[n]),e=await v(r),o={};return n.map((t,n)=>o[t]=e[n]),o}const n=t;return Promise.allSettled(n).then(t=>Array.from(n,(n,r)=>{const e=t[r];return"fulfilled"===e.status?{promise:n,value:e.value}:{promise:n,error:e.reason}}))}async function P(t){return(await v(t)).filter(t=>!!t.value).map(t=>t.value)}async function g(t){return(await Promise.allSettled(t)).filter(t=>"fulfilled"===t.status).map(t=>t.value)}async function j(t){return(await Promise.allSettled(t)).filter(t=>"rejected"===t.status).map(t=>t.reason)}function A(t,n=void 0,r){const e=new AbortController;return m(r,()=>e.abort()),new Promise((r,o)=>{let u=setTimeout(()=>{u=0,r(n)},t);m(e,()=>{u&&(clearTimeout(u),o(i()))})})}function T(n,r,e,o){const i=e&&"abort"in e?e:null;null!=o||i||(o=e);let u=setTimeout(()=>{u=0,i?.abort()},r);const s=()=>o||new t("promiseUtils:timeout","The wrapped promise did not resolve within "+r+" ms");return n.then(t=>{if(0===u)throw s();return clearTimeout(u),t},t=>{throw clearTimeout(u),0===u?s():t})}function E(t,n){const r=new AbortController,e=setTimeout(()=>r.abort(),n);return m(t,()=>{r.abort(),clearTimeout(e)}),{...t,signal:r.signal}}function S(t){return t&&"function"==typeof t.then}function C(t){return S(t)?t:Promise.resolve(t)}function L(t,n=-1){let r,e,o,u,s=null;const l=(...c)=>{if(r){e=c,u&&u.reject(i()),u=Promise.withResolvers();const t=u.promise;if(s){const t=s;s=null,t.abort()}return t}if(o=u||Promise.withResolvers(),u=null,n>0){const e=new AbortController;r=C(t(...c,e.signal));const o=r;A(n).then(()=>{r===o&&(u?e.abort():s=e)})}else r=1,r=C(t(...c));const a=()=>{const t=e;e=o=r=s=null,null!=t&&l(...t)},f=r,m=o;return f.then(a,a),f.then(m.resolve,m.reject),m.promise};return l}function R(t,n){return t.then(n,n)}function $(t,n){n.then(t.resolve,t.reject)}async function k(t){await Promise.resolve(),u(t)}export{A as after,j as allSettledErrors,g as allSettledValues,R as always,i as createAbortError,L as debounce,v as eachAlways,P as eachAlwaysValues,o as filter,d as ignoreAbortErrors,p as isAbortError,c as isAborted,S as isPromiseLike,y as logOnError,m as onAbort,w as onAbortOrThrow,$ as settleWithPromise,l as signalFromSignalOrOptions,a as throwIfAbortError,u as throwIfAborted,s as throwIfDestroyed,f as throwIfNotAbortError,T as timeout,k as waitTick,C as when,h as whenOrAbort,b as whenOrTimeout,E as wrapAbortWithTimeout};
