/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import t from"../../core/Error.js";import has from"../../core/has.js";import{whenOrAbort as o}from"../../core/promiseUtils.js";import{getGlbFormatId as r,getGltfFormatId as e}from"../../layers/support/infoFor3D.js";import{DataLayerSource as n}from"../../layers/support/source/DataLayerSource.js";import{executeRawQueryJSON as s}from"./executeQueryJSON.js";import{executeRawQueryPBF as a}from"./executeQueryPBF.js";import i from"../support/FeatureSet.js";import u from"../support/Query.js";async function f(t,o,r,e,n){return m(o,await c(t,o,r,e,n),r,e)}async function c(t,o,r,e,n){const i={...e},u=p(o,r),f=null!=o.outStatistics?.[0],c=has("featurelayer-pbf-statistics"),m=!f||c;let d;if("pbf"===r?.format&&m)try{d=await a(t,u,i,n)}catch(y){if("query:parsing-pbf"!==y.name)throw y;r.format="json"}return"json"!==r?.format&&m||(d=await s(t,u,i,n)),l(r?.fieldsIndex,d.fields),d}function l(t,o){if(null!=t&&null!=o)for(const r of o){const o=t.get(r.name);o&&Object.assign(r,o.toJSON())}}async function m(t,r,e,n){const s=e?.infoFor3D;if(!d(t,s)||null==s||!r.assetMaps||!r.features?.length)return i.fromJSON(r);const{meshFeatureSetFromJSON:a}=await o(import("../support/meshFeatureSet.js"),n);return a(t,s,r)}function p(o,s){let a=u.from(o);a.sourceSpatialReference=a.sourceSpatialReference??s?.sourceSpatialReference??null,(s?.gdbVersion||s?.dynamicDataSource)&&(a=a===o?a.clone():a,a.gdbVersion=o.gdbVersion||s.gdbVersion,a.dynamicDataSource=o.dynamicDataSource?n.from(o.dynamicDataSource):s.dynamicDataSource);const i=s?.infoFor3D;if(null!=i&&d(o,i)){a=a===o?a.clone():a,a.formatOf3DObjects=null;const n=r(i),s=e(i);for(const t of i.queryFormats){if(t===n){a.formatOf3DObjects=t;break}t!==s||a.formatOf3DObjects||(a.formatOf3DObjects=t)}if(!a.formatOf3DObjects)throw new t("query:unsupported-3d-query-formats","Could not find any supported 3D object query format. Only supported formats are 3D_glb and 3D_gltf");if(null==a.outFields||!a.outFields.includes("*")){a=a===o?a.clone():a,null==a.outFields&&(a.outFields=[]);const{originX:t,originY:r,originZ:e,translationX:n,translationY:s,translationZ:u,scaleX:f,scaleY:c,scaleZ:l,rotationX:m,rotationY:p,rotationZ:d,rotationDeg:y}=i.transformFieldRoles;a.outFields.push(t,r,e,n,s,u,f,c,l,m,p,d,y)}}return a}function d(t,o){return null!=o&&!0===t.returnGeometry&&"xyFootprint"!==t.multipatchOption&&!t.outStatistics}export{f as executeQuery};
