/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{signal as t}from"../../../../../core/signal.js";import{CollisionJob as e}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(e,s,l,n,c,h=i){this.styleRepository=s,this._declutterBudget=h,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=t(!1);const y=(t,e,i)=>this._createCollisionJob(t,e,i),_=t=>{const e=this.styleRepository.getStyleLayerByUID?.(t);if(e){if(this._zoom+a<e.minzoom||this._zoom-a>=e.maxzoom)return!1;const t=e.getLayoutProperty?.("visibility");if(t&&1===t.getValue())return!1}const i=this.styleRepository.layerContexts?.get(t);return!1!==i?.effectiveVisible},u=(t,e)=>(this.styleRepository.getStyleLayerByUID?.(t.styleLayerUID)?.z??0)-(this.styleRepository.getStyleLayerByUID?.(e.styleLayerUID)?.z??0);this._symbolRepository=new o(c,n),this._symbolDeclutterer=new r(e,n,this._symbolRepository,this.styleRepository,y,l,u,_)}get symbolRepository(){return this._symbolRepository}_createCollisionJob(t,i,s){return this.updateDecluttererViewState(),new e(t,i,s,this.styleRepository,this._zoom,this._viewState.rotation)}get fading(){return this._fading.value}get decluttererOffset(){return this._offsetFromScreenCenter}registerFeatureTile(t){this.symbolRepository?(this.symbolRepository.registerFeatureTile(t),this.restartDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}unregisterFeatureTile(t){this.symbolRepository?(this._symbolRepository.unregisterFeatureTile(t),this.restartDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}insertFeatureTileMetrics(t,e){this.symbolRepository?(this.symbolRepository.insertFeatureTileMetrics(t,e),this.restartDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}removeFeatureTileMetrics(t,e){this.symbolRepository?(this.symbolRepository.removeFeatureTileMetrics(t,e),this.restartDeclutter()):console.error("InternalError: Symbol repository not yet initialized")}addTile(t){t.decluttered=!1,this._tileToHandle.set(t,t.on("symbols-changed",()=>{this._symbolRepository.registerVectorTile(t),this.restartDeclutter()})),this._symbolRepository.registerVectorTile(t),this.restartDeclutter()}removeTile(t){const e=this._tileToHandle.get(t);e&&(this._symbolRepository.unregisterVectorTile(t),this.restartDeclutter(),e.remove(),this._tileToHandle.delete(t))}update(t,e){this._zoom=t,this._viewState={scale:e.scale,rotation:e.rotation,center:[e.center[0],e.center[1]],size:[e.size[0],e.size[1]]};const i=[0,0];e.toScreen(i,e.center);const s=[0,0];return e.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(t=>t.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(t){this._symbolRepository.deleteStyleLayers(t)}_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};
