/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../chunks/tslib.es6","../../../core/Accessor","../../../core/Handles","../../../core/maybe","../../../core/reactiveUtils","../../../core/scheduling","../../../core/time","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","./index","./MemoryController","./StreamDataLoader","../../support/Scheduler"],(function(e,t,r,a,s,o,n,i,l,u,c,d,h,_,p,m,y,C){"use strict";function g(e){return new f.ResourceController({view:e})}var f;e.ResourceControllerMain=function(e){function r(){var t;return(t=e.apply(this,arguments)||this).updating=!1,t}return t._inheritsLoose(r,e),r}(a),r.__decorate([u.property({readOnly:!0})],e.ResourceControllerMain.prototype,"updating",void 0),e.ResourceControllerMain=r.__decorate([_.subclass("esri.views.3d.support.ResourceController")],e.ResourceControllerMain),function(a){let c=function(e){function r(){var t;return(t=e.apply(this,arguments)||this)._scheduler=null,t._memoryController=null,t._streamDataLoader=null,t._scheduleTask=C.ImmediateTask,t._cameraChangeTime=0,t._handles=new s,t._frameTask=null,t._state=C.State.IDLE,t}t._inheritsLoose(r,e);var a=r.prototype;return a.initialize=function(){this._cameraChangeTime=performance.now(),this._scheduler=C.newScheduler(),this._memoryController=m.newMemoryController(this.view),this._streamDataLoader=new y.StreamDataLoader,this._streamDataLoader.setup(p.maxDownloadSlots,p.downloadSlotsPerClient,this._scheduler),this._handles.add([n.watch((()=>{var e;return null==(e=this.view.state)?void 0:e.camera}),((e,t)=>this._cameraChangedHandler(e,t)),n.sync),n.watch((()=>this.view.stationary),(()=>this._stationaryChangedHandler()))]),this._frameTask=i.addFrameTask({update:e=>this._frame(e)}),this._scheduleTask=this._scheduler.registerTask(C.TaskPriority.RESOURCE_CONTROLLER)},a.destroy=function(){this._handles=o.destroyMaybe(this._handles),this._scheduleTask.remove(),this._frameTask=o.removeMaybe(this._frameTask),this._streamDataLoader=o.destroyMaybe(this._streamDataLoader),this._memoryController=o.destroyMaybe(this._memoryController),this._scheduler=o.destroyMaybe(this._scheduler)},a.schedule=function(e,t,r){return this._scheduleTask.schedule(e,t,r)},a.createStreamDataRequester=function(e){const t=this._streamDataLoader;return{request:(r,a,s)=>t.request(r,a,e,s),get busy(){return!t.hasDownloadSlots(e)}}},a._frame=function(e){this.view.suspended||this.view.stateManager&&(this.view.stateManager.step(l.secondsFromMilliseconds(e.deltaTime)),!this._scheduler)||(this._updateState(),this._scheduler.state=this._state,this._scheduler.updateBudget(e)?(this._memoryController.update(),this._scheduler.frame()):this._memoryController.updating&&this._memoryController.update())},a._cameraChangedHandler=function(e,t){e&&t&&e.almostEquals(t)||(this._cameraChangeTime=performance.now(),this._updateState(),this._scheduler.state=this._state)},a._stationaryChangedHandler=function(){this.memoryController.resetStableQuality()},a._updateState=function(){this.view.animation?this._state=C.State.ANIMATING:this.view.interacting?this._state=C.State.INTERACTING:(this._state===C.State.ANIMATING&&(this._cameraChangeTime=0),performance.now()-this._cameraChangeTime<=d?this._state=C.State.INTERACTING:this._state=C.State.IDLE)},t._createClass(r,[{key:"updating",get:function(){var e,t,r;return!!(null!=(e=this._memoryController)&&e.updating||null!=(t=this._streamDataLoader)&&t.updating||null!=(r=this._scheduleTask)&&r.updating)}},{key:"scheduler",get:function(){return this._scheduler}},{key:"memoryController",get:function(){return this._memoryController}},{key:"test",get:function(){return{getQueueStats:e=>this._streamDataLoader.test.loadQueue.getStatsForType(e),state:this._state}}}]),r}(e.ResourceControllerMain);r.__decorate([u.property({constructOnly:!0})],c.prototype,"view",void 0),r.__decorate([u.property()],c.prototype,"_scheduler",void 0),r.__decorate([u.property()],c.prototype,"_memoryController",void 0),r.__decorate([u.property()],c.prototype,"_streamDataLoader",void 0),r.__decorate([u.property()],c.prototype,"_scheduleTask",void 0),r.__decorate([u.property({readOnly:!0})],c.prototype,"updating",null),c=r.__decorate([_.subclass("esri.views.3d.support.ResourceController")],c),a.ResourceController=c;const d=300}(f||(f={})),e.newResourceController=g,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
