import{executeQuery as e,executeCount as t,concatenateWhereClauses as r,constrainQueryTimeExtentToView as s}from"../../support/_query.js";import{isSubtypeSublayer as o}from"../../utilities/_layers.js";import{AsyncQueue as u}from"../../utilities/asyncIterable.js";import{removeZValues as i}from"../../utilities/geometry.js";import{caseInsensitiveEquals as a}from"../../utilities/string.js";import{QueryOptions as l}from"./QueryOptions.js";import{QueryProviderBase as n}from"./QueryProviderBase.js";export class SubtypeSublayerQueryProvider extends n{isSupported(e){const{sublayer:t}=e;return o(t)&&t.layer&&e.layerExtension?.capabilities.supportsQuery}_query(t,s,o){const{sublayer:u}=t,i=this._createQuery(u.layer,o,o.outFields??t.featureSettings.outFields);return i.where=r(`${u.layer.subtypeField}=${u.subtypeCode}`,s),e(i,t,u.layer,o.cancelToken)}_queryMany(t,o,i){if(1===t.length){const e={...l.DEFAULTS,...l.fromFeatureSource(t[0]),...i};return this._query(t[0],o,e)}const n=new u,y=t.length;let c=0;const p=t.reduce(((e,t)=>{const r=t,s={...l.DEFAULTS,...l.fromFeatureSource(r),...i},o=s.outFields??r.featureSettings.outFields;if(e.has(r.sublayer.layer)){const t=e.get(r.sublayer.layer);let i=!1;for(const{sources:e,queryOptions:l,outFields:y}of t)if(n=s,(u=l).returnGeometry===n.returnGeometry&&(u.outSpatialReference===n.outSpatialReference||u.outSpatialReference?.equals(n.outSpatialReference))&&u.maxAllowableOffset===n.maxAllowableOffset&&o.length===y.length&&o.every((e=>y.some((t=>a(e,t)))))){e.push(r),i=!0;break}i||t.push({sources:[r],queryOptions:s,outFields:o})}else e.set(r.sublayer.layer,[{sources:[r],queryOptions:s,outFields:o}]);var u,n;return e}),new Map);for(const[t,u]of p)for(const{sources:i,queryOptions:l,outFields:p}of u)(async()=>{if(n.isDone)return;const u=new Map,f=[];for(const e of i)u.set(e.sublayer.subtypeCode,e),f.push(e.sublayer.subtypeCode);const d=this._createQuery(t,l,p);let b=!1;d.outFields.some((e=>"*"===e||a(e,t.subtypeField)))||(b=!0,d.outFields=[...d.outFields,t.subtypeField]),d.where="1=1"===o?"":o,d.where=r(`${t.subtypeField} IN (${f.join(",")})`,d.where),s(d,i[0]);try{for await(const r of e(d,(e=>{if(null!=e.attributes){const r=e.attributes;let s=r[t.subtypeField];if(void 0===s){s=r[Object.keys(r).find((e=>a(e,t.subtypeField)))]}return u.get(s)}}),t,l.cancelToken)){if(n.isDone)return;b&&r.attributes.delete(t.subtypeField),n.add(r)}}catch(e){n.reject(e)}finally{c+=i.length,c===y&&n.done()}})();return n}_count(e,s,o){const{sublayer:u}=e,i=this._createBasicQuery(u.layer,o,o.outFields??e.featureSettings.outFields);return i.where=r(`${u.layer.subtypeField}=${u.subtypeCode}`,s),t(i,e,u.layer,o.cancelToken)}_createQuery(e,t,r){const s=this._createBasicQuery(e,t,r);return s.outSpatialReference=t.outSpatialReference,s.maxAllowableOffset=t.maxAllowableOffset,s.returnZ=e.hasZ,s}_createBasicQuery(e,t,r){const s=e.createQuery();s.outFields=r,s.returnGeometry=t.returnGeometry;let o=t.spatialFilter;return o&&(s.spatialRelationship="intersects",!o||e.hasZ&&e.capabilities?.data?.supportsZ||(o=i(o.clone())),s.geometry=o),e.capabilities.query.supportsPagination&&(s.num=t.maxResults),s}}