/*
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/collectionUtils","../../core/Evented","../../core/HandleOwner","../../core/Loadable","../../core/Logger","../../core/maybe","../../core/Promise","../../core/promiseUtils","../../core/reactiveUtils","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/ensureType","../../core/accessorSupport/decorators/subclass","../../geometry/Extent","../../geometry/projection","../../geometry/support/aaBoundingRect","../../geometry/support/intersectsBase","../graphics/data/BoundsStore","./ImageElement","./MediaElementBase","./mediaUtils","./VideoElement","../../geometry/SpatialReference"],(function(e,t,n,s,r,o,i,a,l,c,d,p,u,m,f,h,_,y,g,x,E,w,R,M,S,I,b,v){"use strict";const B={key:"type",defaultKeyValue:"image",base:S,typeMap:{image:M,video:b}},C=s.ofType(B);let V=function(t){function n(n){var s;return(s=t.call(this,n)||this)._index=new R.BoundsStore,s._elementViewsMap=new Map,s._elementsIndexes=new Map,s._elementsChangedHandler=t=>{for(const e of t.removed){const t=s._elementViewsMap.get(e);s._elementViewsMap.delete(e),s._index.delete(t),s.handles.remove(t),t.destroy(),s.notifyChange("fullExtent")}const{spatialReference:n}=e._assertThisInitialized(s);for(const e of t.added){if(s._elementViewsMap.get(e))continue;const t=new I.MediaElementView({spatialReference:n,element:e});s._elementViewsMap.set(e,t);const r=u.watch((()=>t.bounds),(()=>s._updateIndexForElement(t,!1)));s._updateIndexForElement(t,!0),s.handles.add(r,t)}s._elementsIndexes.clear(),s.elements.forEach(((e,t)=>s._elementsIndexes.set(e,t))),s.emit("refresh")},s.elements=new C,s}e._inheritsLoose(n,t);var s=n.prototype;return s.load=function(){var t=e._asyncToGenerator((function*(e){if(p.throwIfAborted(e),!this.spatialReference){const e=this.elements.find((e=>c.isSome(e.georeference)&&c.isSome(e.georeference.coords)));this._set("spatialReference",e?c.unwrap(c.unwrap(e.georeference).coords).spatialReference:v.WGS84)}return this._elementsChangedHandler({added:this.elements.items,removed:[]}),this.handles.add(this.elements.on("change",this._elementsChangedHandler)),this}));function n(e){return t.apply(this,arguments)}return n}(),s.destroy=function(){this._index.clear(),this._elementViewsMap.clear(),this._elementsIndexes.clear()},s.queryElements=function(){var t=e._asyncToGenerator((function*(e,t){yield this.load(),yield x.initializeProjection(e.spatialReference,this.spatialReference,null,t);const n=x.project(e,this.spatialReference);if(!n)return[];const s=n.normalize(),r=[];for(const o of s)this._index.forEachInBounds(E.fromExtent(o),(({normalizedCoords:e,element:t})=>{c.isSome(e)&&w.extentIntersectsPolygon(o,e)&&r.push(t)}));return r.sort(((e,t)=>this._elementsIndexes.get(e)-this._elementsIndexes.get(t))),r}));function n(e,n){return t.apply(this,arguments)}return n}(),s._updateIndexForElement=function(e,t){const n=e.normalizedBounds,s=this._index.has(e),r=c.isSome(n);this._index.delete(e),r&&this._index.set(e,n),this.notifyChange("fullExtent"),t||s===r||this.emit("refresh")},e._createClass(n,[{key:"elements",set:function(e){this._set("elements",r.referenceSetter(e,this._get("elements"),C))}},{key:"fullExtent",get:function(){if("not-loaded"===this.loadStatus)return null;const e=this._index.fullBounds;return c.isNone(e)?null:new g({xmin:e[0],ymin:e[1],xmax:e[2],ymax:e[3],spatialReference:this.spatialReference})}},{key:"spatialReference",set:function(e){"not-loaded"===this.loadStatus?this._set("spatialReference",e):l.getLogger(this.declaredClass).error("#spatialReference","spatialReference cannot be changed after the source is loaded.")}}]),n}(a.LoadableMixin(d.EsriPromiseMixin(i.HandleOwnerMixin(o.EventedAccessor))));t.__decorate([m.property()],V.prototype,"elements",null),t.__decorate([m.property({readOnly:!0})],V.prototype,"fullExtent",null),t.__decorate([m.property()],V.prototype,"spatialReference",null),V=t.__decorate([y.subclass("esri.layers.support.LocalMediaElementSource")],V);return V}));
