/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{VectorTile as e}from"../VectorTile.js";import{fadeDuration as t}from"./config.js";function o(e){return(e.uniqueSymbol?.show&&e.uniqueSymbol?.lastShow)??!1}function i(e,t){if(e.priority-t.priority)return e.priority-t.priority;if(o(e)&&!o(t))return-1;if(o(t)&&!o(e))return 1;const i=e.tile.key,s=t.tile.key;return i.world-s.world?i.world-s.world:i.level-s.level?i.level-s.level:i.row-s.row?i.row-s.row:i.col-s.col?i.col-s.col:e.xTile-t.xTile?e.xTile-t.xTile:e.yTile-t.yTile}class s{get running(){return this._running}constructor(e,t,o,i,s,r,n,l){this.selectionMode=e,this._visibleTiles=t,this._symbolRepository=o,this._styleRepository=i,this._createCollisionJob=s,this._assignTileSymbolsOpacity=r,this._symbolLayerSorter=n,this._isLayerVisible=l,this._selectionJob=null,this._selectionJobCompleted=!1,this._collisionJob=null,this._collisionJobCompleted=!1,this._opacityJob=null,this._opacityJobCompleted=!1,this._running=!0}setScreenSize(e,t){this._screenWidth===e&&this._screenHeight===t||this.restart(),this._screenWidth=e,this._screenHeight=t}restart(){this._selectionJob=null,this._selectionJobCompleted=!1,this._collisionJob=null,this._collisionJobCompleted=!1,this._opacityJob=null,this._opacityJobCompleted=!1,this._running=!0}continue(e){if(this._selectionJob||(this._selectionJob=this._createSelectionJob()),!this._selectionJobCompleted){const t=performance.now();if(!this._selectionJob.work(e))return!1;if(this._selectionJobCompleted=!0,0===(e=Math.max(0,e-(performance.now()-t))))return!1}if(this._collisionJob||(this._collisionJob=this._createCollisionJob(this._selectionJob.sortedSymbols,this._screenWidth,this._screenHeight)),!this._collisionJobCompleted){const t=performance.now();if(!this._collisionJob.work(e))return!1;if(this._collisionJobCompleted=!0,0===(e=Math.max(0,e-(performance.now()-t))))return!1}if(this._opacityJob||(this._opacityJob=this._createOpacityJob()),!this._opacityJobCompleted){const t=performance.now();if(!this._opacityJob.work(e))return!1;if(this._opacityJobCompleted=!0,0===(e=Math.max(0,e-(performance.now()-t))))return!1}return this._running=!1,!0}_getFilteredByLayer(){let e;if(this._styleRepository?.layerContexts)for(const t of this._symbolRepository.uniqueSymbols){const o=this._styleRepository.layerContexts?.get(t.styleLayerUID);if(o?.attributeView)for(const i of t.uniqueSymbols){e??=new Map,e.get(t.styleLayerUID)||e.set(t.styleLayerUID,new Set);const s=e.get(t.styleLayerUID);o.isFeatureFiltered(i.id)&&s.add(i.id)}}return e}_resetSelection(){for(let e=0;e<this._symbolRepository.uniqueSymbols.length;e++){const t=this._symbolRepository.uniqueSymbols[e];for(let e=0;e<t.uniqueSymbols.length;e++){const o=t.uniqueSymbols[e];for(const e of o.tileSymbols)e.selectedForRendering=!1}}}_createSelectionJob(){const e="feature-tile"===this.selectionMode?n:l,t=this._symbolRepository.uniqueSymbols;this._resetSelection();const o=[];let s=0,r=0;const c=this._isLayerVisible,y=this._getFilteredByLayer(),a=this._styleRepository?.layerContexts;function h(n){let l;const h=performance.now();for(;r<t.length;r++,s=0){const i=t[r],u=i.styleLayerUID,f=y?.get(u);let b=0;if(a){b=a.get(u).layerOrder}if(!c(u)){o[r]||(o[r]={styleLayerUID:u,layerOrder:b,symbols:[]});continue}o[r]||={styleLayerUID:u,symbols:[],layerOrder:b};const m=o[r];for(;s<i.uniqueSymbols.length;s++){if(l=i.uniqueSymbols[s],s%100==99&&performance.now()-h>n)return!1;if(l.lastShow=l.show,l.id&&f?.has(l.id)){l.show=!1,l.parts[0].show=!1,l.parts[1].show=!1;continue}const t=e(l);if(t){t.selectedForRendering=!0,m.symbols.push(t),l.show=!0;for(const e of l.parts)e.show=!0}else l.show=!1}}for(const e of o)e.symbols.sort(i);return o.sort((e,t)=>t.layerOrder-e.layerOrder),!0}const u=this._symbolLayerSorter;return{work:h,get sortedSymbols(){return o.sort(u)}}}_createOpacityJob(){const t=this._assignTileSymbolsOpacity,o=this._visibleTiles;let i=0;function s(e,o){for(const t of e.symbols.values())r(t,o);t(e,o);for(const t of e.childrenTiles)s(t,o)}return{work(r){const n=performance.now();for(;i<o.length;i++){if(performance.now()-n>r)return!1;const l=o[i];if(null!=l.parentTile)continue;const c=performance.now();l instanceof e?s(l,c):t(l,c)}return!0}}}}function r(e,o){for(const i of e){const e=i.uniqueSymbol;for(const i of e.parts){const s=i.targetOpacity>.5?1:-1;i.startOpacity+=s*((o-i.startTime)/t),i.startOpacity=Math.min(Math.max(i.startOpacity,0),1),i.startTime=o,i.targetOpacity=e.show&&i.show?1:0}}}function n(e){let t=null,o=null,i=null;for(const s of e.tileSymbols){const e=s.tile;e.isReady&&e.isCoverage?t=s:e.isReady?o=s:e.rendering&&(i=s)}return t??o??i}function l(e){let t=null,o=!1,i=!1;for(const s of e.tileSymbols)if(!i||!o){const e=s.tile;(!t||e.isCoverage||e.neededForCoverage&&!o)&&(t=s,(e.neededForCoverage||e.isCoverage)&&(i=!0),e.isCoverage&&(o=!0))}return i?t:null}export{s as SymbolDeclutterer};
