/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as e}from"tslib";import t from"../../core/Collection.js";import{referenceSetter as s}from"../../core/collectionUtils.js";import{EventedAccessor as n}from"../../core/Evented.js";import{LoadableMixin as r}from"../../core/Loadable.js";import o from"../../core/Logger.js";import{EsriPromiseMixin as i}from"../../core/Promise.js";import{throwIfAborted as l}from"../../core/promiseUtils.js";import{watch as a}from"../../core/reactiveUtils.js";import{property as m,subclass as d}from"../../core/accessorSupport/decorators.js";import c from"../../geometry/Extent.js";import{initializeProjection as p,project as h}from"../../geometry/projectionUtils.js";import f from"../../geometry/SpatialReference.js";import{fromExtent as u}from"../../geometry/support/aaBoundingRect.js";import{extentIntersectsPolygon as g}from"../../geometry/support/intersectsBase.js";import{equals as _}from"../../geometry/support/spatialReferenceUtils.js";import{BoundsStore as x}from"../graphics/data/BoundsStore.js";import y from"./ImageElement.js";import j from"./MediaElementBase.js";import{MediaElementView as R}from"./MediaElementView.js";import E from"./VideoElement.js";const w={key:"type",defaultKeyValue:"image",base:j,typeMap:{image:y,video:E}},M=t.ofType(w);let I=class extends(r(i(n))){constructor(e){super(e),this._index=new x,this._elementViewsMap=new Map,this._elementsIndexes=new Map,this._elementsChangedHandler=e=>{for(const s of e.removed){const e=this._elementViewsMap.get(s);this._elementViewsMap.delete(s),this._index.delete(e),this.removeHandles(e),e.destroy(),this.notifyChange("fullExtent")}const{spatialReference:t}=this;for(const s of e.added){if(this._elementViewsMap.get(s))continue;const e=new R({spatialReference:t,element:s});this._elementViewsMap.set(s,e);const n=a(()=>e.coords,()=>this._updateIndexForElement(e,!1));this._updateIndexForElement(e,!0),this.addHandles(n,e)}this._elementsIndexes.clear(),this.elements.forEach((e,t)=>this._elementsIndexes.set(e,t)),this.emit("refresh")},this.elements=new M}async load(e){if(l(e),!this.spatialReference){const e=this.elements.find(e=>null!=e.georeference?.coords);this._set("spatialReference",e?e.georeference.coords.spatialReference:f.WGS84)}return this._elementsChangedHandler({added:this.elements.items,removed:[]}),this.addHandles(this.elements.on("change",this._elementsChangedHandler)),this}destroy(){this._index.clear(),this._elementViewsMap.clear(),this._elementsIndexes.clear()}get elements(){return this._get("elements")}set elements(e){this._set("elements",s(e,this._get("elements"),M))}get fullExtent(){if("not-loaded"===this.loadStatus)return null;const e=this._index.fullBounds;return null==e?null:new c({xmin:e[0],ymin:e[1],xmax:e[2],ymax:e[3],spatialReference:this.spatialReference})}set spatialReference(e){"not-loaded"===this.loadStatus?this._set("spatialReference",e):o.getLogger(this).error("#spatialReference","spatialReference cannot be changed after the source is loaded.")}async queryElements(e,t){await this.load(),await p(e.spatialReference,this.spatialReference,null,t);const s=_(e.spatialReference,this.spatialReference)?e:h(e,this.spatialReference);if(!s)return[];const n=s.normalize(),r=[];for(const o of n)this._index.forEachInBounds(u(o),({normalizedCoords:e,element:t})=>{null!=e&&g(o,e)&&r.push(t)});return r.sort((e,t)=>this._elementsIndexes.get(e)-this._elementsIndexes.get(t)),r}hasElement(e){return this.elements.includes(e)}_updateIndexForElement(e,t){const s=e.normalizedBounds,n=this._index.has(e),r=null!=s;this._index.delete(e),r&&this._index.set(e,s),this.notifyChange("fullExtent"),t||(n!==r?this.emit("refresh"):this.emit("change",{element:e.element}))}};e([m()],I.prototype,"elements",null),e([m({readOnly:!0})],I.prototype,"fullExtent",null),e([m()],I.prototype,"spatialReference",null),I=e([d("esri.layers.support.LocalMediaElementSource")],I);export{I as default};
