import{isBuildMode as h}from"../../utils/envs/is-build-mode.js";import{logger as e}from"../../tools/notifiers/logger.js";import d from"./formatter.js";import{blue as g,gray as p,red as c}from"./helpers/colors.js";import{isVirtualFile as P}from"../../fs/utils/isVirtualFile.js";const a=30;class E{#t=[];#e=[];#o=new Map;#r=[];pushError(t,r,...o){const i=d.interpolate(t,...o);this.#t.push({severity:r,message:i,type:"ERROR"})}reportBrokenLink(t){this.#t.push(t)}reportCompilationError(t){const r=`${t.message}::${t.sourceFileRelativePath}::${t?.sourceFileLocation?.line}`;this.#o.has(r)||this.#o.set(r,t)}reportPageRenderError(t){this.#r.push(t)}panicOnBuild(t,...r){t instanceof Error&&(t=t.message+`
`+t.stack),h()?e.panic(t,...r):this.pushError(t,"PANIC",...r)}async panicOnBuildContentErrorForRealFile(t,r,o,...i){await P(r,o)||this.panicOnBuildContentError(t,...i)}panicOnBuildContentError(t,...r){t instanceof Error&&(t=t.message+`
`+t.stack),h()?e.panicOnContentError(t,...r):this.pushError(t,"PANIC",...r)}reportMarkdocProblem(t){this.#e.push(t)}clearErrors(){this.#t=[]}clearMarkdocProblems(){this.#e=[]}clearEsbuildProblems(){this.#o.clear()}clearPageRenderProblems(){this.#r=[]}getCompilationProblem(t){return this.#o.get(t)}getCompilationProblems(){return[...this.#o.values()]}getPageRenderProblems(){return this.#r}getPageRenderProblem(t){return this.#r.find(r=>r.sourceFileRelativePath===t)}getProblems(){return[...this.#t,...this.getCompilationProblems(),...this.#e,...this.#r]}summary(t,r=0){const o=this.getProblems(),i=this.#e.filter(l=>l.type==="BROKEN_LINK").length,n=this.#t.filter(l=>l.type==="BROKEN_LINK").length,m=this.#e.length-i,f=this.#t.length-n+this.getCompilationProblems().length+this.#r.length;return e.logSticky("validate",o.length?c("  \u274C Status: %s markdoc errors, %s broken links, %s other errors"):"  \u2705 Status: No errors found",m,i+n,f),r!==0&&e.logSticky("pages",`  \u{1F4C4} Total pages: ${r}`),e.logSticky("timing",t),e.logSticky("validate-sep",""),e.isInteractive()&&(o.length?e.logSticky("actions","Press (e) to print all errors, (q) to quit"):e.logSticky("actions",p("Press (q) to quit"))),o}printErrors(t=[]){e.clearSticky(),e.disableSticky=!0;const r=this.#t.length,o=t.length?t:this.getProblems();let i=0;for(const n of o.slice(0,a))i>=r?e.contentError(c(`[${++i}] `)+u(n)):e.error(c(`[${++i}] `)+u(n));o.length>a&&e.error(`... and ${o.length-a} more errors`),e.printSticky(),e.disableSticky=!1}listenStdin(){if(!e.isInteractive())return;process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.setEncoding("utf8");const t=this.printErrors.bind(this);process.stdin.on("data",function(r){if(r===""&&process.exit(),r==="\x7F"){process.stdout.write("\b \b");return}if(r==="\r"){e.clearSticky(),process.stdout.write(`
`),e.printSticky();return}if(r==="e"){t();return}r==="q"&&process.exit(0)})}}const C=new E;function u(s){const t=s.sourceFileLocation,r=t?`:${t.line}:${t.character??1}`:"";return s.message+(s.codeframe?`

`+s.codeframe+`

`:`
`)+(s.sourceFileRelativePath?p(`at ${g("./"+s.sourceFileRelativePath)}${r}`)+`
`:"")}export{E as Reporter,C as reporter};
