import emojiRegex from 'emoji-regex';
import { marked } from 'marked';
/**
 * Converts markdown text to plain text while preserving emoji characters
 * @param markdown The markdown text to convert
 * @param preserveEmojis Whether to preserve emoji characters (default: false)
 * @returns Plain text with or without emojis preserved
 */
const markdownToPlainText = (markdown, preserveEmojis = false) => {
    if (!markdown)
        return '';
    // Convert Markdown to HTML
    const html = marked(markdown, { async: false });
    let processedHtml = html;
    let emojiPlaceholders = [];
    // If preserving emojis, replace them with placeholders
    if (preserveEmojis) {
        const emojiRx = emojiRegex();
        emojiPlaceholders = [];
        processedHtml = html.replace(emojiRx, match => {
            emojiPlaceholders.push(match);
            return `__EMOJI_${emojiPlaceholders.length - 1}__`;
        });
    }
    // Use DOMParser to convert HTML to plain text (common approach for both paths)
    const parser = new DOMParser();
    const doc = parser.parseFromString(processedHtml, 'text/html');
    let plainText = doc.body.textContent?.trim() ?? '';
    // If preserving emojis, restore them from placeholders
    if (preserveEmojis && emojiPlaceholders.length > 0) {
        plainText = plainText.replace(/__EMOJI_(\d+)__/g, (_, index) => emojiPlaceholders[parseInt(index, 10)] ?? '');
    }
    return plainText;
};
export default markdownToPlainText;
//# sourceMappingURL=markdownToPlainText.js.map