/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{sqlIn 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 s(i,s,n,r){const u=new Array(s.length),l=new Map,c=new Map,a=t(i.fieldsIndex,n.outFields),d=!0===i.capabilities?.operations?.supportsQuery&&null!=i.queryFeatures,f=r?.hasRequiredFields??o;for(let e=0;e<s.length;e++){const t=s[e];if(d&&!t.isAggregate){if(n.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=n.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=n.clone();y(t,g);const o=Array.from(c.keys());t.where=e(g,o),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 s=await i.queryFeatures(t,r);for(const t of s.features){const i="object-id"===e?t.getObjectId():t.getGlobalId();if(null==i)continue;const s=o.get(i);if(!s)continue;const{graphic:n,index:r}=s;if(b&&t.attributes){n.attributes??={};for(const e of a)e in t.attributes&&(n.attributes[e]=t.attributes[e])}const{geometry:l,origin:c}=n;t.geometry||=l,t.origin=c,u[r]=t}}catch{}return u.filter(Boolean)}async function n(e){if(e.expressionInfos?.length||Array.isArray(e.content)&&e.content.some(e=>"expression"===e.type))return i()}export{s as fetchFeaturePopupFeatures,n as loadFeaturePopupArcadeModules};
