import{isBuildMode as u}from"../is-build-mode.js";import p from"./formatter.js";import e from"./logger.js";import{blue as f,gray as h,red as c}from"./colors.js";const a=30;class P{errors=[];markdocProblems=[];esbuildProblems=new Map;pushError(r,t,...o){const s=p.interpolate(r,...o);this.errors.push({severity:t,message:s,type:"ERROR"})}reportBrokenLink(r){this.errors.push(r)}reportEsbuildError(r){const t=`${r.message}::${r.sourceFileRelativePath}::${r?.sourceFileLocation?.line}`;this.esbuildProblems.has(t)||this.esbuildProblems.set(t,r)}panicOnBuild(r,...t){r instanceof Error&&(r=r.message+`
`+r.stack),u()?e.panic(r,...t):this.pushError(r,"PANIC",...t)}panicOnBuildContentError(r,...t){r instanceof Error&&(r=r.message+`
`+r.stack),u()?e.panicOnContentError(r,...t):this.pushError(r,"PANIC",...t)}reportMarkdocProblem(r){this.markdocProblems.push(r)}clearErrors(){this.errors=[]}clearMarkdocProblems(){this.markdocProblems=[]}clearEsbuildProblems(){this.esbuildProblems.clear()}getEsbuildProblem(r){return this.esbuildProblems.get(r)}getEsbuildProblems(){return[...this.esbuildProblems.values()]}getProblems(){return[...this.errors,...this.getEsbuildProblems(),...this.markdocProblems]}summary(r,t=0){const o=this.getProblems(),s=this.markdocProblems.filter(l=>l.type==="BROKEN_LINK").length,n=this.errors.filter(l=>l.type==="BROKEN_LINK").length,m=this.markdocProblems.length-s,b=this.errors.length-n+this.getEsbuildProblems().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,s+n,b),t!==0&&e.logSticky("pages",`  \u{1F4C4} Total pages: ${t}`),e.logSticky("timing",r),e.logSticky("validate-sep",""),e.isInteractive()&&(o.length?e.logSticky("actions","Press (e) to print all errors, (q) to quit"):e.logSticky("actions",h("Press (q) to quit"))),o}printErrors(r=[]){e.clearSticky(),e.disableSticky=!0;const t=this.errors.length,o=r.length?r:[...this.errors,...this.getEsbuildProblems(),...this.markdocProblems];let s=0;for(const n of o.slice(0,a))s>=t?e.contentError(c(`[${++s}] `)+d(n)):e.error(c(`[${++s}] `)+d(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 r=this.printErrors.bind(this);process.stdin.on("data",function(t){if(t===""&&process.exit(),t==="\x7F"){process.stdout.write("\b \b");return}if(t==="\r"){e.clearSticky(),process.stdout.write(`
`),e.printSticky();return}if(t==="e"){r();return}t==="q"&&process.exit(0)})}}const y=new P;function d(i){const r=i.sourceFileLocation,t=r?`:${r.line}:${r.character??1}`:"";return i.message+(i.codeframe?`

`+i.codeframe+`

`:`
`)+(i.sourceFileRelativePath?h(`at ${f("./"+i.sourceFileRelativePath)}${t}`)+`
`:"")}export{P as Reporter,y as reporter};
