/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{neverAbortedSignal as t}from"../../arcadeEnvironment.js";import{shapeExtent as e}from"../../kernel.js";import{throwIfCancelled as r,FeatureSetError as a}from"../support/errorsupport.js";import{FeatureSet as i}from"../support/FeatureSet.js";import{defaultMaxRecords as s}from"../support/shared.js";import{invokeRemoteGeometryOp as l}from"../../geometry/operatorsWorkerConnection.js";import{getGeometryExtent as n}from"../../../geometry/support/extentUtils.js";class o extends i{constructor(t){super(),this.declaredClass="esri.arcade.featureset.actions.SpatialFilter",this._maxProcessing=40,this._parent=t.parentfeatureset,this._relation=t.relation,this._relationString=t.relationString??"",this._relationGeom=t.relationGeom}get _spatialFilter(){return{relation:"esriSpatialRelRelation"!==this._relation?this._relation:`esriSpatialRelRelation:${this._relationString}`,geometry:this._relationGeom}}async _queryAll(){return(await this.query({abortSignal:t})).features}async query(t){await this._ensureLoaded();const e=await this._parent.query({...t,spatialFilter:this._spatialFilter});return r(t.abortSignal),{...e,spatialFilterApplied:null==t.spatialFilter,features:e.spatialFilterApplied?e.features:this._applySpatialFilter(e.features,t.abortSignal)}}async*_applySpatialFilter(t,e){for await(const a of t){r(e);const t=[];for(const e of a)await this._executeSpatialRelationTest(e)&&t.push(e);t.length>0&&(yield t)}}async queryStat(t){if(null!=t.spatialFilter)return{calculated:!1};const e=await this._parent.queryStat({...t,spatialFilter:this._spatialFilter});return e.calculated?e:null==t.where&&null==t.spatialFilter?this._manualStat(t.stat,t.field,t.limit??s,t.abortSignal):{calculated:!1}}async canQueryAggregate(t){return null==t.spatialFilter&&this._parent.canQueryAggregate({...t,spatialFilter:this._spatialFilter})}async queryAggregate(t){if(null!=t.spatialFilter)throw new a("NeverReach");return this._parent.queryAggregate({...t,spatialFilter:this._spatialFilter})}async _executeSpatialRelationTest(t){if(null==t.geometry)return!1;switch(this._relation){case"esriSpatialRelEnvelopeIntersects":{const r=e(this._relationGeom),a=n(t.geometry);return null!=r&&null!=a&&l("intersects",[r.toJSON(),a])}case"esriSpatialRelIntersects":return l("intersects",[this._relationGeom.toJSON(),t.geometry]);case"esriSpatialRelContains":return l("contains",[this._relationGeom.toJSON(),t.geometry]);case"esriSpatialRelOverlaps":return l("overlaps",[this._relationGeom.toJSON(),t.geometry]);case"esriSpatialRelWithin":return l("within",[this._relationGeom.toJSON(),t.geometry]);case"esriSpatialRelTouches":return l("touches",[this._relationGeom.toJSON(),t.geometry]);case"esriSpatialRelCrosses":return l("crosses",[this._relationGeom.toJSON(),t.geometry]);case"esriSpatialRelRelation":return l("relate",[this._relationGeom.toJSON(),t.geometry,this._relationString]);default:return this._relation,!1}}getFieldsIndex(){return this._parent.getFieldsIndex()}}export{o as default};
