/*
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","../../../geometry","../../../core/Collection","../../../core/Logger","../../../core/MapUtils","../../../core/maybe","../../../core/promiseUtils","../../../core/reactiveUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","../../../geometry/support/aaBoundingRect","../../../geometry/support/contains","../../../layers/support/mediaUtils","../tiling/PagedTileQueue","../tiling/TileInfoView","../tiling/TileKey","../tiling/TileQueue","../tiling/TileStrategy","../engine/webgl/Overlay","../engine/webgl/OverlayContainer","./LayerView2D","../../layers/LayerView","../../../geometry/Extent"],(function(e,t,r,i,n,s,o,l,a,c,u,h,p,y,d,f,m,_,g,v,w,T,C,S,E,R,q){"use strict";let U=function(t){function r(){var e;return(e=t.apply(this,arguments)||this)._overlayContainer=null,e._fetchQueue=null,e._tileStrategy=null,e._elementReferences=new Map,e.layer=null,e.elements=new i,e}e._inheritsLoose(r,t);var c=r.prototype;return c.attach=function(){this.handles.add(a.on((()=>this.layer.source),"refresh",(()=>{for(const e of this._tileStrategy.tiles)this._updateTile(e);this.requestUpdate()}))),this._overlayContainer=new S,this.container.addChild(this._overlayContainer),this._fetchQueue=new w({tileInfoView:this.view.featuresTilingScheme,concurrency:10,process:(e,t)=>this._queryElements(e,t)}),this._tileStrategy=new T({cachePolicy:"purge",resampling:!0,acquireTile:e=>this._acquireTile(e),releaseTile:e=>this._releaseTile(e),tileInfoView:this.view.featuresTilingScheme}),this.requestUpdate()},c.detach=function(){this.handles.removeAll(),this.elements.removeAll(),this._tileStrategy.destroy(),this._fetchQueue.destroy(),this._overlayContainer.removeAllChildren(),this.container.removeAllChildren(),this._elementReferences.clear()},c.supportsSpatialReference=function(e){return!0},c.moveStart=function(){this.requestUpdate()},c.viewChange=function(){this.requestUpdate()},c.moveEnd=function(){this.requestUpdate()},c.update=function(e){this._tileStrategy.update(e)},c.hitTest=function(){var t=e._asyncToGenerator((function*(e,t){const r=[],i=[e.x,e.y];for(const s of this.elements){var n;const t=null==(n=o.unwrap(s.georeference))?void 0:n.coords;o.isSome(t)&&f.ringsContainsCoords(t.rings,i)&&r.push({type:"media",element:s,layer:this.layer,mapPoint:e})}return r.reverse()}));function r(e,r){return t.apply(this,arguments)}return r}(),c.canResume=function(){return null!=this.layer.source&&t.prototype.canResume.call(this)},c.doRefresh=function(){var t=e._asyncToGenerator((function*(){}));function r(){return t.apply(this,arguments)}return r}(),c._acquireTile=function(e){const t=new V(e.clone());return this._updateTile(t),t},c._updateTile=function(e){this.updatingHandles.addPromise(this._fetchQueue.push(e.key).then((t=>{const[r,i]=e.setElements(t);this._acquireElements(e,r),this._releaseElements(e,i),this.requestUpdate()}),(e=>{l.isAbortError(e)||n.getLogger(this.declaredClass).error(e)})))},c._releaseTile=function(e){this._fetchQueue.abort(e.key.id),e.elements&&this._releaseElements(e,e.elements),this.requestUpdate()},c._queryElements=function(){var t=e._asyncToGenerator((function*(e,t){const r=this.layer.source;if(o.isNone(r))return[];this.view.featuresTilingScheme.getTileBounds(b,e,!0);const i=new q({xmin:b[0],ymin:b[1],xmax:b[2],ymax:b[3],spatialReference:this.view.spatialReference});return r.queryElements(i,t)}));function r(e,r){return t.apply(this,arguments)}return r}(),c._acquireElements=function(e,t){const r=this.layer.source,i=this.view.spatialReference;if(!o.isNone(r))for(const n of t){s.getOrCreateMapValue(this._elementReferences,n.uid,(()=>{const e=new m.MediaElementView({element:n,spatialReference:i}),t=new C(e);return this._overlayContainer.addChild(t),this.elements.add(n),{tiles:new Set,projectedElement:e,overlay:t}})).tiles.add(e)}},c._releaseElements=function(e,t){for(const r of t){const t=this._elementReferences.get(r.uid);t.tiles.delete(e),t.tiles.size||(this._overlayContainer.removeChild(t.overlay),t.overlay.destroy(),t.projectedElement.destroy(),this._elementReferences.delete(r.uid),this.elements.remove(r))}},r}(E.LayerView2DMixin(R));t.__decorate([c.property()],U.prototype,"_fetchQueue",void 0),t.__decorate([c.property()],U.prototype,"layer",void 0),t.__decorate([c.property({readOnly:!0})],U.prototype,"elements",void 0),U=t.__decorate([y.subclass("esri.views.2d.layers.MediaLayerView2D")],U);const b=d.create();let V=function(){function e(e){this.key=e,this.elements=null,this.isReady=!1,this.visible=!0}return e.prototype.setElements=function(e){const t=[],r=new Set(this.elements);this.elements=e;for(const i of e)r.has(i)?r.delete(i):t.push(i);return this.isReady=!0,[t,Array.from(r)]},e}();return U}));
