/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as s}from"tslib";import e from"./Accessor.js";import{abortHandle as i}from"./handleUtils.js";import{createResolver as r,createAbortError as t,isAbortError as o}from"./promiseUtils.js";import{subclass as l}from"./accessorSupport/decorators/subclass.js";class h{constructor(){this._resolver=r(),this._status=0,this._resolvingPromises=[],this._resolver.promise.then(()=>{this._status=1,this._cleanUp()},()=>{this._status=2,this._cleanUp()}),this.promise=this._resolver.promise}destroy(){this._cleanUp()}addResolvingPromise(s){this._resolvingPromises.push(s),this._tryResolve()}isResolved(){return 1===this._status}isRejected(){return 2===this._status}isFulfilled(){return 0!==this._status}abort(){this._resolver.reject(t())}_cleanUp(){this._allPromise=null,this._resolvingPromises=null}_tryResolve(){if(this.isFulfilled())return;const s=r(),e=[...this._resolvingPromises,s.promise],i=this._allPromise=Promise.all(e);i.then(()=>{this.isFulfilled()||this._allPromise!==i||this._resolver.resolve()},s=>{this.isFulfilled()||this._allPromise!==i||o(s)||this._resolver.reject(s)}),s.resolve()}}const m=e=>{const r=e;let t=class extends r{constructor(...s){super(...s),this._promiseProps=new h,this.addResolvingPromise(Promise.resolve())}destroy(){this._promiseProps.destroy()}isResolved(){return this._promiseProps.isResolved()}isRejected(){return this._promiseProps.isRejected()}isFulfilled(){return this._promiseProps.isFulfilled()}when(s,e){return this._promiseProps.promise.then(()=>this).then(s,e)}catch(s){return this.when(null,s)}addResolvingPromise(s){s&&!this._promiseProps.isFulfilled()&&this._promiseProps.addResolvingPromise("_promiseProps"in s?s.when():s)}addResolvingTask(s){s&&!this._promiseProps.isFulfilled()&&(this.addHandles(i(s)),this._promiseProps.addResolvingPromise(s.promise))}};return t=s([l("esri.core.Promise")],t),t},n=m(e);export{n as EsriPromise,m as EsriPromiseMixin};
