/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../chunks/_rollupPluginBabelHelpers","../support/FeatureSet","../support/IdSet","../support/shared"],(function(t,e,n,s){"use strict";let i=function(i){function r(t){var e;return(e=i.call(this,t)||this)._topnum=0,e.declaredClass="esri.arcade.featureset.actions.Top",e._countedin=0,e._maxProcessing=100,e._topnum=t.topnum,e._parent=t.parentfeatureset,e}t._inheritsLoose(r,i);var a=r.prototype;return a._getSet=function(){var e=t._asyncToGenerator((function*(t){if(null===this._wset){yield this._ensureLoaded();const e=yield this._parent._getSet(t);return this._wset=new n(e._candidates.slice(0),e._known.slice(0),!1,this._clonePageDefinition(e.pagesDefinition)),this._setKnownLength(this._wset)>this._topnum&&(this._wset._known=this._wset._known.slice(0,this._topnum)),this._setKnownLength(this._wset)>=this._topnum&&(this._wset._candidates=[]),this._wset}return this._wset}));function s(t){return e.apply(this,arguments)}return s}(),a._setKnownLength=function(t){return t._known.length>0&&"GETPAGES"===t._known[t._known.length-1]?t._known.length-1:t._known.length},a._isInFeatureSet=function(t){const e=this._parent._isInFeatureSet(t);if(e===s.IdState.NotInFeatureSet)return e;const n=this._idstates[t];return n===s.IdState.InFeatureSet||n===s.IdState.NotInFeatureSet?n:e===s.IdState.InFeatureSet&&void 0===n?this._countedin<this._topnum?(this._idstates[t]=s.IdState.InFeatureSet,this._countedin++,s.IdState.InFeatureSet):(this._idstates[t]=s.IdState.NotInFeatureSet,s.IdState.NotInFeatureSet):s.IdState.Unknown},a._expandPagedSet=function(){var e=t._asyncToGenerator((function*(t,e,n,s,i){if(null===this._parent)throw new Error("Parent Paging not implemented");if(e>this._topnum&&(e=this._topnum),this._countedin>=this._topnum&&t.pagesDefinition.internal.set.length<=t.pagesDefinition.resultOffset){let e=t._known.length;return e>0&&"GETPAGES"===t._known[e-1]&&(t._known.length=e-1),e=t._candidates.length,e>0&&"GETPAGES"===t._candidates[e-1]&&(t._candidates.length=e-1),"success"}const r=yield this._parent._expandPagedSet(t,e,n,s,i);return this._setKnownLength(t)>this._topnum&&(t._known.length=this._topnum),this._setKnownLength(t)>=this._topnum&&(t._candidates.length=0),r}));function n(t,n,s,i,r){return e.apply(this,arguments)}return n}(),a._getFeatures=function(){var e=t._asyncToGenerator((function*(t,e,s,i){const r=[],a=this._maxQueryRate();if(!0===this._checkIfNeedToExpandKnownPage(t,a))return yield this._expandPagedSet(t,a,0,0,i),this._getFeatures(t,e,s,i);-1!==e&&void 0===this._featureCache[e]&&r.push(e);let o=0;for(let n=t._lastFetchedIndex;n<t._known.length&&(o++,o<=s&&(t._lastFetchedIndex+=1),!(void 0===this._featureCache[t._known[n]]&&(t._known[n]!==e&&r.push(t._known[n]),r.length>a)));n++);if(0===r.length)return"success";const u=new n([],r,!1,null),_=Math.min(r.length,s);yield this._parent._getFeatures(u,-1,_,i);for(let n=0;n<_;n++){const t=this._parent._featureFromCache(r[n]);void 0!==t&&(this._featureCache[r[n]]=t)}return"success"}));function s(t,n,s,i){return e.apply(this,arguments)}return s}(),a._getFilteredSet=function(){var e=t._asyncToGenerator((function*(t,e,s,i,r){yield this._ensureLoaded();const a=yield this._getSet(r);return new n(a._candidates.slice(0).concat(a._known.slice(0)),[],!1,this._clonePageDefinition(a.pagesDefinition))}));function s(t,n,s,i,r){return e.apply(this,arguments)}return s}(),a._refineKnowns=function(t,e){let n=0,i=null;const r=[];for(let a=0;a<t._candidates.length;a++){const o=this._isInFeatureSet(t._candidates[a]);if(o===s.IdState.InFeatureSet){if(t._known.push(t._candidates[a]),n+=1,null===i?i={start:a,end:a}:i.end===a-1?i.end=a:(r.push(i),i={start:a,end:a}),t._known.length>=this._topnum)break}else if(o===s.IdState.NotInFeatureSet)null===i?i={start:a,end:a}:i.end===a-1?i.end=a:(r.push(i),i={start:a,end:a}),n+=1;else if(o===s.IdState.Unknown)break;if(n>=e)break}null!==i&&r.push(i);for(let s=r.length-1;s>=0;s--)t._candidates.splice(r[s].start,r[s].end-r[s].start+1);this._setKnownLength(t)>this._topnum&&(t._known=t._known.slice(0,this._topnum)),this._setKnownLength(t)>=this._topnum&&(t._candidates=[])},a._stat=function(){var e=t._asyncToGenerator((function*(){return{calculated:!1}}));function n(){return e.apply(this,arguments)}return n}(),a._canDoAggregates=function(){var e=t._asyncToGenerator((function*(){return!1}));function n(){return e.apply(this,arguments)}return n}(),r.registerAction=function(){e._featuresetFunctions.top=function(t){return new r({parentfeatureset:this,topnum:t})}},r}(e);return i}));
