import*as c from"readline";import{Writable as u}from"stream";import l,{LogLevel as r}from"./formatter.js";import{telemetry as h}from"../../../cli/telemetry/index.js";import{isProductionMode as S}from"../../../shared/env.js";import{gray as f}from"./colors.js";import{exit as d}from"../process.js";import{isDevelopMode as g}from"../is-develop-mode.js";const k=[{id:"sep",content:`
`},{id:"sep2",content:f("\u2500".repeat(Math.round(process.stderr.columns/2)))+`
`},{id:"sep3",content:`
`}];class p{globalStickyLines=[];prevStickyLines=0;context;isProdMode;timers=new Map;timeouts=new Map;disableSticky=!1;constructor(t){this.context=t,this.isProdMode=S()}panic(t,...e){t instanceof Error&&(t=t.message+`
`+t.stack),this.error(t,...e),h.send("cli_error_caught",{message:t}),d(1)}panicOnContentError(t,...e){t instanceof Error&&(t=t.message+`
`+t.stack),this.contentError(t,...e),h.send("cli_error_caught",{message:t,scope:"content"}),d(1)}info(t,...e){this.log({level:r.INFO,message:t,args:e})}infoTime(t,e,...i){return this.logTiming(r.INFO,e,t,...i)}success(t,...e){this.log({level:r.SUCCESS,message:t,args:e})}logSticky(t,e,...i){const o=this.globalStickyLines.find(s=>s.id===t),n=l.interpolate(e,...i)+`
`;if(!this.isInteractive()){this.log({level:r.INFO,message:e,args:i});return}o?o.content=n:(this.globalStickyLines.length===0&&this.globalStickyLines.push(...k),this.globalStickyLines.push({id:t,content:n}));for(let s=0;s<this.prevStickyLines;s++)c.moveCursor(process.stderr,0,-1),c.clearLine(process.stderr,1);this.printSticky(),this.prevStickyLines=this.globalStickyLines.length}successTime(t,e,...i){return this.logTiming(r.SUCCESS,e,t,...i)}warn(t,...e){this.log({level:r.WARN,message:t,args:e})}warnProd(t,...e){this.isProdMode?this.warn(t,...e):this.verbose(t,...e)}error(t,...e){this.log({level:r.ERROR,message:t,args:e})}contentError(t,...e){this.log({level:r.ERROR,message:t,scope:"content",args:e})}verbose(t,...e){process.env.REPORTER_VERBOSE==="true"&&this.log({level:r.VERBOSE,message:t,args:e})}verboseTime(t,e,...i){if(process.env.REPORTER_VERBOSE==="true")return this.logTiming(r.VERBOSE,e,t,...i)}httpTime(t){return this.logTiming(r.HTTP,"",t)}startTiming(t){const e=t||Symbol();this.timers.set(e,performance.now());const i=setTimeout(()=>{this.timers.delete(e),this.timeouts.delete(e)},500*1e3);return this.timeouts.set(e,i),e}updateContext(t){this.context={...this.context,...t}}clearAllTimeouts(){for(const t of this.timeouts.values())clearTimeout(t);this.timeouts.clear()}logTiming(t,e,i,...o){const n=this.timers.get(i);if(!n)return;const s=Math.round(performance.now()-n);return this.timers.delete(i),this.log({level:t,message:e,duration:s,args:o}),{message:e,time_ms:s}}setStickyState({globalStickyLines:t,prevStickyLines:e}){this.globalStickyLines=t,this.prevStickyLines=e}getStickyState(){return{globalStickyLines:this.globalStickyLines,prevStickyLines:this.prevStickyLines}}printSticky(){this.globalStickyLines.forEach(t=>{process.stderr.write(t.content)})}clearSticky(){if(this.isInteractive())for(let t=0;t<this.prevStickyLines;t++)c.moveCursor(process.stderr,0,-1),c.clearLine(process.stderr,1)}log({level:t,message:e,duration:i,scope:o,args:n}){!this.disableSticky&&this.isInteractive()&&this.clearSticky();let s=e&&l.interpolate(e,...n);s&&i!=null&&t!==r.VERBOSE&&h.send("timing_performed",{time_ms:i,message:s});const a={level:t,message:s,duration:i,scope:o,context:this.context};process.stderr.write(l.format(a)),!this.disableSticky&&this.isInteractive()&&this.printSticky()}isInteractive(){return!!(process.stderr&&process.stderr.isTTY&&process.env.TERM!=="dumb"&&!("CI"in process.env)&&g())}patchConsole(){for(const t of v)console[t]=(...e)=>{!this.disableSticky&&this.isInteractive()&&this.clearSticky();const i=[],o=new u({write(a,E,m){i.push(a.toString()),m()}});new console.Console({stdout:o,stderr:o})[t](...e);let s="";t==="table"||t==="dir"?s=`
${i.join("").slice(0,-1)}`:s=i.join("").slice(0,-1),process.stderr.write(l.format({level:p.getLogLevel(t),message:s})),!this.disableSticky&&this.isInteractive()&&this.printSticky()}}static getLogLevel(t){switch(t){case"error":return r.ERROR;case"warn":return r.WARN;case"debug":case"trace":return r.VERBOSE;case"info":case"log":case"dir":case"table":case"assert":return r.INFO;default:const e=t;throw new Error(`Unhandled method: ${e}`)}}}const y=new p;var I=y;const v=["log","warn","error","info","debug","trace","dir","table","assert"];export{p as Logger,I as default};
