/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/rottler@1.1.7/src/rottler.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const _checkFunction=(t,e=!0)=>{const s=typeof t;if("function"===s)return!0;if(e)throw new Error(`Expected function but got ${s}`);return!1};class RottlerEntry{constructor(){const t=(new Date).getTime();this.startedAt=t,this.lastUsedAt=0,this.uses=[]}}class Rottler{static ms(t="seconds",e=1){const s=1e3,r=6e4,i=36e5,n=864e5,o=7*n;return{seconds:s,minutes:r,hours:i,days:n,weeks:o,msSeconds:.001,msMinutes:1/r,msHours:1/i,msDays:1/n,msWeeks:1/o}[t]*e}constructor({period:t=6e4,rate:e=10,delay:s=10,timeout:r,throwError:i=!0,synch:n=!1,sleep:o,smooth:h=!1,smoothMinimum:a=.25}={}){if(this.period=t,this.rate=e,this.delay=s,this._entry=new RottlerEntry,this.throwError=i,this.synch=n,this._events={rate:{name:"rate",listener:null},delay:{name:"delay",listener:null}},this.sleep=o,this.synch&&!o)throw new Error("synch option needs a synch sleep function");if(this.synch||(this.setTimeout=r,r||"undefined"==typeof setTimeout||(this.setTimeout=setTimeout),_checkFunction(this.setTimeout)),this.smoothMinimum=a,this.smooth=h,this.smoothMinimum<0||this.smoothMinimum>1)throw new Error("smoothMinimum should be a between 0 and 1")}waiter(t){return new Promise((e=>this.setTimeout((()=>e(t)),t)))}throw({msg:t,throwAsync:e}){const s=new Error(t);if(e)return Promise.reject(s);throw s}get entry(){return this._entry}_cleanEntry(){const t=this.entry,e=this._now()-Math.max(this.period,this.delay);return t.uses=t.uses.filter((t=>t>e)),t}rowIterator({rows:t,transformer:e}){const s=this,r=({waitTime:s,async:r,index:i})=>{const n={index:i,row:t[i],rows:t,waitTime:s,transformation:null,async:r},o={value:n,done:!1};return e&&(n.transformation=e(n)),o};return{[Symbol.asyncIterator]:()=>({rowNumber:0,rows:t,hasNext(){return this.rowNumber<this.rows.length},next(){if(this.hasNext()){const t=s.waitTime();return s.rottle().then((()=>r({waitTime:t,index:this.rowNumber++,async:!0})))}return Promise.resolve({done:!0})}}),[Symbol.iterator]:()=>({rowNumber:0,rows:t,hasNext(){return this.rowNumber<this.rows.length},next(){if(this.hasNext()){const t=s.waitTime();return s.rottle(),r({waitTime:t,index:this.rowNumber++,async:!1})}return{done:!0}}})}}_now(){return(new Date).getTime()}sinceLast(){return this.entry.lastUsedAt?this._now()-this.entry.lastUsedAt:1/0}sinceFirst(){return this.entry.lastUsedAt?this._now()-this.entry.uses[0]:1/0}tooSoon(){return this.sinceLast()<this.delay}size(){return this.entry.uses.length}available(){return this.rate-this.size()}waitTime({noSmooth:t=!1}={}){if(!this.entry.lastUsedAt)return 0;const e=this.sinceLast(),s=this.sinceFirst(),r=this.period-s,i=this.available(),n=i>0?0:r;let o=this.delay-e;const h=i-1,a=h/this.rate;if(this.smooth&&!t&&!n&&this.rate&&h>0&&this.period&&a>this.smoothMinimum){o=Math.max(o,Math.ceil(r/h))}return Math.max(n,o,0)}reset(){return this._entry=new RottlerEntry,this.entry}rottle({noSmooth:t=!1}={}){const e=this.waitTime({noSmooth:t});return this.synch?(e>0&&this.sleep(e),this.use({waitTime:e})):e>0?this.waiter(e).then((()=>this.rottle({noSmooth:!0}))):this.useAsync({waitTime:e})}useAsync(t){return new Promise(((e,s)=>{try{e(this.use(t))}catch(t){s(t)}}))}use({throwAsync:t=!1,waitTime:e}={}){const s=this._now(),r=this._cleanEntry(),i=this.sinceLast();return this.available()<1?(this._events.rate.listener&&this._events.rate.listener(),this.throwError&&this.throw({msg:`Rate limit error - attempt to use  more than ${this.rate} times in ${this.period}ms`,throwAsync:t})):i<this.delay-2?(this._events.delay.listener&&this._events.delay.listener(),this.throwError&&this.throw({msg:`Rate limit delay error - attempt to use ${this.sinceLast()}ms after last - min delay is ${this.delay}ms`,throwAsync:t})):(r.lastUsedAt=s,r.uses.push(s)),{...r,waitTime:e}}ms(...t){return this.constructor.ms(...t)}on(t,e){if(!this._events[t])throw new Error(`event ${t} doesnt exist`);_checkFunction(e),this._events[t].listener=()=>e()}off(t){if(!this._events[t])throw new Error(`event ${t} doesnt exist`);this._events[t].listener=null}}module.exports=Rottler;
//# sourceMappingURL=/sm/a01f79a231f195b03d21ccf9edeabc35af1757a5df78ea2e77e45d623b6ea83d.map