/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../core/CircularArray","../../../core/mathUtils","../../../core/maybe"],(function(t,e,s,i){"use strict";const r="__esri_stream_id__",o="__esri_timestamp__",n=1e3;let a=function(){function t(t,e,s,i,o=128){this._trackIdToObservations=new Map,this._idCounter=0,this._lastPurge=performance.now(),this._addOrUpdated=new Map,this._removed=[],this._maxAge=0,this._timeInfo=s,this._purgeOptions=i,this.store=t,this.objectIdField=e,this.purgeInterval=o,this._useGeneratedIds=this.objectIdField===r}var a=t.prototype;return a.add=function(t){if(this._useGeneratedIds){const e=this._nextId();t.attributes[this.objectIdField]=e,t.objectId=e}else t.objectId=t.attributes[this.objectIdField];if(this._addOrUpdated.set(t.objectId,t),this._maxAge=Math.max(this._maxAge,t.attributes[this._timeInfo.startTimeField]),!this._timeInfo.trackIdField)return i.isNone(this._trackIdLessObservations)&&(this._trackIdLessObservations=new e(1e5)),void this._trackIdLessObservations.enqueue(t.objectId);const r=t.attributes[this._timeInfo.trackIdField];if(!this._trackIdToObservations.has(r)){const t=i.isSome(this._purgeOptions)&&null!=this._purgeOptions.maxObservations?this._purgeOptions.maxObservations:n,o=s.clamp(t,0,n);this._trackIdToObservations.set(r,new e(o))}const o=this._trackIdToObservations.get(r).enqueue(t.objectId);i.isSome(o)&&(this._addOrUpdated.has(o)?this._addOrUpdated.delete(o):this._removed.push(o))},a.checkForUpdates=function(){const t=this._getToAdd(),e=this._getToRemove(),s=performance.now();s-this._lastPurge>=this.purgeInterval&&(this._purge(s),this._lastPurge=s);const r=[];if(i.isSome(e))for(const o of e){const t=this.store.removeById(o);i.isSome(t)&&r.push(t)}if(i.isSome(t))for(const i of t)i.attributes[o]=s,this.store.add(i);(t||r)&&this.store.update(t,r)},a._getToAdd=function(){if(!this._addOrUpdated.size)return null;const t=new Array(this._addOrUpdated.size);let e=0;return this._addOrUpdated.forEach((s=>t[e++]=s)),this._addOrUpdated.clear(),t},a._getToRemove=function(){const t=this._removed;return this._removed.length?(this._removed=[],t):null},a._nextId=function(){const t=this._idCounter;return this._idCounter=(this._idCounter+1)%4294967294+1,t},a._purge=function(t){const e=this._purgeOptions;i.isSome(e)&&(this._purgeSomeByDisplayCount(e),this._purgeByAge(e),this._purgeByAgeReceived(t,e),this._purgeTracks())},a._purgeSomeByDisplayCount=function(t){if(!t.displayCount)return;let e=this.store.size;if(e>t.displayCount){if(this._timeInfo.trackIdField)for(const s of this._trackIdToObservations.values())if(e>t.displayCount&&s.size){const t=i.unwrap(s.dequeue());this._removed.push(t),e--}if(i.isSome(this._trackIdLessObservations)){let s=e-t.displayCount;for(;s-- >0;){const t=this._trackIdLessObservations.dequeue();i.isSome(t)&&this._removed.push(t)}}}},a._purgeByAge=function(t){var e;if(!t.age||null==(e=this._timeInfo)||!e.startTimeField)return;const s=60*t.age*1e3,i=this._maxAge-s;this.store.forEach((t=>{t.attributes[this._timeInfo.startTimeField]<i&&this._removed.push(t.objectId)}))},a._purgeByAgeReceived=function(t,e){if(!e.ageReceived)return;const s=t-60*e.ageReceived*1e3;this.store.forEach((t=>{t.attributes[o]<s&&this._removed.push(t.objectId)}))},a._purgeTracks=function(){this._trackIdToObservations.forEach(((t,e)=>{0===t.size&&this._trackIdToObservations.delete(e)}))},t}();t.DEFAULT_STREAM_ID_FIELD=r,t.ESRI_TIMESTAMP=o,t.StreamFeatureManager=a,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
