/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{removeMaybe as e}from"../../../../core/maybe.js";import{addFrameTask as s}from"../../../../core/scheduling.js";class o{constructor(){this.lodCrossfadeSignedDuration=0}}class d{constructor(e){this._view=e,this._preRenderFrameTaskHandle=null,this._currentFrameStartTime=null,this._numFadingNodes=0}destroy(){this._preRenderFrameTaskHandle?.remove(),this._preRenderFrameTaskHandle=null,this._view=null}get updating(){return this._numFadingNodes>0}stopNodeFading(s){null!=s.lodCrossfadeProgress&&(this._numFadingNodes--,s.lodCrossfadeProgress=null,0===this._numFadingNodes&&(null!=this._preRenderFrameTaskHandle&&(this._preRenderFrameTaskHandle=e(this._preRenderFrameTaskHandle)),this._view.notifyLODUpdate(),this._view.notifyUpdate()))}_startNodeFading(e,o,d){0===this._numFadingNodes&&(this._preRenderFrameTaskHandle=s({preRender:e=>this._updateAllNodeFading(e)}),this._view.notifyLODUpdate()),null==e.lodCrossfadeProgress&&(this._numFadingNodes++,this._view.notifyUpdate()),e.lodCrossfadeSignedDuration=d,e.lodCrossfadeProgress=o}_updateAllNodeFading(e){const s=this._view.nodeCrossfadingEnabled;this._view.foreachCrossfadeNode((o,d)=>{if(null!=o?.lodCrossfadeProgress){const i=o.lodCrossfadeSignedDuration,t=i>0?this._view.fullOpacity:0,r=e.deltaTime/i,a=o.lodCrossfadeProgress+Math.abs(r),n=!s||a>=1||0===i,l=t-(n?0:i>0?1:-1)*(1-a);n?(this.stopNodeFading(o),i<0&&this._view.markNodeToRemove(d)):o.lodCrossfadeProgress=a,this._view.setNodeOpacityByIndex(d,l)}}),this._view.removeMarkedNodes()}stopAllNodeFading(){this._view.foreachCrossfadeNode((e,s)=>{if(null!=e?.lodCrossfadeProgress){this.stopNodeFading(e);const o=e.lodCrossfadeSignedDuration;o<0&&this._view.markNodeToRemove(s);const d=o>0?this._view.fullOpacity:0;this._view.setNodeOpacityByIndex(s,d)}}),this._view.removeMarkedNodes()}fadeNode(e,s,o,d){null==this._currentFrameStartTime&&(this._currentFrameStartTime=Date.now());const t=this._view,r=t.nodeCrossfadingEnabled,a=0===o?t.fullOpacity:0,n=r?d?0===o?t.lodCrossfadeinDuration:t.lodCrossfadeoutDuration:t.lodCrossfadeUncoveredDuration:0,l=this._view.getNodeOpacityByIndex(e);if(r&&l!==a&&n>0){const e=1-Math.abs(a-l);this._startNodeFading(s,e,i(o)*n)}else this.stopNodeFading(s),this._view.setNodeOpacityByIndex(e,a),1===o&&this._view.removeNode(e)}isNodeFullyFadedIn(e){const s=this._view.getNodeCrossfadeMetaData(e);return null==s||null==s.lodCrossfadeProgress&&this._view.getNodeOpacityByIndex(e)===this._view.fullOpacity}}function i(e){return 0===e?1:-1}export{d as I3SCrossfadeHelper,o as NodeCrossfadeMetaData};
