/**
 * Strips markdown formatting and links from text for previews.
 */
export const stripMarkdown = (text: string | undefined): string => {
    if (!text) return '';

    return text
        // 1. Remove WikiLinks/Links while keeping text: [[id|text]] or [text](url)
        .replace(/\[{2}(?:[^|\]]+\|)?([^\]]+)\]{2}/g, '$1')
        .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
        // 2. Remove HTML tags (e.g., <u></u>)
        .replace(/<[^>]*>?/gm, '')
        // 3. Remove backslashes (escaping the Won symbol in Windows)
        .replace(/\\/g, '')
        // 4. Remove formatting: bold, italic, strikethrough, code
        .replace(/(\*\*|__|\*|_|~~|`)/g, '')
        // 5. Remove headers
        .replace(/^#+\s+/gm, '')
        // 6. Remove list markers (bullet points and numbers at the start of lines)
        .replace(/^\s*[\-*•\d.]+\s+/gm, '')
        // 7. Remove blockquotes
        .replace(/^\s*>\s+/gm, '')
        // 8. Final cleanup: replace multiple spaces/newlines with a single space
        .replace(/\s+/g, ' ')
        .trim();
};

/**
 * 헤더/리스트 ID 생성에 사용하는 마크다운 서식 제거 함수
 * (TOC 컴포넌트와 DocumentClient의 MarkdownComponents에서 공유)
 */
export function cleanTextForId(raw: string): string {
    return raw
        .replace(/\\/g, '')
        .replace(/\[\[(.+?)\]\]/g, '$1')
        .replace(/\[(.+?)\]\(.+?\)/g, '$1')
        .replace(/\*\*(.+?)\*\*/g, '$1')
        .replace(/\*(.+?)\*/g, '$1')
        .replace(/__(.+?)__/g, '$1')
        .replace(/_(.+?)_/g, '$1')
        .replace(/~~(.+?)~~/g, '$1')
        .trim();
}
