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