"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.addMarkdownItems=addMarkdownItems;const markdoc_1=require("@markdoc/markdoc"),theme_1=require("@redocly/theme"),models_1=require("../../models"),utils_1=require("../../utils"),simplifyAstStructure_1=require("../../utils/simplifyAstStructure");function addMarkdownItems(t,e,i){const r=(0,theme_1.isString)(t)?(0,simplifyAstStructure_1.simplifyAstStructure)((0,markdoc_1.parse)(t)):markdoc_1.Ast.fromJSON(JSON.stringify(null==t?void 0:t.result)),s=[];let l;const n=(t,e,i=s,r,l)=>{let n;t=(0,utils_1.unescapeHTMLChars)(t),r.attributes.id?n=r.attributes.id:(n=l?`${l}/${(0,utils_1.safeSlugify)(t)}`:`section/${(0,utils_1.safeSlugify)(t)}`,r.attributes.id=n);const o={id:n.toLowerCase(),name:t,level:e,items:[],ast:[r]};return i.push(o),o},o=(t,e)=>{var i,r;null===(r=null===(i=t[t.length-1])||void 0===i?void 0:i.ast)||void 0===r||r.push(e)},u=t=>{var i,r;const u=getMarkdownContent(t)||"",d=e&&"tag"===e.type?e.id:void 0;"heading"===t.type&&1===t.attributes.level?l=n(u,t.attributes.level,void 0,t,d):t.attributes.level?n(u,t.attributes.level,null==l?void 0:l.items,t,null!==(i=null==l?void 0:l.id)&&void 0!==i?i:d):(null===(r=null==l?void 0:l.items)||void 0===r?void 0:r.length)?o(null==l?void 0:l.items,t):(null==s?void 0:s.length)&&o(s,t)};if(Array.isArray(r))for(const t of r)u(t);else u(r);return mapHeadingsDeep(e,s,i)}function mapHeadingsDeep(t,e,i=1){return e.map((e=>{const r=(0,models_1.getTagOrGroup)("section",e,t);return r.depth=i,e.items&&(r.items=mapHeadingsDeep(r,e.items,i+1)),r}))}function getMarkdownContent(t){var e,i;return"string"==typeof(null===(e=null==t?void 0:t.attributes)||void 0===e?void 0:e.content)?null===(i=t.attributes)||void 0===i?void 0:i.content:(null==t?void 0:t.children)?getMarkdownContent(t.children[0]):void 0}
//# sourceMappingURL=markdown.js.map