/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../chunks/_rollupPluginBabelHelpers","../../../chunks/tslib.es6","../../../core/Logger","../../../core/promiseUtils","../../../core/reactiveUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","../../../geometry/support/spatialReferenceUtils","./BitmapTileLayerView2D","./LayerView2D","./support/imageUtils","../tiling/TileInfoView","../tiling/TileKey","../tiling/TileQueue","../tiling/TileStrategy","../../layers/LayerView","../../layers/RefreshableLayerView","../../layers/TileLayerView","../../support/drapedUtils"],(function(e,t,i,r,s,l,n,a,o,h,u,c,p,f,y,d,_,g,m,w,I,T){"use strict";const v=[0,0];let V=function(t){function l(){var e;return(e=t.apply(this,arguments)||this)._tileStrategy=null,e._fetchQueue=null,e.layer=null,e}e._inheritsLoose(l,t);var n=l.prototype;return n.update=function(e){this._fetchQueue.pause(),this._fetchQueue.state=e.state,this._tileStrategy.update(e),this._fetchQueue.resume()},n.attach=function(){const e="tileServers"in this.layer?this.layer.tileServers:null;this._tileInfoView=new y(this.layer.tileInfo,this.layer.fullExtent),this._fetchQueue=new _({tileInfoView:this._tileInfoView,concurrency:e&&10*e.length||10,process:(e,t)=>this.fetchTile(e,t)}),this._tileStrategy=new g({cachePolicy:"keep",resampling:this.resampling,acquireTile:e=>this.acquireTile(e),releaseTile:e=>this.releaseTile(e),tileInfoView:this._tileInfoView}),this.requestUpdate(),this.handles.add(s.watch((()=>this.resampling),(()=>{this.doRefresh()}))),t.prototype.attach.call(this)},n.detach=function(){t.prototype.detach.call(this),this._tileStrategy.destroy(),this._fetchQueue.clear(),this.container.removeAllChildren(),this._fetchQueue=this._tileStrategy=this._tileInfoView=null},n.moveStart=function(){this.requestUpdate()},n.viewChange=function(){this.requestUpdate()},n.moveEnd=function(){this.requestUpdate()},n.supportsSpatialReference=function(e){var t;return u.equals(null==(t=this.layer.tileInfo)?void 0:t.spatialReference,e)},n.createFetchPopupFeaturesQueryGeometry=function(e,t){return T.createQueryGeometry(e,t,this.view)},n.doRefresh=function(){var t=e._asyncToGenerator((function*(){this.updateRequested||this.suspended||(this._fetchQueue.reset(),this._tileStrategy.tiles.forEach((e=>this._enqueueTileFetch(e))))}));function i(){return t.apply(this,arguments)}return i}(),n.isUpdating=function(){var e,t;return null!=(e=null==(t=this._fetchQueue)?void 0:t.updating)&&e},n.acquireTile=function(e){const t=this._bitmapView.createTile(e),i=t.bitmap;return[i.x,i.y]=this._tileInfoView.getTileCoords(v,t.key),i.resolution=this._tileInfoView.getTileResolution(t.key),[i.width,i.height]=this._tileInfoView.tileInfo.size,this._enqueueTileFetch(t),this._bitmapView.addChild(t),this.requestUpdate(),t},n.releaseTile=function(e){this._fetchQueue.abort(e.key.id),this._bitmapView.removeChild(e),e.once("detach",(()=>e.destroy())),this.requestUpdate()},n.fetchTile=function(){var t=e._asyncToGenerator((function*(e,t={}){const i="tilemapCache"in this.layer?this.layer.tilemapCache:null,{signal:s,resamplingLevel:l=0}=t;if(!i)try{return yield this._fetchImage(e,s)}catch(o){if(!r.isAbortError(o)&&!this.resampling)return f.createBlankImage(this._tileInfoView.tileInfo.size);if(l<3){const i=this._tileInfoView.getTileParentId(e.id);if(i){const r=new d(i),s=yield this.fetchTile(r,{...t,resamplingLevel:l+1});return f.resampleImage(this._tileInfoView,s,r,e)}}throw o}const n=new d(0,0,0,0);let a;try{if(yield i.fetchAvailabilityUpsample(e.level,e.row,e.col,n,{signal:s}),n.level!==e.level&&!this.resampling)return f.createBlankImage(this._tileInfoView.tileInfo.size);a=yield this._fetchImage(n,s)}catch(o){if(r.isAbortError(o))throw o;a=yield this._fetchImage(e,s)}return this.resampling?f.resampleImage(this._tileInfoView,a,n,e):a}));function i(e){return t.apply(this,arguments)}return i}(),n._enqueueTileFetch=function(){var t=e._asyncToGenerator((function*(e){if(!this._fetchQueue.has(e.key.id)){try{const t=yield this._fetchQueue.push(e.key);e.bitmap.source=t,e.bitmap.width=this._tileInfoView.tileInfo.size[0],e.bitmap.height=this._tileInfoView.tileInfo.size[1],e.once("attach",(()=>this.requestUpdate()))}catch(t){r.isAbortError(t)||i.getLogger(this.declaredClass).error(t)}this.requestUpdate()}}));function s(e){return t.apply(this,arguments)}return s}(),n._fetchImage=function(){var t=e._asyncToGenerator((function*(e,t){return this.layer.fetchTile(e.level,e.row,e.col,{signal:t})}));function i(e,i){return t.apply(this,arguments)}return i}(),e._createClass(l,[{key:"resampling",get:function(){return!("resampling"in this.layer)||!1!==this.layer.resampling}}]),l}(I(w(c.BitmapTileLayerView2D(p.LayerView2DMixin(m)))));t.__decorate([l.property()],V.prototype,"_fetchQueue",void 0),t.__decorate([l.property()],V.prototype,"resampling",null),V=t.__decorate([h.subclass("esri.views.2d.layers.TileLayerView2D")],V);return V}));
