import{toFeature as e}from"../../data/Feature.js";import{cancelify as t,cancelifyWithAbortSignal as r}from"../../support/Cancellable.js";import{watch as i}from"../../support/observableUtils.js";import*as a from"../../utilities/asyncIterable.js";import{isSubtypeSublayerExtension as s}from"../../utilities/extensions.js";import{isBuildingComponentSublayer as n}from"../../utilities/layers.js";import{QueryProviderBase as o}from"./QueryProviderBase.js";export class ClientSideQueryProvider extends o{get _waitForUpdatingFinished(){return!0}_query(t,r,i){const s=this;return async function*(){const n=await s._getClientSideLayerGraphics(t,r,i);n?.length?yield*n.map((r=>e(r,{source:t}))):yield*a.empty()}()}async _count(e,t,r){const i=await this._getClientSideLayerGraphics(e,t,r);return{source:e,count:i?i.length:0}}_createQuery(e,t,r){const i=s(e)?e.layerExtension:e,{_layerView:a,featureSettings:o}=i,l=n(i.sublayer)?e.layerExtension?.mapExtension:e.mapExtension,u=a.createQuery();return u.where=t,u.geometry=r.spatialFilter,u.outFields=o.outFields,u.returnGeometry=r.returnGeometry,u.outSpatialReference=r.outSpatialReference,u.maxAllowableOffset=r.maxAllowableOffset,u.num=r.maxResults,u.returnZ="scene"===l?.viewMode,u}async _getClientSideLayerGraphics(e,a,n){const o=s(e)?e.layerExtension:e,{_layerView:l}=o;if(!l)return;this._waitForUpdatingFinished&&await t(new Promise(((e,t)=>{l.updating||e();const r=i(l,"updating",(t=>{t||(r.remove(),e())}))})),n.cancelToken);const u=await r((async t=>l.queryFeatures(this._createQuery(e,a,n),{signal:t})),n.cancelToken);return u?.features}}