/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
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 n from"../layerSources/DynamicDataLayer.js";import{executeRawQueryJSON as a}from"./executeQueryJSON.js";import{executeRawQueryPBF as s}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 s(t,u,i,n)}catch(y){if("query:parsing-pbf"!==y.name)throw y;r.format="json"}return"json"!==r?.format&&m||(d=await a(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 a=e?.infoFor3D;if(!d(t,a)||null==a||!r.assetMaps||!r.features?.length)return i.fromJSON(r);const{meshFeatureSetFromJSON:s}=await o(import("../support/meshFeatureSet.js"),n);return s(t,a,r)}function p(o,a){let s=u.from(o);s.sourceSpatialReference=s.sourceSpatialReference??a?.sourceSpatialReference??null,(a?.gdbVersion||a?.dynamicDataSource)&&(s=s===o?s.clone():s,s.gdbVersion=o.gdbVersion||a.gdbVersion,s.dynamicDataSource=o.dynamicDataSource?n.from(o.dynamicDataSource):a.dynamicDataSource);const i=a?.infoFor3D;if(null!=i&&d(o,i)){s=s===o?s.clone():s,s.formatOf3DObjects=null;const n=r(i),a=e(i);for(const t of i.queryFormats){if(t===n){s.formatOf3DObjects=t;break}t!==a||s.formatOf3DObjects||(s.formatOf3DObjects=t)}if(!s.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==s.outFields||!s.outFields.includes("*")){s=s===o?s.clone():s,null==s.outFields&&(s.outFields=[]);const{originX:t,originY:r,originZ:e,translationX:n,translationY:a,translationZ:u,scaleX:f,scaleY:c,scaleZ:l,rotationX:m,rotationY:p,rotationZ:d,rotationDeg:y}=i.transformFieldRoles;s.outFields.push(t,r,e,n,a,u,f,c,l,m,p,d,y)}}return s}function d(t,o){return null!=o&&!0===t.returnGeometry&&"xyFootprint"!==t.multipatchOption&&!t.outStatistics}export{f as executeQuery};
