/*
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/Evented","../../../../core/Logger","../../../../core/maybe","../../../../core/promiseUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/has","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","./basicInterfaces","./ContentObjectType","./TextureTechnique","./TextureTechniqueConfiguration","./Util","../../../support/Scheduler"],(function(e,t,r,o,s,n,i,u,a,c,p,h,l,d,_,f,T,g,x){"use strict";const y=n.getLogger("esri.views.3d.webgl-engine.lib.TextureRepository");e.TextureRepository=function(e){function r(t,r,o){var n;return(n=e.call(this,{})||this)._stage=t,n._techniqueRepository=r,n._rctx=o,n._textures=new Map,n._loadingCount=0,n._frameUpdates=new Map,n.events=new s,n._frameTask=t.resourceController.scheduler.registerTask(x.TaskPriority.TEXTURE_UNLOAD),n}t._inheritsLoose(r,e);var o=r.prototype;return o.normalizeCtorArgs=function(){return{}},o.dispose=function(){this._frameTask.remove(),this._stage.forEachOfType(_.ContentObjectType.Texture,(e=>e.unload()))},o.acquire=function(e){const t=this._textures.get(e);return t?(t.ref(),i.unwrapOr(t.loadingPromise,t)):this._createNewRef(e)},o.update=function(){let e=!1;this._frameUpdates.forEach((t=>{const r=t.texture.frameUpdate(this._rctx,this.textureTechnique,t.previousToken);r>=0&&r!==t.previousToken&&(t.previousToken=r,e=!0)})),e&&this.events.emit("changed",d.RenderRequestType.BACKGROUND)},o._createNewRef=function(e){const t=this._stage.getObject(e);if(i.isNone(t))return g.assert(void 0!==t),null;const r=t.events.on("unloaded",(()=>{r.remove(),this._onTextureUnloaded(e)})),o=new m(e,(()=>{this._frameTask.schedule((()=>{o.isUnreferenced&&t.unload()}))}));return this._textures.set(e,o),o.ref(),i.isSome(t.glTexture)?(this._updateGLTexture(o,t.glTexture),t.requiresFrameUpdates&&this._frameUpdates.set(e,{texture:t,previousToken:-1}),o):(this._loadingCount++,o.loadingPromise=this._stage.schedule((()=>{const r=t.load(this._rctx,(()=>this.textureTechnique)),s=r=>(this._loadingCount--,o.loadingPromise=null,this._updateGLTexture(o,r),t.requiresFrameUpdates&&this._frameUpdates.set(e,{texture:t,previousToken:-1}),o),n=e=>(this._loadingCount--,o.loadingPromise=null,u.isAbortError(e)||y.error(e),null);return u.isPromiseLike(r)?r.then(s,n):s(r)})),o.loadingPromise)},o._updateGLTexture=function(e,t){e.glTexture=t,this.events.emit("changed",d.RenderRequestType.UPDATE)},o._onTextureUnloaded=function(e){this._textures.delete(e),this._frameUpdates.delete(e)},t._createClass(r,[{key:"updating",get:function(){return this._loadingCount>0||this._frameTask.updating}},{key:"textureTechnique",get:function(){return i.isNone(this._textureTechnique)&&(this._textureTechnique=this._techniqueRepository.acquire(f.TextureTechnique,new T.TextureTechniqueConfiguration)),this._textureTechnique}}]),r}(o),r.__decorate([a.property()],e.TextureRepository.prototype,"_loadingCount",void 0),r.__decorate([a.property()],e.TextureRepository.prototype,"_frameTask",void 0),r.__decorate([a.property()],e.TextureRepository.prototype,"updating",null),e.TextureRepository=r.__decorate([l.subclass("esri.views.3d.webgl-engine.lib.TextureRepository")],e.TextureRepository);let m=function(){function e(e,t){this.id=e,this._release=t,this._refCount=0}var r=e.prototype;return r.ref=function(){++this._refCount},r.release=function(){--this._refCount,this._refCount>0||(0!==this._refCount?(y.error("Cannot dereference texture that has no references!"),this._refCount=0):this._release())},t._createClass(e,[{key:"isUnreferenced",get:function(){return 0===this._refCount}}]),e}();Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
