/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{destroyMaybe as t,removeMaybe as i}from"../../../../core/maybe.js";import{initial as s,watch as a,sync as r}from"../../../../core/reactiveUtils.js";import{subclass as l}from"../../../../core/accessorSupport/decorators.js";import{a as c}from"../../../../chunks/boundedPlane.js";import{Deconflictor as h,DeconflictorGraphic as n}from"./Deconflictor.js";import{LabelDeconflictor as o}from"./LabelDeconflictor.js";import{TaskPriority as d}from"../../../support/Scheduler.js";let p=class extends h{constructor(){super(...arguments),this._contexts=new Map,this.visibilityGroup=1,this._marginFactor=-.1,this.test={overrideMarginFactor:e=>{this._marginFactor=e,this.setDirty()},getNumVisibleLabels:()=>this._getNumVisible(16),getNumVisibleGraphics:()=>this._getNumVisible(1)}}get labels(){return this._labels}get viewState(){return this._viewState}initialize(){this._updatingHandles.add(()=>this.view?.state?.camera,()=>{this._updateViewState(),this.setDirty()}),this._updatingHandles.add(()=>this.view?.slice?.plane,()=>{this._updateSlicePlane(),this._slicePlaneChanged(),this.setDirty()},s),this.addHandles(a(()=>this.view.basemapTerrain?.updating||this.view.graphicsView?.updating||this.view.allLayerViews.some(e=>e.updating),(e,t)=>{!e&&t&&this.setDirty()},r)),this._frameTask=this.view.resourceController.scheduler.registerTask(d.GRAPHICS_DECONFLICTOR,this),this._labels=new o({view:this.view,parent:this})}destroy(){this._labels=t(this._labels),this._frameTask=i(this._frameTask)}get marginFactor(){return this._marginFactor}get usedMemory(){return super.usedMemory+this._labels.usedMemory}setDirty(){this._contexts.size>0&&(super.setDirty(),this._labels.setDirty())}runTask(e){const t=super.runTask(e);return this.readyToRun||this._labels.setDirty(),t}_updateViewState(){this.view?.state&&(this._viewState.camera.copyFrom(this.view.state.camera),this._updateSlicePlane())}_updateSlicePlane(){const e=this.view?.slice?.plane;null!=e&&c(e,this._viewState.slicePlane),this._viewState.slicePlaneEnabled=null!=e}_slicePlaneChanged(){for(const e of this._contexts.keys())if(e.symbolCreationContext.slicePlaneEnabled)return void this.setDirty()}addGraphicsOwner(e){const t=this._getGraphicsContext(e);return{addGraphic:i=>this._addGraphic(e,t,i),removeGraphic:e=>this._removeGraphic(t,e),labelingInfoChange:()=>this._labelsEnabledChanged(e,t),featureReductionChange:()=>this._enabledChanged(e,t),slicePlaneEnabledChange:()=>this._slicePlaneEnabledChanged(e,t),screenSizePerspectiveEnabledChanged:()=>this._screenSizePerspectiveEnabledChange(),clear:()=>t.forEach(e=>this._removeGraphic(t,e.graphics3DGraphic)),remove:()=>this._removeGraphicsOwner(e),setDirty:()=>this.setDirty(),getState:e=>this._getDeconflictorState(e),setState:(e,t)=>this._setDeconflictorState(e,t)}}_getDeconflictorState(e){return(e.getVisibilityFlag(1,8)?1:0)|(e.getVisibilityFlag(16,8)?16:0)}_setDeconflictorState(e,t){const i=!!(1&t),s=!!(16&t);e.setVisibilityFlag(1,8,i),e.setVisibilityFlag(16,8,s),this.setDirty()}_removeGraphicsOwner(e){const t=this._contexts.get(e);t&&(t.forEach(e=>this._removeGraphic(t,e.graphics3DGraphic)),this._contexts.delete(e),this.setDirty())}_addGraphic(e,t,i){const s=i.graphic.uid,a=new n(i,e.symbolCreationContext.slicePlaneEnabled);t.set(s,a),this.addToCheckOcclusion(a),_(e)&&this.addToActiveGraphics(a),e.labelsEnabled&&(this._labels.addToCheckOcclusion(a),this._labels.addToActiveGraphics(a));const r=this._initialVisibility(e,i);i.setVisibilityFlag(1,8,r)}_removeGraphic(e,t){const i=t.graphic.uid,s=e.get(i);s&&(this.removeFromActiveGraphics(s),this.removeFromCheckOcclusion(s),this._labels.removeFromActiveGraphics(s),this._labels.removeFromCheckOcclusion(s),e.delete(i),this.setDirty())}_enabledChanged(e,t){_(e)?t.forEach(e=>this.addToActiveGraphics(e)):t.forEach(e=>this.removeFromActiveGraphics(e)),this.setDirty()}_labelsEnabledChanged(e,t){e.labelsEnabled?(t.forEach(e=>this._labels.addToCheckOcclusion(e)),t.forEach(e=>this._labels.addToActiveGraphics(e))):(t.forEach(e=>this._labels.removeFromCheckOcclusion(e)),t.forEach(e=>this._labels.removeFromActiveGraphics(e)))}_slicePlaneEnabledChanged(e,t){const i=e.symbolCreationContext.slicePlaneEnabled;t.forEach(e=>e.slicePlaneEnabled=i),this.setDirty()}_screenSizePerspectiveEnabledChange(){this.setDirty()}_initialVisibility(e,t){if(t.isDraped)return!1;const i=t.layers;if(!i?.length)return!1;let s=!1,a=!1;for(const r of i)this.isSupportedLayer(r)&&(s=!0,a||=r.occlusionCheck);return!(a||s&&_(e))}_getGraphicsContext(e){const t=this._contexts.get(e);if(t)return t;const i=new Map;return this._contexts.set(e,i),this.setDirty(),i}};function _(e){const t=e.layer;return!("selection"!==t?.featureReduction?.type)}p=e([l("esri.views.3d.layers.graphics.GraphicsDeconflictor")],p);export{p as GraphicsDeconflictor};
