import { marked } from "marked";
const toInlinePlainText = (textWithHtml)=>{
    const withoutHtmlTags = textWithHtml.replace(/<[^>]*>/g, ' ');
    const withCollapsedWhitespaces = withoutHtmlTags.replace(/\s{2,}/g, ' ');
    return withCollapsedWhitespaces.trim();
};
const unclosedElement = /(\*{1,3}|`)($|\w[\w\s]*$)/;
const completeUnclosedElement = (text)=>{
    const match = unclosedElement.exec(text);
    if (match) {
        const symbol = match[1];
        const replacements = {
            '***': '<strong part="answer-strong"><em part="answer-emphasis">$2</em></strong>',
            '**': '<strong part="answer-strong">$2</strong>',
            '*': '<em part="answer-emphasis">$2</em>',
            '`': '<code part="answer-inline-code">$2</code>'
        };
        return text.replace(unclosedElement, replacements[symbol]);
    }
    return text;
};
const escapeHtml = (text)=>text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
const customRenderer = {
    blockquote (quote) {
        return `<blockquote part="answer-quote-block">${quote}</blockquote>`;
    },
    code (code) {
        return `<pre part="answer-code-block"><code>${escapeHtml(code)}</code></pre>`;
    },
    codespan (text) {
        return `<code part="answer-inline-code">${text}</code>`;
    },
    em (text) {
        return `<em part="answer-emphasis">${text}</em>`;
    },
    heading (text, level) {
        const plainText = toInlinePlainText(text);
        return `<div part="answer-heading-${level}" aria-label="${plainText}">${text}</div>`;
    },
    html (text) {
        return escapeHtml(text);
    },
    list (body, ordered, start) {
        const type = ordered ? 'ol' : 'ul';
        const part = ordered ? 'answer-ordered-list' : 'answer-unordered-list';
        const tag = ordered && 1 !== start ? `<${type} part="${part}" start="${start}">` : `<${type} part="${part}">`;
        return `${tag}${body}</${type}>`;
    },
    link (href, title, text) {
        const titleAttribute = title ? ` title="${escapeHtml(title)}"` : '';
        const safeHref = href ? escapeHtml(href) : '';
        if (!safeHref) return `<span>${text}</span>`;
        return `<atomic-generated-answer-inline-link href="${safeHref}"${titleAttribute} exportparts="answer-link,answer-link-text,answer-link-icon">${text}</atomic-generated-answer-inline-link>`;
    },
    listitem (text) {
        const unwrappedText = text.replace(/^<p[^>]*>/, '').replace(/<\/p>\n?$/, '');
        const withClosedElement = completeUnclosedElement(unwrappedText);
        return `<li part="answer-list-item">${withClosedElement}</li>`;
    },
    paragraph (text) {
        return `<p part="answer-paragraph">${text}</p>`;
    },
    strong (text) {
        return `<strong part="answer-strong">${text}</strong>`;
    },
    table (header, body) {
        return `<div part="answer-table-container" class="scrollable-table"><table part="answer-table"><thead>${header}</thead><tbody>${body}</tbody></table></div>`;
    },
    tablecell (content, flags) {
        const type = flags.header ? 'th' : 'td';
        const part = flags.header ? 'answer-table-header' : 'answer-table-content';
        const tag = flags.align ? `<${type} part="${part}" align="${flags.align}">` : `<${type} part="${part}">`;
        return `${tag}${content}</${type}>`;
    },
    text (text) {
        return completeUnclosedElement(text);
    }
};
const transformMarkdownToHtml = (text)=>marked.use({
        renderer: customRenderer
    }).parse(text);
export { transformMarkdownToHtml };
