import remarkParse from 'remark-parse';
import { unified } from 'unified';
import { extractPlainText } from './chunker-utils.js';
/**
 * Session Log Chunker — the most critical chunker.
 *
 * Preserves parent heading breadcrumbs for every content block.
 * Output: `[Session 142 > Traps > Next.js Caching] We found that...`
 */
export class SessionLogChunker {
    strategy = 'session-log';
    chunk(content, filePath, type) {
        const tree = unified().use(remarkParse).parse(content);
        const chunks = [];
        // Heading breadcrumb stack: index = depth-1, value = heading text.
        // When we encounter a heading of depth N, we clear all headings deeper than N.
        const breadcrumbs = [];
        for (const node of tree.children) {
            if (node.type === 'heading') {
                const heading = node;
                const depth = heading.depth;
                const text = extractPlainText(heading.children);
                // Set this depth, truncate deeper levels
                breadcrumbs[depth - 1] = text;
                breadcrumbs.length = depth;
                continue;
            }
            // For any content node, emit a chunk with breadcrumb context
            const text = nodeToText(node, content);
            if (!text.trim())
                continue;
            const breadcrumbPath = breadcrumbs.filter(Boolean).join(' > ');
            const contextPrefix = breadcrumbPath ? `[${breadcrumbPath}]` : `[${filePath}]`;
            const label = breadcrumbPath || filePath;
            const startLine = node.position?.start.line ?? 1;
            const endLine = node.position?.end.line ?? startLine;
            chunks.push({
                content: text.trim(),
                contextPrefix,
                filePath,
                type,
                strategy: this.strategy,
                label,
                startLine,
                endLine,
                metadata: {},
            });
        }
        return chunks;
    }
}
/**
 * Convert an mdast content node back to its original text
 * using the source position to slice the original content.
 */
function nodeToText(node, source) {
    if (node.position) {
        const lines = source.split('\n');
        const startIdx = node.position.start.line - 1;
        const endIdx = node.position.end.line;
        return lines.slice(startIdx, endIdx).join('\n');
    }
    if ('value' in node && typeof node.value === 'string')
        return node.value;
    return '';
}
//# sourceMappingURL=session-log-chunker.js.map