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