import{reporter as r}from"../../../../utils/index.js";import{isObject as k}from"../../../../../shared/jsUtils.js";function C(u,s){const h=u.split(`
`),o=[],n=[],l={steps:[]},d=/chunk\s*\{(.*)\}/,g=/(?:^|\s)@chunk-end(?!\S)/;for(let e=0;e<h.length;e++){const a=h[e],p=a.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){r.panicOnBuildContentError(`Invalid format of the @chunk tag at ${e+1} in ${s}. It should be a valid JSON object. ${f}`),t={steps:[]}}}k(t)||(r.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)&&(r.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 c={start:e,condition:t,children:[]};n.length>0?n[n.length-1].children.push(c):o.push(c),n.push(c)}else g.test(a)?n.length>0?n.pop():r.panicOnBuildContentError(`Unmatched closing tag '@chunk-end' at line ${e+1} in ${s}`):n.length>0?n[n.length-1].children.push(a):o.push(a)}if(n.length>0){let e;for(;e=n.pop();)r.panicOnBuildContentError(`Unmatched opening tag '@chunk' at line ${e.start+1} in ${s}`)}return{root:o,metadata:l}}export{C as parseFileChunks};
