/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{destroyMaybe as t}from"../../../core/maybe.js";class e{constructor(t){this._getFadeDuration=t,this._fadeStart=0,this._delayedTime=0}clear(){this._current=t(this._current),this._next=t(this._next),this._waiting=t(this._waiting),this._delayed=t(this._delayed)}get current(){if(null==this._current)return null;if(!this._isFadingEnabled){let t=null;this._delayed?(t=this._delayed,this._delayed=null):this._waiting?(t=this._waiting,this._waiting=null):this._next&&(t=this._next,this._next=null),t&&(this.clear(),this._current=t)}let i=e.test.fadeMoment;if(this._delayed&&(i=i||performance.now(),i>=this._delayedTime&&(this._push(this._delayed,0),this._delayed=null)),this._next){i=i||performance.now();const e=this._fadeDuration,n=null!=this._current&&this._next.texture===this._current.texture,s=0!==this._next.type,r=i-this._fadeStart>=e;(n||s||r)&&(t(this._current),this._current=this._next,this._next=this._waiting,this._waiting=null,this._fadeStart=this._alignFadeStart(i))}return this._current}get next(){return this._next}get fadeFactor(){if(null==this._next)return 1;const t=e.test.fadeMoment||performance.now(),i=Math.max(0,t-this._fadeStart),n=this._fadeDuration;return i>n?0:1-i/n}get isFading(){return null!=this._next||null!=this._delayed}push(e,i=0){this._delayed=t(this._delayed),this._push(e,i)}_push(i,n){if(this._isFadingEnabled||this.clear(),null==this._current)return void(this._current=i);const s=this._waiting??this._next??this._current;if(i?.equals(s))return void i.destroy();const r=e.test.fadeMoment||performance.now();return 0!==n?(this._delayed=i,void(this._delayedTime=r+n)):null==this._next?(this._next=i,void(this._fadeStart=this._alignFadeStart(r))):void(null!=i&&(t(this._waiting),this._waiting=i))}get _fadeDuration(){const t=this._getFadeDuration();return this._waiting?.5*t:t}_alignFadeStart(t){const e=this._getFadeDuration();return t+e-t%e}get _isFadingEnabled(){return this._getFadeDuration()>0}static{this.test={fadeMoment:0}}}export{e as TextureFader};
