/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{removeUnordered as e}from"../../../../core/arrayUtils.js";import{toConst as t}from"../../../../core/compilerUtils.js";import{EventEmitter as s}from"../../../../core/Evented.js";import i from"../../../../core/Handles.js";import"../../../../core/has.js";import{destroyMaybe as o}from"../../../../core/maybe.js";import{generateUID as r}from"../../../../core/uid.js";import{DirtyEventNames as h}from"./DirtyEvents.js";import{Octree as c}from"./Octree.js";class d{constructor(e,t,o=""){this.stage=e,this.apiLayerViewUid=o,this.id=r(),this.events=new s,this.visible=!0,this.sliceable=!1,this._objectsAdded=new Array,this._handles=new i,this._objects=new Map,this._pickable=!0,this.visible=t?.visible??!0,this._pickable=t?.pickable??!0,this.updatePolicy=t?.updatePolicy??0,e.addLayer(this);for(const s of h)this._handles.add(this.events.on(s,t=>e.handleEvent(s,t)))}destroy(){this._handles.size&&(this._handles.destroy(),this.stage.removeLayer(this),this.invalidateSpatialQueryAccelerator())}get objects(){return this._objects}getObject(e){return t(this._objects.get(e))}set pickable(e){this._pickable=e}get pickable(){return this._pickable&&this.visible}add(e){this._objects.set(e.id,e),e.layer=this,this.events.emit("layerObjectAdded",e),null!=this._octree&&this._objectsAdded.push(e)}remove(t){this._objects.delete(t.id)&&(this.events.emit("layerObjectRemoved",t),t.layer=null,null!=this._octree&&(e(this._objectsAdded,t)||this._octree.remove([t])))}addMany(e){for(const t of e)this._objects.set(t.id,t),t.layer=this;this.events.emit("layerObjectsAdded",e),null!=this._octree&&this._objectsAdded.push(...e)}removeMany(t){const s=new Array;for(const e of t)this._objects.delete(e.id)&&s.push(e);if(0!==s.length&&(this.events.emit("layerObjectsRemoved",s),s.forEach(e=>e.layer=null),null!=this._octree)){for(let t=0;t<s.length;)e(this._objectsAdded,s[t])?(s[t]=s[s.length-1],s.length-=1):++t;this._octree.remove(s)}}commit(){this.stage.commitLayer(this)}sync(){1!==this.updatePolicy&&this.stage.syncLayer(this.id)}notifyObjectBBChanged(e,t){null==this._octree||this._objectsAdded.includes(e)||this._octree.update(e,t)}getSpatialQueryAccelerator(){return null==this._octree&&this._objects.size>50?(this._octree=new c(e=>e.boundingVolumeWorldSpace.bounds),this._octree.add(this._objects.values())):null!=this._octree&&this._objectsAdded.length>0&&(this._octree.add(this._objectsAdded),this._objectsAdded.length=0),this._octree}invalidateSpatialQueryAccelerator(){this._octree=o(this._octree),this._objectsAdded.length=0}get test(){}}export{d as WebGLLayer};
