/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{isIntegratedMeshLayer as e,isTiledLayer as a,isLayerWithFlowRenderer as r}from"../../../layers/support/layerUtils.js";import{hitTestSelectSimilarDistance as t}from"../../support/hitTestSelectUtils.js";import{isScaleRangeActive as n,scaleBoundsPredicate as i}from"../../support/layerViewUtils.js";async function s(s,y){const{results:p,ground:f}=await t(s,y),u=(!f.layer||!e(f.layer))&&f.mapPoint,d=[],m=c(s),w=u?l(s,m):o;let g=0,h=0;const V=()=>{const e=w.layerViews[h++];if(!u||!e||!("fetchPopupFeaturesAtLocation"in e))return;const t=f.mapPoint,l=e.layer;if(a(l)&&"effectiveScaleRange"in l){const{minScale:e,maxScale:a}=l.effectiveScaleRange;if(n(e,a)){if(r(l)&&!i(s.scale,e,a))return;const n=s.basemapTerrain.getScale(t);if(!i(n,e,a))return}}d.push({mapPoint:t,layerView:e})};let S=null;for(;g<p.length||h<w.layerViews.length;){const e=p[g];if(e&&"graphic"!==e.type)++g;else if("scene"!==e?.layer?.type||e?.layer?.parent!==s?.map?.basemap)if(e){const a=e.layer?.uid,r=w.layerUids.has(a)&&e.distance===f.distance,t=m.get(e.layer?.uid);if(S??=e.mapPoint,h<w.layerViews.length&&(r||(e.distance??0)>f.distance)&&w.layerViews[h]!==t){V();continue}d.push({graphic:e.graphic,layerView:t}),++g}else V();else++g}return S??=f.mapPoint,{hits:d,location:S}}function l(e,a){const r=new Set,t=new Set;for(let i=e.basemapTerrain.numLayers(1)-1;i>=0;i--){const a=e.basemapTerrain.layerViewByIndex(i,1);r.add(a.layer.uid),t.add(a)}const n=e.overlayManager.renderer.layers;for(const{uid:i}of n){const e=a.get(i);e&&(r.add(i),t.add(e))}return{layerUids:r,layerViews:Array.from(t).reverse()}}const o={layerUids:new Set,layerViews:[]};function c(e){const a=new Map;for(const r of e.allLayerViews){const e=r.layer.uid;a.set(e,r)}return a}export{s as popupHitTest};
