import l from"node:path";import{MARKDOC_ERROR_TYPES as s}from"../../../constants/common.js";import{slash as m}from"../../../utils/path/slash.js";import{getCodeframe as f}from"../../utils/codeframes/codeframes.js";function L(e,r,n){const{frame:t,start:a}=f(h(e,n),!0);return{type:e.meta?e.meta?.brokenLinkType?"BROKEN_LINK":"RESOLVE":"MARKDOC",brokenLinkType:e.meta?.brokenLinkType,codeframe:t,message:e.error.message,sourceFileRelativePath:r,sourceFileLocation:a,...e.meta}}function o(e,r,n){const t=e?.line??r?.line??n?.line,a=e?.character??r?.character??n?.character;return{line:t!==void 0?t+1:0,character:a!==void 0?a+1:void 0}}function h(e,r){const n=o(e.location?.start,e.error.location?.start,{line:e.lines[0]}),t=o(e.location?.end,e.error.location?.end,{line:e.lines[1]});t.line--;const a=r.split(`
`);switch(e.type){case"resolve_link":const c=e.meta?.rawLink;let i=n.line;for(;i<t.line&&!(a[i-1].indexOf(c)>-1);)i++;if(n.line=i,i<1)break;n.character=n.character??a[i-1].indexOf(c)+1,t.line=i,t.character=t.character??n.character+c.length;break;default:n.character=n.character??1,t.character=t.character??(a[t.line-1]?.length||0)+1}return{start:n,end:t,lines:a}}function R(e,r,n){const t=l.relative(n,e.location[0]?.source.absoluteRef),c=m(t)===r?"":e.location[0]?.pointer;return`${e.message.replaceAll(n+"/","")} in ${t}${c?` at ${c}`:""}`}function E(e){return"type"in e&&s.includes(e.type)}export{R as formatBundleFromMarkdocError,L as formatMarkdocError,E as isMarkdocError,h as normalizeErrorLocation};
