import{InvalidOperationError as r}from"../../InvalidOperationError.js";import{isCancellation as e}from"../../support/utilities.js";import{AsyncQueue as t}from"../../utilities/asyncIterable.js";import{checkArg as i}from"../../utilities/checkArg.js";import{parallelEach as o}from"../../utilities/promise.js";import{QueryError as s}from"./QueryError.js";import{QueryOptions as n}from"./QueryOptions.js";export class QueryProviderBase{query(e,t,s){i("source",e).isNotMissing().isNotEmpty(),i("where",t).isNotMissing();const n=Array.isArray(e)?e:[e];if(!n.every((r=>this.isSupported(r))))throw new r("Unsupported feature source.");const u=this;return async function*(){await o(n,(r=>r.initialize())),yield*u._queryMany(n,t,s)}()}async count(e,t,o){if(i("source",e).isNotMissing(),i("where",t).isNotMissing(),!this.isSupported(e))throw new r("Unsupported feature source.");await e.initialize();const s={returnGeometry:!1,outFields:void 0};return this._count(e,t,{...n.fromFeatureSource(e),...o,...s})}_queryMany(r,i,o){const u=new t;if(0===r.length)return u.done(),u;let a=0;const c=new Map;for(const t of r)(async()=>{if(u.isDone)return;const f={...n.DEFAULTS,...n.fromFeatureSource(t),...o};try{for await(const r of this._query(t,i,f)){if(u.isDone)return;u.add(r)}}catch(r){if(e(r))return void u.reject(r);c.set(t,r)}finally{++a===r.length&&(0===c.size?u.done():u.reject(new s(c)))}})();return u}}