import y from"@markdoc/markdoc";import{isString as A}from"@redocly/theme/core/openapi";import{getTagOrGroup as S}from"../../models/index.js";import{safeSlugify as h,unescapeHTMLChars as T}from"../../utils/index.js";import{simplifyAstStructure as d}from"../../utils/simplifyAstStructure.js";function O(e,u,f){const i=A(e)?d(y.parse(e)):y.Ast.fromJSON(JSON.stringify(e?.result)),s=[];let o;const l=(t,n,g=s,r,p)=>{t=T(t);let a;r.attributes.id?a=r.attributes.id:(a=p?`${p}/${h(t)}`:`section/${h(t)}`,r.attributes.id=a);const b={id:a.toLowerCase(),name:t,level:n,items:[],ast:[r]};return g.push(b),b},c=(t,n)=>{t[t.length-1]?.ast?.push(n)},m=t=>{const n=C(t)||"",r=u&&u.type==="tag"?u.id:void 0;t.type==="heading"&&t.attributes.level===1?o=l(n,t.attributes.level,void 0,t,r):t.attributes.level?l(n,t.attributes.level,o?.items,t,o?.id??r):o?.items?.length?c(o?.items,t):s?.length&&c(s,t)};if(Array.isArray(i))for(const t of i)m(t);else m(i);return v(u,s,f)}function v(e,u,f=1){return u.map(i=>{const s=S("section",i,e);return s.depth=f,i.items&&(s.items=v(s,i.items,f+1)),s})}function C(e){if(typeof e?.attributes?.content=="string")return e.attributes?.content;if(e?.children)return C(e.children[0])}export{O as addMarkdownItems};
