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