import{HttpError as t}from"@clevercloud/client/esm/streams/clever-cloud-sse.js";import{Buffer as e}from"../buffer.js";import{LogsProgress as s}from"./logs-progress.js";const r="cc-logs-stream-closed-by-user";class i{#t;#e;#s;#r;#i;#o;constructor(t,{waitingTimeout:r}={}){this.#e=new e(this._appendLogs.bind(this),{timeout:1e3,length:10}),this.#r={type:"idle"},this.#s=new s(t-10),this.#i=new o(this.#a.bind(this)),this.#o={live:2e3,cold:8e3,...r??{}}}_createStream(t,e,s,r){throw new Error("Abstract method. Implement me!")}_convertLog(t){throw new Error("Abstract method. Implement me!")}openLogsStream(t){this.stop(),this._updateStreamState({type:"connecting"}),this.#n(t)}stop(){this.#i.stop(),this.#t?.close({type:r}),this.#e.clear(),this.#s.reset(),this._updateStreamState({type:"idle"})}pause(){this.#h("user")}resume(){"paused"===this.#r.type&&(this._updateStreamState(this.#g()),this.#t.resume())}acceptOverflow(){"paused"===this.#r.type&&"overflow"===this.#r.reason&&this.resume()}discardOverflow(){"paused"===this.#r.type&&"overflow"===this.#r.reason&&this.#t?.close({type:"cc-logs-stream-closed-after-discard-overflow"})}complete(){this.#i.stop(),this.#t?.close({type:"cc-logs-stream-closed-by-completed"}),this.#e.flush(),this.#s.complete(),this._updateStreamState({type:"completed",progress:this.#s.getProgress(),overflowing:this.#s.isOverflowing()})}getLastLogDate(){return this.#s.getLastLogDate()}_updateStreamState(t){this.#r=t}_appendLogs(t){this.#s.progress(t)?this.#h("overflow"):"paused"!==this.#r.type&&this._updateStreamState(this.#g())}#n(t){this.#t=this._createStream(t,10,1e3,10).on("open",(()=>this.#p(t))).onLog(this.#m.bind(this)).on("error",this.#l.bind(this)),this.#t.start().then(this.#u.bind(this)).catch(this.#c.bind(this))}#p(t){"connecting"===this.#r.type&&(this.#s.start(t),this.#i.start(this.#s.isLive()?this.#o.live:this.#o.cold))}async#m(t){this.#i.stop();const e=await this._convertLog(t);null!=e&&(this.#s.isEmpty()?this._appendLogs([e]):this.#e.add(e))}#u(t){this.#e.flush(),t.type!==r&&this.complete()}#l(t){this.#t.retryCount>=3&&console.log("received an `error` event from log stream",t)}#c(e){console.error(e),e instanceof t&&404===e.status&&!this.#s.isLive()?this.complete():this._updateStreamState({type:"error"})}#a(){this.#s.isLive()?this._updateStreamState({type:"waitingForFirstLog"}):this.complete()}#h(t){"connecting"!==this.#r.type&&"waitingForFirstLog"!==this.#r.type&&"running"!==this.#r.type||(this.#t?.pause(),this.#e.flush(),"user"===t?this._updateStreamState({type:"paused",reason:"user",progress:this.#s.getProgress(),overflowing:this.#s.isOverflowing()}):this._updateStreamState({type:"paused",reason:"overflow",progress:this.#s.getProgress()}))}#g(){return{type:"running",progress:this.#s.getProgress(),overflowing:this.#s.isOverflowing()}}}class o{#d;constructor(t){this._callback=t}start(t){null!=this.#d&&stop(),this.#d=setTimeout((()=>{this._callback(),this.#d=null}),t)}stop(){null!=this.#d&&(clearTimeout(this.#d),this.#d=null)}}export{i as LogsStream};
//# sourceMappingURL=logs-stream.js.map
