import c from"node:path";import{getNodeAttribute as n}from"../../../../shared/markdoc.js";import{slash as L}from"../../../../shared/utils.js";import{reporter as F}from"../../../utils/reporter/reporter.js";import{MdResolveError as h}from"./md-resolve-error.js";async function V(i,w,b,{actions:{contentDir:m},context:u}){const o=n(i,w);if(!o)return;const r=o.startsWith("/")?o.slice(1):L(c.relative(m,c.resolve(m,c.dirname(b),o)));if(!await u.fs.exists(r)){F.panicOnBuildContentError(`Code snippet file ${r} does not exist`);return}let e=await u.fs.read(r);const d=n(i,"after"),p=n(i,"before"),y=n(i,"from"),C=n(i,"to"),v=n(i,"prefix")||"",s=y??d,f=C??p,g=d!==null,x=p!==null,E=(typeof s=="number"?Math.max(s||0,1):1)+(g?1:0);let a=(typeof f=="number"?f:1/0)-(x?1:0);if(typeof s=="string"){let t=e.indexOf(s);if(t===-1)throw new h(`Cannot find start text "${s}" in snippet file`,{rawLink:r});for(;e[t]!==`
`&&t>0;)t--;typeof a=="number"&&(a-=e.slice(0,t-1).split(`
`).length),e=e.slice(t+1)}if(typeof f=="string"){let t=e.indexOf(f);if(t===-1)throw new h(`Cannot find end text "${p}" in snippet file`,{rawLink:r});e[t-1]===`
`&&t--,e=e.slice(0,t)}const l=e.split(`
`);if(x)for(;l[l.length-1]==="";)l.pop();i.children=[],i.attributes.rawContent=v+l.slice(E-1,a).join(`
`)}export{V as resolveCodeSnippetFromFile};
