/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{addMany as e}from"../../../core/arrayUtils.js";import{ignoreAbortErrors as r,whenOrAbort as t}from"../../../core/promiseUtils.js";import{getDisplayFieldName as s}from"../../../layers/support/fieldUtils.js";import{isKnowledgeGraphLayer as a,isLinkChartLayer as i,isMapNotesLayer as l}from"../../../layers/support/layerUtils.js";import{calculateTolerance as n}from"../../../renderers/support/clickToleranceUtils.js";import{createQueryGeometry as o}from"../../../views/support/drapedUtils.js";import{isISelectableLayerWithObjectIds as c,isISelectableLayerView as u,isISelectableLayerWithGraphics as d,isISelectableLayerViewWithObjectIds as y}from"../../../views/support/selectionUtils.js";async function p(e){const{returnFeatureTitleFields:s,returnGeometry:a,selector:i,signal:l,sources:n,view:o}=e;if(!n?.length)return[];const{layers:c,layerViews:u,graphicsLayers:d}=m(n),y=[];return await r(t(Promise.all([h({candidates:y,layers:c,returnFeatureTitleFields:s,returnGeometry:a,selector:i,signal:l,view:o}),f({candidates:y,layerViews:u,returnGeometry:a,selector:i,signal:l,view:o}),v({candidates:y,graphicsLayers:d,selector:i})]),l)),y}function m(e){const r=[],t=[],s=[];return e.forEach(e=>{(a(e)||i(e))&&e.layers?.length?r.push(...e.layers.toArray()):l(e)&&e.sublayers?.length?s.push(...e.sublayers.toArray()):c(e)?r.push(e):u(e)?t.push(e):d(e)&&s.push(e)}),{layers:r,layerViews:t,graphicsLayers:s}}async function f(r){const{candidates:s,layerViews:a,returnGeometry:i,selector:l,signal:n,view:o}=r;"point"!==l.type?await t(Promise.allSettled(a.map(async r=>{if(y(r)){const t=b(r,l,o,i),a=await r.queryFeatures(t,{signal:n});e(s,a.features)}else d(r.layer)&&await v({candidates:s,graphicsLayers:[r.layer],selector:l})})),n):await g({candidates:s,layerViews:a,selector:l,view:o})}async function g({candidates:e,layerViews:r,selector:t,view:s}){const a=s.toScreen(t);if(!a)return;const i=r.map(e=>e.layer),{results:l}=await s.hitTest(a,{include:i});l.forEach(r=>{"graphic"in r&&r.graphic&&e.push(r.graphic)})}async function h(r){const{candidates:s,layers:a,returnFeatureTitleFields:i,returnGeometry:l,selector:n,signal:o,view:c}=r;await t(Promise.allSettled(a.map(async r=>{if("isTable"in r&&r.isTable)return;const t=T(r,n,c,i,l),a=await r.queryFeatures(t,{signal:o});e(s,a.features)})),o)}function w(e,r,t){return"point"===e.type?o(e,"renderer"in r?n({renderer:r.renderer}):0,t):e}function F(e,r,t){const{fields:a,fieldsIndex:i}=e,l=new Set([e.objectIdField]);"globalIdField"in e&&null!=e.globalIdField&&i.has(e.globalIdField)&&l.add(i.get(e.globalIdField).name);const n="displayField"in e?e.displayField:null,o=s({displayField:n,fields:a});if(null!=o&&i.has(o)&&l.add(o),"subtypeField"in e&&null!=e.subtypeField){const t=i.get(e.subtypeField)?.name;if(null!=t&&l.add(t),"utilityNetworks"in r.map&&r.map.utilityNetworks?.length){const e=i.get("assetgroup");e?.name&&l.add(e.name);const r=i.get("assettype");r?.name&&l.add(r.name)}}return(t||r.requiredFieldsOptions.featureTitleFields)&&"featureTitleFields"in e&&e.featureTitleFields&&e.featureTitleFields.forEach(e=>l.add(e)),Array.from(l.values())}function b(e,r,t,s=!0){const a=e.createQuery();return a.outFields=["*"],a.geometry=w(r,e.layer,t),a.returnGeometry=s,a.outSpatialReference=t.spatialReference,a}function T(e,r,t,s=!1,a=!0){const i=e.createQuery();return i.outFields=F(e,t,s),i.geometry=w(r,e,t),i.returnGeometry=a,i.outSpatialReference=t.spatialReference,i}async function v(e){const{candidates:r,graphicsLayers:t,selector:s}=e,a=t.flatMap(e=>e.graphics.toArray())??[];if(!a?.length||!s)return;const i=await import("../../../geometry/operators/intersectsOperator.js");i.accelerateGeometry(s),a.forEach(e=>{e.geometry&&"mesh"!==e.geometry.type&&i.execute(s,e.geometry)&&r.push(e)})}export{f as collectSelectionFromFeatureLayerViews,h as collectSelectionFromFeatureLayers,v as collectSelectionFromGraphicsLayers,p as getSelectionFromGeometry,w as getSuggestedQueryGeometry,F as getSuggestedQueryOutFields};
