import{__decorate as e}from"tslib";import r from"@arcgis/core/core/Collection";import{InvalidOperationError as i}from"../../InvalidOperationError.js";import{InitializableBase as o}from"../../support/InitializableBase.js";import{serializable as t}from"../../support/Serializable.js";import{isCancellation as s}from"../../support/utilities.js";import{groupBy as n}from"../../utilities/array.js";import{AsyncQueue as a}from"../../utilities/asyncIterable.js";import{checkArg as d}from"../../utilities/checkArg.js";import{parallelEach as u,reduce as p}from"../../utilities/promise.js";import{QueryError as c}from"./QueryError.js";import{QueryOptions as y}from"./QueryOptions.js";let l=class extends o{providers=new r;isSupported(e){return!!this.providers.find((r=>r.isSupported(e)))}query(e,r,o){d("source",e).isNotMissing().isNotEmpty(),d("where",r).isNotEmpty();const t=new a,l=Array.isArray(e)?e:[{source:e,where:r,options:o}],f=[];let h=0;const v=(e,r)=>{r&&0!==e.length&&(h++,(async()=>{if(t.isDone)return;const i=await p(e,(async(e,r)=>{const i={...y.DEFAULTS,...y.fromFeatureSource(r.source),...r.options};if(e.has(r.where)){const o=e.get(r.where);let t=!1;for(const{sources:e,options:s}of o)if(await y.areSameOptions(i,s)){e.push(r.source),t=!0;break}t||o.push({sources:[r.source],options:i})}else e.set(r.where,[{sources:[r.source],options:i}]);return e}),new Map);if(t.isDone)return;await u(e,(e=>e.source.initialize()));const o=[];for(const[e,n]of i){if(t.isDone)return;for(const i of n)o.push((async()=>{if(!t.isDone)try{const o=r.query(i.sources,e,i.options);for await(const e of o){if(t.isDone)return;i.options.outSpatialReference&&(e.schema.spatialReference=i.options.outSpatialReference),t.add(e)}}catch(e){s(e)?t.reject(e):e instanceof c?f.push(e):f.push([i.sources,e])}})())}await Promise.all(o),0==--h&&(f.length?t.reject(new c(f)):t.done())})())};return(async()=>{await this.initialize(),n(l,(e=>this.providers.find((r=>r.isSupported(e.source))))).forEach(v),0===h&&t.reject(new i("Unable to query: no provider registered for this type of feature source."))})(),t}async count(e,r,o){d("source",e).isNotMissing(),d("where",r).isNotMissing(),await this.initialize();const t=this.providers.find((r=>r.isSupported(e)));if(!t)throw new i("Unable to query: no provider registered for this type of feature source.");try{await e.initialize();const i={returnGeometry:!1,outFields:void 0};return await t.count(e,r,{...y.fromFeatureSource(e),...o,...i})}catch(r){throw s(r)?r:new c([[e,r]])}}async _onInitialize(){await super._onInitialize();const{ArcGISServiceTableQueryProvider:e,ArcGISSublayerQueryProvider:r,BuildingComponentSublayerClientSideQueryProvider:i,CSVLayerClientSideQueryProvider:o,FeatureLayerQueryProvider:t,GeoJSONLayerClientSideQueryProvider:s,GeoRSSLayerQueryProvider:n,ImageryLayerQueryProvider:a,KMLLayerQueryProvider:d,MapNotesSublayerQueryProvider:u,RouteLayerQueryProvider:p,SceneLayerClientSideQueryProvider:c,SceneLayerQueryProvider:y,StreamLayerClientSideQueryProvider:l,SubtypeSublayerQueryProvider:f,WFSLayerQueryProvider:h}=await import("./defaultProviders.js");this.providers.add(new e),this.providers.add(new r),this.providers.add(new o),this.providers.add(new t),this.providers.add(new s),this.providers.add(new n),this.providers.add(new a),this.providers.add(new d),this.providers.add(new i),this.providers.add(new c),this.providers.add(new y),this.providers.add(new l),this.providers.add(new f),this.providers.add(new p),this.providers.add(new u),this.providers.add(new h)}};l=e([t],l);export{l as QueryService};