var h={},_,l;function R(){if(l)return _;l=1;function i(n,t){typeof t=="boolean"&&(t={forever:t}),this._originalTimeouts=JSON.parse(JSON.stringify(n)),this._timeouts=n,this._options=t||{},this._maxRetryTime=t&&t.maxRetryTime||1/0,this._fn=null,this._errors=[],this._attempts=1,this._operationTimeout=null,this._operationTimeoutCb=null,this._timeout=null,this._operationStart=null,this._timer=null,this._options.forever&&(this._cachedTimeouts=this._timeouts.slice(0))}return _=i,i.prototype.reset=function(){this._attempts=1,this._timeouts=this._originalTimeouts.slice(0)},i.prototype.stop=function(){this._timeout&&clearTimeout(this._timeout),this._timer&&clearTimeout(this._timer),this._timeouts=[],this._cachedTimeouts=null},i.prototype.retry=function(n){if(this._timeout&&clearTimeout(this._timeout),!n)return!1;var t=new Date().getTime();if(n&&t-this._operationStart>=this._maxRetryTime)return this._errors.push(n),this._errors.unshift(new Error("RetryOperation timeout occurred")),!1;this._errors.push(n);var r=this._timeouts.shift();if(r===void 0)if(this._cachedTimeouts)this._errors.splice(0,this._errors.length-1),r=this._cachedTimeouts.slice(-1);else return!1;var e=this;return this._timer=setTimeout(function(){e._attempts++,e._operationTimeoutCb&&(e._timeout=setTimeout(function(){e._operationTimeoutCb(e._attempts)},e._operationTimeout),e._options.unref&&e._timeout.unref()),e._fn(e._attempts)},r),this._options.unref&&this._timer.unref(),!0},i.prototype.attempt=function(n,t){this._fn=n,t&&(t.timeout&&(this._operationTimeout=t.timeout),t.cb&&(this._operationTimeoutCb=t.cb));var r=this;this._operationTimeoutCb&&(this._timeout=setTimeout(function(){r._operationTimeoutCb()},r._operationTimeout)),this._operationStart=new Date().getTime(),this._fn(this._attempts)},i.prototype.try=function(n){console.log("Using RetryOperation.try() is deprecated"),this.attempt(n)},i.prototype.start=function(n){console.log("Using RetryOperation.start() is deprecated"),this.attempt(n)},i.prototype.start=i.prototype.try,i.prototype.errors=function(){return this._errors},i.prototype.attempts=function(){return this._attempts},i.prototype.mainError=function(){if(this._errors.length===0)return null;for(var n={},t=null,r=0,e=0;e<this._errors.length;e++){var o=this._errors[e],a=o.message,u=(n[a]||0)+1;n[a]=u,u>=r&&(t=o,r=u)}return t},_}var y;function d(){return y||(y=1,(function(i){var n=R();i.operation=function(t){var r=i.timeouts(t);return new n(r,{forever:t&&(t.forever||t.retries===1/0),unref:t&&t.unref,maxRetryTime:t&&t.maxRetryTime})},i.timeouts=function(t){if(t instanceof Array)return[].concat(t);var r={retries:10,factor:2,minTimeout:1*1e3,maxTimeout:1/0,randomize:!1};for(var e in t)r[e]=t[e];if(r.minTimeout>r.maxTimeout)throw new Error("minTimeout is greater than maxTimeout");for(var o=[],a=0;a<r.retries;a++)o.push(this.createTimeout(a,r));return t&&t.forever&&!o.length&&o.push(this.createTimeout(a,r)),o.sort(function(u,s){return u-s}),o},i.createTimeout=function(t,r){var e=r.randomize?Math.random()+1:1,o=Math.round(e*Math.max(r.minTimeout,1)*Math.pow(r.factor,t));return o=Math.min(o,r.maxTimeout),o},i.wrap=function(t,r,e){if(r instanceof Array&&(e=r,r=null),!e){e=[];for(var o in t)typeof t[o]=="function"&&e.push(o)}for(var a=0;a<e.length;a++){var u=e[a],s=t[u];t[u]=(function(v){var f=i.operation(r),m=Array.prototype.slice.call(arguments,1),g=m.pop();m.push(function(p){f.retry(p)||(p&&(arguments[0]=f.mainError()),g.apply(this,arguments))}),f.attempt(function(){v.apply(t,m)})}).bind(t,s),t[u].options=r}}})(h)),h}var c,T;function C(){return T||(T=1,c=d()),c}export{C as r};
