/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{signal as e}from"../../../../../core/signal.js";import{CollisionJob as t}from"./CollisionJob.js";import{declutterBudget as i,fadeDuration as s}from"./config.js";import{SymbolDeclutterer as r}from"./SymbolDeclutterer.js";import{SymbolRepository as o}from"./SymbolRepository.js";const l=.5,a=1e-6;class n{constructor(t,s,l,n,c,h=i,u=()=>{}){this.styleRepository=s,this.tiles=n,this._declutterBudget=h,this._requestFeatureDeclutter=u,this._tileToHandle=new Map,this._viewState={scale:0,rotation:0,center:[0,0],size:[0,0]},this._declutterViewState={scale:0,rotation:0,center:[0,0],size:[0,0]},this._offsetFromScreenCenter=[0,0],this._completed=!1,this._fading=e(!1);const y=(e,t,i)=>this._createCollisionJob(e,t,i),_=e=>{const t=this.styleRepository.getStyleLayerByUID?.(e);if(t){if(this._zoom+a<t.minzoom||this._zoom-a>=t.maxzoom)return!1;const e=t.getLayoutProperty?.("visibility");if(e&&1===e.getValue())return!1}const i=this.styleRepository.layerContexts?.get(e);return!1!==i?.effectiveVisible},m=(e,t)=>(this.styleRepository.getStyleLayerByUID?.(e.styleLayerUID)?.z??0)-(this.styleRepository.getStyleLayerByUID?.(t.styleLayerUID)?.z??0);this._symbolRepository=new o(c,n),this._symbolDeclutterer=new r(t,n,this._symbolRepository,this.styleRepository,y,l,m,_)}get symbolRepository(){return this._symbolRepository}_createCollisionJob(e,i,s){return this.updateDecluttererViewState(),new t(e,i,s,this.styleRepository,this._zoom,this._viewState.rotation)}get fading(){return this._fading.value}get decluttererOffset(){return this._offsetFromScreenCenter}registerFeatureTile(e){this.symbolRepository?(this.symbolRepository.registerFeatureTile(e),this._requestFeatureDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}unregisterFeatureTile(e){this.symbolRepository?(this._symbolRepository.unregisterFeatureTile(e),this._requestFeatureDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}insertFeatureTileMetrics(e,t){this.symbolRepository?(this.symbolRepository.insertFeatureTileMetrics(e,t),this._requestFeatureDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}removeFeatureTileMetrics(e,t){this.symbolRepository?(this.symbolRepository.removeFeatureTileMetrics(e,t),this._requestFeatureDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}addTile(e){e.decluttered=!1,this._tileToHandle.set(e,e.on("symbols-changed",()=>{this._symbolRepository.registerVectorTile(e),this.restartDeclutter()})),this._symbolRepository.registerVectorTile(e),this.restartDeclutter()}removeTile(e){const t=this._tileToHandle.get(e);t&&(this._symbolRepository.unregisterVectorTile(e),this.restartDeclutter(),t.remove(),this._tileToHandle.delete(e))}update(e,t){this._zoom=e,this._viewState={scale:t.scale,rotation:t.rotation,center:[t.center[0],t.center[1]],size:[t.size[0],t.size[1]]};const i=[0,0];t.toScreen(i,t.center);const s=[0,0];return t.toScreen(s,this._declutterViewState.center),this._offsetFromScreenCenter[0]=i[0]-s[0],this._offsetFromScreenCenter[1]=i[1]-s[1],this._continueDeclutter(),this._completed}restartDeclutter(){this._completed=!1,this._symbolDeclutterer.restart(),this._notifyUnstable()}clear(){this._completed=!1,this._symbolRepository=null,this._symbolDeclutterer.restart(),this._tileToHandle.forEach(e=>e.remove()),this._tileToHandle.clear()}get stale(){return this._zoom!==this._declutterZoom||this._viewState.size[0]!==this._declutterViewState.size[0]||this._viewState.size[1]!==this._declutterViewState.size[1]||this._viewState.scale!==this._declutterViewState.scale||this._viewState.rotation!==this._declutterViewState.rotation}deleteStyleLayers(e){this._symbolRepository.deleteStyleLayers(e)}_continueDeclutter(){this._completed&&!this.stale||(this._symbolDeclutterer.running||(this.updateDecluttererViewState(),this._symbolDeclutterer.restart()),this._symbolDeclutterer.setScreenSize(this._viewState.size[0],this._viewState.size[1]),this._completed=this._symbolDeclutterer.continue(this._declutterBudget),this._completed&&this._scheduleNotifyStable())}_scheduleNotifyStable(){null!=this._stableNotificationHandle&&clearTimeout(this._stableNotificationHandle),this._stableNotificationHandle=setTimeout(()=>{this._stableNotificationHandle=null,this._fading.value=!1},(1+l)*s)}_notifyUnstable(){null!=this._stableNotificationHandle&&(clearTimeout(this._stableNotificationHandle),this._stableNotificationHandle=null),this._fading.value=!0}updateDecluttererViewState(){this._declutterZoom=this._zoom,this._declutterViewState.center[0]=this._viewState.center[0],this._declutterViewState.center[1]=this._viewState.center[1],this._declutterViewState.rotation=this._viewState.rotation,this._declutterViewState.scale=this._viewState.scale,this._declutterViewState.size[0]=this._viewState.size[0],this._declutterViewState.size[1]=this._viewState.size[1],this._offsetFromScreenCenter[0]=0,this._offsetFromScreenCenter[1]=0}}export{n as SymbolFader};
