/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{EventEmitter as e}from"../../../../../../core/Evented.js";import{handlesGroup as t}from"../../../../../../core/handleUtils.js";import"../../../../../../core/has.js";import{destroyMaybe as s}from"../../../../../../core/maybe.js";import{StreamFeatureManager as i}from"../../../../../../layers/graphics/data/StreamFeatureManager.js";import{createConnection as n}from"../../../../../../layers/graphics/sources/connections/createConnection.js";import{internalTimeReceivedField as r}from"../../../../../../layers/support/streamLayerUtils.js";import{StreamConnectionState as o}from"../StreamConnectionState.js";import{StreamFeatureStore as a}from"../StreamFeatureStore.js";import{ALoadStrategy as c}from"./ALoadStrategy.js";import{StreamSourceChunk as h}from"./chunks/StreamSourceChunk.js";class m extends c{constructor(t,s,n){super(t.metadata,s,n),this._service=t,this._connectionState=new o,this._forceRefresh=!1,this.events=new e;const{timeInfo:c}=this.metadata,{purgeOptions:h}=s.full;this._stagingStore=new a(this.metadata.weakCloneWithAdditionalFields([{name:r,alias:"timeReceived",type:"esriFieldTypeDate"}]),e=>this.events.emit("features-updated",e)),this._manager=new i(this._stagingStore,this.metadata.featureIdInfo,c,h),this.connect()}destroy(){super.destroy(),this.disconnect()}get about(){return{willQueryAllFeatures:!1,willQueryFullResolutionGeometry:!1}}get connectionStatus(){return this._connectionState.connectionStatus}get errorString(){return this._connectionState?.errorString}get availableFields(){return this._schema.full.availableFields}get definitionExpression(){return this._schema.full.definitionExpression}async tryUpdate(e,t){const s=null!=this._chunk;if(t.sourceRefreshVersion!==this._refreshVersion){if(this._refreshVersion=t.sourceRefreshVersion,!this._manager.checkForUpdates()&&s&&!this._forceRefresh)return this.events.emit("tick"),!0;this._chunk&&(this.store.remove(this._chunk),e.requireInvalidation()),this._forceRefresh=!1,this._chunk=new h(this._stagingStore.reader),this.store.insert(this._chunk),this.events.emit("tick")}return this._schema.partial=t,!0}async load(e){}unload(e){}disconnect(){this._streamConnection=s(this._streamConnection),this._connectionState.connection=null,this._handlesGroup?.remove()}connect(){if(null!=this._streamConnection)return;const{geometryType:e,spatialReference:s}=this.metadata,{maxReconnectionAttempts:i,maxReconnectionInterval:r,geometryDefinition:o,definitionExpression:a,customParameters:c}=this._schema.full;this._streamConnection=n(this._service.source,s,this._service.outSpatialReference,e,a,o,i,r,c),this._handlesGroup=t([this._streamConnection.on("data-received",e=>this._onFeature(e)),this._streamConnection.on("message-received",e=>this._onWebSocketMessage(e))]),this._connectionState.connection=this._streamConnection}clear(){this._manager.checkForUpdates(),this._stagingStore.clear(),this._forceRefresh=!0}updateCustomParameters(e){this._streamConnection?.updateCustomParameters(e)}sendMessageToSocket(e){this._streamConnection?.sendMessageToSocket(e)}sendMessageToClient(e){this._streamConnection?.sendMessageToClient(e)}_onOverride(){}_onWebSocketMessage(e){if("type"in e)switch(e.type){case"delete":if(e.objectIds)for(const t of e.objectIds)this._manager.removeById(t);if(e.trackIds)for(const t of e.trackIds)this._manager.removeByTrackId(t);break;case"clear":this.clear()}this.events.emit("message-received",e)}_onFeature(e){try{this._manager.add(e),this.events.emit("data-received",e)}catch(t){}}}export{m as StreamLoadStrategy};
