/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../chunks/tslib.es6","../../../geometry","../../../Graphic","../../../core/Accessor","../../../core/HandleOwner","../../../core/maybe","../../../core/Promise","../../../core/reactiveUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","../data/StreamFeatureManager","../sources/connections/createConnection","../../../views/3d/support/EventedSet","../../../geometry/support/typeUtils"],(function(e,t,r,o,n,i,a,s,c,p,l,h,u,d,y,f,_,m,g){"use strict";const v=2500;e.StreamGraphic=function(e){function r(){return e.apply(this,arguments)||this}return t._inheritsLoose(r,e),r.prototype.getObjectId=function(){return this.objectId},r}(n),r.__decorate([l.property({type:Number,json:{read:!0}})],e.StreamGraphic.prototype,"objectId",void 0),e.StreamGraphic=r.__decorate([y.subclass("esri.layers.graphics.controllers.StreamGraphic")],e.StreamGraphic);let S=function(){function e(e){this.onUpdate=e,this._idToGraphic=new Map}var r=e.prototype;return r.destroy=function(){this._idToGraphic.clear()},r.add=function(e){this._idToGraphic.set(e.objectId,e)},r.get=function(e){return this._idToGraphic.get(e)},r.forEach=function(e){this._idToGraphic.forEach(e)},r.removeById=function(e){const t=this._idToGraphic.get(e);return t?(t.sourceLayer=t.layer=null,this._idToGraphic.delete(e),t):null},r.update=function(e,t){this.onUpdate(e,t)},t._createClass(e,[{key:"size",get:function(){return this._idToGraphic.size}}]),e}();e.StreamController=function(r){function o(){var e;return(e=r.apply(this,arguments)||this)._updateInfo={websocket:0,client:0},e.graphics=new m.EventedSet,e}t._inheritsLoose(o,r);var n=o.prototype;return n.initialize=function(){this.addResolvingPromise(this.layer.when((()=>this._startup())))},n.destroy=function(){this.clear()},n._clearInterval=function(){this._updateIntervalId&&(clearInterval(this._updateIntervalId),this._updateIntervalId=0)},n.clear=function(){this._clearInterval(),this.connection&&(this.connection.destroy(),this.connection=null),this.store&&(this.store.destroy(),this.store=null),this.graphics.clear(),this.handles.removeAll()},n._startup=function(){const{layer:e,layerView:t}=this,{parsedUrl:r,spatialReference:o,definitionExpression:n,geometryDefinition:i,objectIdField:a,timeInfo:s,purgeOptions:c,maxReconnectionAttempts:l,maxReconnectionInterval:h,customParameters:u}=e,d=g.featureGeometryTypeKebabDictionary.toJSON(e.geometryType),y=o,m=t.view.spatialReference,v={geometry:i,where:n};this.clear(),this._set("connection",_.createConnection(r,y,m,d,v,l,h,u)),this._outSpatialReference=m.toJSON(),this.store=new S(this._onUpdate.bind(this)),this.featuresManager=new f.StreamFeatureManager(this.store,a,s.toJSON(),c);const I="startup-watches";this.handles.remove(I),this.handles.add([this.connection.on("feature",(e=>this._onFeature(e))),p.watch((()=>[e.definitionExpression,e.geometryDefinition,e.purgeOptions]),(()=>this._startup()))],I),this._initUpdateInterval()},n._onFeature=function(t){this._updateInfo.websocket++,this.layerView.hasEventListener("data-received")&&this.layerView.emit("data-received",{attributes:t.attributes,centroid:t.centroid,geometry:t.geometry});try{s.isSome(t.geometry)&&!t.geometry.spatialReference&&(t.geometry.spatialReference=this._outSpatialReference);const r=e.StreamGraphic.fromJSON(t);r.sourceLayer=r.layer=this.layer,this.featuresManager.add(r)}catch{}},n._onUpdate=function(e,t){s.isSome(t)&&this.graphics.removeMany(t),s.isSome(e)&&(this._updateInfo.client+=e.length,this.graphics.addMany(e))},n._initUpdateInterval=function(){this._clearInterval();const{updateInterval:e}=this.layer;let t=performance.now();this._updateIntervalId=setInterval((()=>{const e=performance.now(),r=e-t;if(r>v){t=e;const o=Math.round(this._updateInfo.client/(r/1e3)),n=Math.round(this._updateInfo.websocket/(r/1e3));this._updateInfo.client=0,this._updateInfo.websocket=0,this.layerView.emit("update-rate",{client:o,websocket:n})}this.featuresManager.checkForUpdates()}),e)},n.pauseStream=function(){this._clearInterval()},n.resumeStream=function(){this._initUpdateInterval()},t._createClass(o,[{key:"updating",get:function(){return!this.connection||"connected"===this.connection.connectionStatus}}]),o}(a.HandleOwnerMixin(c.EsriPromiseMixin(i))),r.__decorate([l.property()],e.StreamController.prototype,"connection",void 0),r.__decorate([l.property()],e.StreamController.prototype,"layer",void 0),r.__decorate([l.property()],e.StreamController.prototype,"layerView",void 0),r.__decorate([l.property({readOnly:!0})],e.StreamController.prototype,"updating",null),e.StreamController=r.__decorate([y.subclass("esri.layers.graphics.controllers.StreamController")],e.StreamController),Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
