import{InvalidOperationError as t}from"../../InvalidOperationError.js";import{AsyncQueue as e}from"../../utilities/asyncIterable.js";import{checkArg as i}from"../../utilities/checkArg.js";import{buffer as r}from"../../utilities/geometry.js";import{parallelEach as o}from"../../utilities/promise.js";import{IdentifyOptions as n}from"./IdentifyOptions.js";export class IdentifyProviderBase{identify(e,r,n){i("source",e).isNotMissing().isNotEmpty(),i("geometry",r).isNotMissing();const s=Array.isArray(e)?e:[e];if(!s.every((t=>this.isSupported(t,r,n))))throw new t("Unsupported feature source.");const a=this;return async function*(){await o(s,(t=>t.initialize())),yield*a._identifyMany(s,await a._applyTolerance(r,n),n,r.type)}()}async count(e,r,o){if(i("source",e).isNotMissing(),i("geometry",r).isNotMissing(),!this.isSupported(e,r,o))throw new t("Unsupported feature source.");await e.initialize();return this._count(e,await this._applyTolerance(r,o),{...n.fromFeatureSource(e),...o,returnGeometry:!1},r.type)}_identifyMany(t,i,r,o){const s=new e;if(0===t.length)return s.done(),s;let a=0;for(const e of t)(async()=>{if(s.isDone)return;const c={...n.DEFAULTS,...n.fromFeatureSource(e),...r};try{for await(const t of this._identify(e,i,c,o)){if(s.isDone)return;s.add(t)}}catch(t){s.reject(t)}finally{++a===t.length&&s.done()}})();return s}async _applyTolerance(t,e){switch(t.type){case"point":case"multipoint":case"polygon":case"polyline":return!e?.tolerance||e.tolerance<=0?t:await r(t,e.tolerance,"meters")??t;default:return t}}}