import*as g from"esbuild";import a from"../utils/reporter/logger.js";import{reporter as f}from"../utils/reporter/reporter.js";import{isBuildMode as E}from"../utils/is-build-mode.js";import{exit as x}from"../utils/process.js";import{telemetry as u}from"../../cli/telemetry/index.js";import{stopAllCompilers as y}from"./esbuild.js";import{getCodeframe as L}from"../utils/codeframes/codeframes.js";class N extends Error{errors;warnings;constructor({errors:r=[],warnings:e=[]}){super(),this.errors=r,this.warnings=e}}async function S({errors:n,warnings:r}){const e=await d(n,"error"),s=await d(r,"warning");r.forEach((t,o)=>{a.warn(s[o])}),n.forEach((t,o)=>{const i=t.location?.file||"";i&&i.includes("node_modules/")?(a.error(e[o]),u.send("cli_error_caught",{message:JSON.stringify(t)})):(a.contentError(e[o]),u.send("cli_error_caught",{message:JSON.stringify(t),scope:"content"}))}),n.length&&E()&&(await y(),x(1))}async function d(n,r){switch(process.env.PORTAL_LOG_FORMAT){case"JSON":return n.map(e=>JSON.stringify(e));default:return await g.formatMessages(n,{kind:r,color:!1,terminalWidth:0})}}function O(n,r){if(!r?.lineText)return{line:1,column:1,lines:n.split(`
`),hasMatched:!1};const e=n.split(`
`),{lineText:s}=r,t=i=>{const l=i.indexOf(s);return l===-1?null:l};if(r.line&&r.line<=e.length&&t(e[r.line-1]))return{line:r.line,column:r.column||1,lines:e,hasMatched:!0};const o=e.findIndex(i=>t(i)!==null);if(o>=0){const i=t(e[o]);return{line:o+1,column:(i??0)+1,lines:e,hasMatched:!0}}return{line:1,column:1,lines:e,hasMatched:!1}}function A(n,r,e,s){const{line:t,column:o,lines:i,hasMatched:l}=O(e,s),{frame:m,start:h}=L({start:{line:t,character:o},end:{line:t,character:o+(s?.lineText?.length||0)},lines:i},!0),p=`${n}::${r}::${t}`,c={message:n,sourceFileRelativePath:r,sourceFileLocation:h,codeframe:m,hasMatched:l};return f.getEsbuildProblem(p)||f.reportEsbuildError({type:"ERROR",severity:"ERROR",...c}),c}export{N as EsbuildError,S as esbuildLogger,A as reportEsbuildError};
