import*as Root from"../root/root.js";import*as Marked from"../third_party/marked/marked.js";import{MarkdownView}from"./MarkdownView.js";export function createIssueDescriptionFromMarkdown(e){return createIssueDescriptionFromRawMarkdown(substitutePlaceholders(getMarkdownFileContent(e.file),e.substitutions),e)}function getMarkdownFileContent(e){const t=Root.Runtime.cachedResources.get(e);if(!t)throw new Error(`Markdown file ${e} not found. Declare it as a resource in the module.json file`);return t}const validPlaceholderMatchPattern=/\{(PLACEHOLDER_[a-zA-Z][a-zA-Z0-9]*)\}/g,validPlaceholderNamePattern=/PLACEHOLDER_[a-zA-Z][a-zA-Z0-9]*/;export function substitutePlaceholders(e,t){const r=new Set(t?t.keys():[]);validatePlaceholders(r);const o=e.replace(validPlaceholderMatchPattern,((e,o)=>{const n=t?t.get(o):void 0;if(!n)throw new Error(`No replacment provided for placeholder '${o}'.`);return r.delete(o),n}));if(r.size>0)throw new Error(`Unused replacements provided: ${[...r]}`);return o}function validatePlaceholders(e){const t=[...e].filter((e=>!validPlaceholderNamePattern.test(e)));if(t.length>0)throw new Error(`Invalid placeholders provided in the substitutions map: ${t}`)}export function createIssueDescriptionFromRawMarkdown(e,t){const r=Marked.Marked.lexer(e),o=findTitleFromMarkdownAst(r);if(!o)throw new Error("Markdown issue descriptions must start with a heading");const n=new MarkdownView;return n.data={tokens:r.slice(1)},{title:o,view:n,links:t.links}}function findTitleFromMarkdownAst(e){return 0===e.length||"heading"!==e[0].type||1!==e[0].depth?null:e[0].text}