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