/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../Graphic.js";import{isSome as t}from"../../../core/arrayUtils.js";import{isIterable as r}from"../../../core/iteratorUtils.js";import{isScreenRect as i,createScreenPoint as n,clipRectToView as s,isScreenPoint as a}from"../../../core/screenUtils.js";import{computeClippedScreenRect as l,clipContainsPoint as o,clipIntersectsScreenRect as c}from"./clippingUtils.js";import{isSupportedScreenPointEvent as p,createScreenPointFromSupportedEvent as u}from"../../support/screenUtils.js";function f(e){if(i(e)){return{hittestType:"rect",input:e}}return{hittestType:"point",input:e}}async function h(e,r,n){const a=!p(r)&&i(r),l=a?s(r,e.width,e.height):p(r)?u(e,r):r;if(!e.ready||isNaN(l.x)||isNaN(l.y))return{screenPoint:null,results:[]};let o=new Set;const c=new Set;let f=!1,h=null,V=null;n?.include?d(n.include,y(e,e=>{o.add(e),m(e,e=>c.add(e))},(e,t)=>{c.add(e),o.add(t)},e=>{h||(h=new Set),h.add(e)},e=>o.add(e),()=>f=!0)):(f=!0,o=new Set(e.allLayerViews),o.forEach(e=>{m(e,e=>c.add(e))})),n?.exclude&&d(n.exclude,y(e,e=>{o.delete(e),m(e,e=>c.delete(e))},e=>c.delete(e),e=>{V||(V=new Set),V.add(e)}));const j=M(e,l)[0],b=w(e,l,j).filter(e=>o.has(e)),L=f?e.graphicsView.hitTest(e,l).map(e=>({type:"graphic",graphic:e,layer:null,mapPoint:a?null:j})):[],S=Array.from(b).flatMap(t=>{if(a){const r=x(e,t,l);return null==r?[]:r.map(e=>t.hitTest(j,e))}return[t.hitTest(j,l)]});let T=[...L,...await Promise.all(S)].filter(t).flat().filter(t);T=T.filter(e=>"graphic"!==e.type||"subtype-group"!==e.layer?.type||c.has(e.graphic.layer));const U=new Set;return T=T.filter(e=>{if("graphic"!==e.type||!e.graphic)return!0;const t=g(e.graphic);return!U.has(t)&&(U.add(t),!0)}),h&&(T=T.filter(e=>!("graphic"in e)||!e.graphic||h?.has(g(e.graphic)))),V&&(T=T.filter(e=>!("graphic"in e)||!e.graphic||!V?.has(g(e.graphic)))),{screenPoint:a?null:l,results:T}}function y(t,r,i,n,s,a){return l=>{if(l instanceof e){if(l.layer===t)a?.();else{const e=t.allLayerViews.find(e=>e.layer===l.layer);e&&s?.(e)}n(g(l))}else if("layer"in l&&"element"in l);else if("subtype-sublayer"===l.type){const e=t.allLayerViews.find(e=>e.layer===l.parent);e&&i(l,e)}else{const e=t.allLayerViews.find(e=>e.layer===l);e&&r(e)}}}function d(e,t){if(e)if(r(e))for(const i of e)if(r(i))for(const e of i)t(e);else t(i);else t(e)}function g(e){const t=e.getObjectId();return t?`${e.layer?.uid??e.sourceLayer?.uid??"MapView"}/${t}`:`"MapView/${e.uid}`}function m(e,t){const r=e.layer;"subtype-group"===r?.type&&r.sublayers.forEach(e=>{t(e)})}function w(e,t,r){const i=a(t)?i=>o(e,i,t,r):r=>c(e,r,t);return e.allLayerViews.filter(e=>!e.suspended&&(!e.clips.length||e.clips.some(i))).reverse()}function x(e,t,r){if(!t.clips.length)return[r];const i=[];for(const n of t.clips){if("rect"!==n.type){i.push(r);continue}const t=l(e,n,r);t&&i.push(t)}return i}function M(e,t){if(i(t)){const r=t;return[e.toMap(n(r.x,r.y)),e.toMap(n(r.x,r.y+r.height)),e.toMap(n(r.x+r.width,r.y+r.height)),e.toMap(n(r.x+r.width,r.y))]}return[e.toMap(t)]}export{f as createHittestQuery,w as getHitTestLayerViews,M as getMapPointsFromScreen,h as hitTest};
