/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{sqlAnd as e}from"../../core/sql.js";import{unpackFieldNames as t,featureHasFields as o}from"./fieldUtils.js";import{loadArcade as i}from"../../support/loadArcade.js";async function n(i,n,s,r){const u=new Array(n.length),l=new Map,c=new Map,a=t(i.fieldsIndex,s.outFields),d=!0===i.capabilities?.operations?.supportsQuery&&null!=i.queryFeatures,f=r?.hasRequiredFields??o;for(let e=0;e<n.length;e++){const t=n[e];if(d&&!t.isAggregate){if(s.returnGeometry||!f(t,a)){const o=t.getObjectId();if(null!=o){l.set(o,{graphic:t,index:e});continue}const i=t.getGlobalId();if(null!=i){c.set(i,{graphic:t,index:e});continue}}u[e]=t}else u[e]=t}if(!d||!i.queryFeatures||0===l.size&&0===c.size)return u.filter(Boolean);const p=[],y=(e,t)=>{t&&(e.outFields??=[],e.outFields.includes(t)||e.outFields.push(t))};if(l.size>0){const e=s.clone();y(e,i.objectIdField),"uniqueIdFields"in i&&i.uniqueIdFields?.length&&(e.outFields??=[],e.outFields.push(...i.uniqueIdFields)),e.objectIds=Array.from(l.keys()),p.push({type:"object-id",query:e,map:l})}const g="globalIdField"in i?i.globalIdField:null;if(null!=g&&c.size>0){const t=s.clone();y(t,g);const o=Array.from(c.keys());t.where=e(s.where,`${g} IN (${o.map(e=>`'${e}'`).join(",")})`),p.push({type:"global-id",query:t,map:c})}const b=r?.updateSourceAttributes??!1;for(const{type:e,query:t,map:o}of p)try{const n=await i.queryFeatures(t,r);for(const t of n.features){const i="object-id"===e?t.getObjectId():t.getGlobalId();if(null==i)continue;const n=o.get(i);if(!n)continue;const{graphic:s,index:r}=n;if(b&&t.attributes){s.attributes??={};for(const e of a)e in t.attributes&&(s.attributes[e]=t.attributes[e])}const{geometry:l,origin:c}=s;t.geometry||=l,t.origin=c,u[r]=t}}catch{}return u.filter(Boolean)}async function s(e){if(e.expressionInfos?.length||Array.isArray(e.content)&&e.content.some(e=>"expression"===e.type))return i()}export{n as fetchFeaturePopupFeatures,s as loadFeaturePopupArcadeModules};
