"use strict";var T=Object.defineProperty;var C=(u,m,t)=>m in u?T(u,m,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[m]=t;var S=(u,m,t)=>(C(u,typeof m!="symbol"?m+"":m,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class l extends Promise{constructor(t,e){const c=e?null:new AbortController,n=e||c.signal,o=[],r=()=>{for(const s of o)clearTimeout(s)};let i;const a={signal:n,cancel:()=>{var s;return(s=this.controller)==null?void 0:s.abort()},onCancel:s=>{i=s},delay:(s,f)=>new l(w=>{o.push(setTimeout(()=>{w(s())},f))},n),sleep:s=>a.delay(()=>{},s),fetch:(s,f)=>new l((w,j)=>{fetch(s,{...f||{},signal:n}).then(y=>w(y)).catch(y=>{y.name!=="AbortError"&&j(y)})},n),futurizable:s=>new l((f,w)=>{s.then(f).catch(w)},n)};let d="pending";const g=new Promise((s,f)=>{if(n.aborted){r();return}else{const w=typeof n.onabort=="function"?n.onabort:()=>{};n.onabort=()=>{w(),r(),d==="pending"&&i&&i()},t(p=>{d="fulfilled",s(p)},p=>{d="rejected",f(p)},a)}});super((s,f)=>{g.then(w=>s(w)).catch(f)});S(this,"controller");S(this,"internalSignal");S(this,"idsTimeout");this.controller=c,this.internalSignal=n,this.idsTimeout=o}static get[Symbol.species](){return this}get[Symbol.toStringTag](){return"Futurable"}get signal(){return this.internalSignal}clearTimeout(){for(const t of this.idsTimeout)clearTimeout(t)}then(t,e){let c,n;const o=new l((r,i)=>{c=r,n=i},this.internalSignal);return o.controller=this.controller,super.then(r=>{var i;if((i=this.internalSignal)!=null&&i.aborted){this.clearTimeout();return}try{c(t?t(r):r)}catch(h){n(h)}},r=>{var i;if((i=this.internalSignal)!=null&&i.aborted){this.clearTimeout();return}try{e?c(e(r)):n(r)}catch(h){n(h)}}),o}catch(t){return this.then(null,t)}finally(t){return this.then(e=>(t(),e),e=>{if(t(),e instanceof Error)throw e;return e})}cancel(){var t,e;!((t=this.internalSignal)!=null&&t.aborted)&&((e=this.controller)==null||e.abort())}delay(t,e){let c,n;const o=new l((r,i)=>{c=r,n=i},this.internalSignal);return o.controller=this.controller,this.then(r=>{this.idsTimeout.push(setTimeout(()=>c(t(r)),e))},r=>{n(r)}),o}sleep(t){return this.delay(e=>e,t)}fetch(t,e){let c,n;const o=new l((r,i)=>{c=r,n=i},this.internalSignal);return o.controller=this.controller,this.then(r=>{const i=typeof t=="function"?t(r):t,h={...typeof e=="function"?e(r):e,signal:this.internalSignal};fetch(i,h).then(a=>c(a)).catch(a=>{a.name!=="AbortError"&&n(a)})}),o}onCancel(t){let e,c;const n=new l((o,r,i)=>{i.onCancel(t),e=o,c=r},this.internalSignal);return n.controller=this.controller,this.then(o=>e(o),o=>c(o)),n}futurizable(t){let e,c;const n=new l((o,r)=>{e=o,c=r},this.internalSignal);return n.controller=this.controller,this.then(o=>{(typeof t=="function"?t(o):t).then(e).catch(c)}),n}static resolve(t,e){return t?new l(c=>c(t),e):new l(c=>c(),e)}static reject(t,e){return new l((c,n)=>n(t),e)}static onCancel({cb:t,signal:e}){return new l((c,n,o)=>{o.onCancel(()=>c(t()))},e)}static delay({cb:t,timer:e,signal:c}){return new l((n,o,r)=>{r.delay(t,e).then(n,o)},c)}static sleep({timer:t,signal:e}){return l.delay({cb:()=>{},timer:t,signal:e})}static fetch(t,e){const c=(e==null?void 0:e.signal)||void 0;return e!=null&&e.signal&&delete e.signal,new l((n,o,r)=>{r.fetch(t,e).then(n).catch(o)},c)}static futurizable({promise:t,signal:e}){return new l((c,n)=>{t.then(c).catch(n)},e)}static handleValues(t,e){const c=[];for(const n in t)t[n]instanceof l?c.push(t[n]):t[n]instanceof Promise?c.push(new l((o,r)=>{t[n].then(i=>o(i)).catch(r)},e)):c.push(new l(o=>o(t[n]),e));return c}static all(t,e){let c,n;const o=new l((i,h,a)=>{c=i,n=h,a.onCancel(()=>{for(const d of r)d.cancel()})},e);e||(e=o.internalSignal);const r=l.handleValues(t,e);return super.all(r).then(i=>c(i)).catch(i=>n(i)),o}static allSettled(t,e){let c;const n=new l((r,i,h)=>{c=r,h.onCancel(()=>{for(const a of o)a.cancel()})},e);e||(e=n.internalSignal);const o=l.handleValues(t,e);return super.allSettled(o).then(r=>c(r)),n}static race(t,e){let c,n;const o=new l((i,h,a)=>{c=i,n=h,a.onCancel(()=>{for(const d of r)d.cancel()})},e);e||(e=o.internalSignal);const r=l.handleValues(t,e);return super.race(r).then(i=>c(i)).catch(i=>n(i)),o}static any(t,e){let c,n;const o=new l((i,h,a)=>{c=i,n=h,a.onCancel(()=>{for(const d of r)d.cancel()})},e);e||(e=o.internalSignal);const r=l.handleValues(t,e);return super.any(r).then(i=>c(i)).catch(i=>n(i)),o}static polling(t,{interval:e,signal:c,immediate:n}){let o,r,i;n&&(o=new l((a,d,g)=>{g.onCancel(()=>{r&&r instanceof l&&r.cancel()});const s=t();(s instanceof l||s instanceof Promise)&&(r=s.then(()=>a()).catch(f=>i&&i(f)))},c));const h=setInterval(()=>{o&&o.cancel(),o=new l((a,d,g)=>{g.onCancel(()=>{r&&r instanceof l&&r.cancel()});const s=t();(s instanceof l||s instanceof Promise)&&(r=s.then(()=>a()).catch(f=>i&&i(f)))},c)},e);return{cancel:()=>{h&&clearInterval(h),o&&o.cancel()},catch:a=>{i=a}}}static withResolvers(t){let e,c,n;const o=new l((i,h,a)=>{e=i,c=h,n=a},t),r=o.cancel;return{resolve:e,reject:c,utils:n,cancel:r,promise:o}}}exports.Futurable=l;