import{isObject as k}from"../../../../../utils/guards/is-object.js";import{reporter as a}from"../../../../tools/notifiers/reporter.js";async function C(u,s){const h=u.split(`
`),c=[],n=[],l={steps:[]},d=/chunk\s*\{(.*)\}/,g=/(?:^|\s)@chunk-end(?!\S)/;for(let e=0;e<h.length;e++){const r=h[e],p=r.match(d);if(p){let t,i=p[1];try{t=JSON.parse(`{${i}}`)}catch{try{i=i.replace(/\}\s*\*\/$/,""),t=JSON.parse(`{${i}}`)}catch(f){await a.panicOnBuildContentError(`Invalid format of the @chunk tag at ${e+1} in ${s}. It should be a valid JSON object. ${f}`),t={steps:[]}}}k(t)||(await a.panicOnBuildContentError(`Invalid format of the @chunk tag at ${e+1} in ${s}. It should be a valid JSON object.`),t={steps:[]}),t.steps&&!Array.isArray(t.steps)&&(await a.panicOnBuildContentError(`Invalid format of the @chunk tag at ${e+1} in ${s}. "steps" should be an array of strings.`),t={...t,steps:[]}),t.steps?l.steps.push(...t.steps):t.steps=[];const o={start:e,condition:t,children:[]};n.length>0?n[n.length-1].children.push(o):c.push(o),n.push(o)}else g.test(r)?n.length>0?n.pop():await a.panicOnBuildContentError(`Unmatched closing tag '@chunk-end' at line ${e+1} in ${s}`):n.length>0?n[n.length-1].children.push(r):c.push(r)}if(n.length>0){let e;for(;e=n.pop();)await a.panicOnBuildContentError(`Unmatched opening tag '@chunk' at line ${e.start+1} in ${s}`)}return{root:c,metadata:l}}export{C as parseFileChunks};
