/*
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","../../../core/Evented","../../../core/MapUtils","./GraphicsMap"],(function(e,t,n,i,s){"use strict";let a=function(e){function n(n){var i;return(i=e.call(this)||this)._limit=n,i._all=new s.GraphicsMap,i._active=new h(t._assertThisInitialized(i)),i._pending=new Map,i._handle=i._all.on("change",(e=>i._handleChanges(e))),i}t._inheritsLoose(n,e);var i=n.prototype;return i.destroy=function(){this._handle.remove()},i.toArray=function(){return this._active.toArray()},i.find=function(e){return this._active.find(e)},i.forEach=function(e){this._active.forEach(e)},i.addMany=function(e){this._all.addMany(e)},i.removeManyByObjectId=function(e){this._all.removeManyByObjectId(e)},i._handleChanges=function(e){let t=e.removed;if(this._pending.size>0){t=new Array;for(const n of e.removed)this._pending.delete(n.objectId)||t.push(n)}let n=this._limit-this._active.length+t.length;n<e.added.length&&(this._active.removeMany(t),t=[],o.reset(1-this._limit/(this._active.length+e.added.length)),this._active.forEach((e=>{o.sample()&&(t.push(e),this._pending.set(e.objectId,e))})),n=this._limit-this._active.length+t.length);let i=e.added;if(n<e.added.length){i=new Array,o.reset(n/e.added.length);for(const t of e.added)o.sample()?i.push(t):this._pending.set(t.objectId,t)}const s=n-i.length;s>0&&this._pending.size>0&&(o.reset(s/this._pending.size),this._pending.forEach((e=>{o.sample()&&(i.push(e),this._pending.delete(e.objectId))}))),this._active.addAndRemove(i,t)},t._createClass(n,[{key:"length",get:function(){return this._active.length}}]),n}(n);const o=new(function(){function e(){this.percentage=1,this.last=-1,this.index=0}var t=e.prototype;return t.reset=function(e){this.percentage=e,this.last=-1},t.sample=function(){const e=Math.floor(this.index*this.percentage);return++this.index,e!==this.last&&(this.last=e,!0)},e}());let h=function(){function e(e){this._parent=e,this._map=new Map}var n=e.prototype;return n.forEach=function(e){this._map.forEach((t=>e(t)))},n.find=function(e){let t;return i.someMap(this._map,(n=>!!e(n)&&(t=n,!0))),t},n.toArray=function(){return[...this._map.values()]},n.addAndRemove=function(e,t){for(const n of e)this._map.set(n.objectId,n);for(const n of t)this._map.delete(n.objectId);(e.length>0||t.length>0)&&this._parent.emit("change",{added:e,removed:t})},n.removeMany=function(e){for(const t of e)this._map.delete(t.objectId);e.length>0&&this._parent.emit("change",{added:[],removed:e})},t._createClass(e,[{key:"length",get:function(){return this._map.size}}]),e}();e.LimitGraphicsMap=a,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
