/**
 * Document parser entry point for TRUEREF-0005.
 *
 * Exposes `parseFile` which transforms a `CrawledFile` into an array of
 * `NewSnippet` records ready for database insertion.
 */

import type { CrawledFile } from '$lib/server/crawler/types.js';
import type { NewSnippet } from '$lib/server/db/schema.js';
import { detectLanguage } from './language.js';
import { parseMarkdown } from './markdown.parser.js';
import { parseCodeFile } from './code.parser.js';

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

export interface ParseOptions {
	repositoryId: string;
	documentId: string;
	versionId?: string;
}

/**
 * Parse a crawled file into an array of `NewSnippet` records.
 *
 * The language is detected from the file extension.  Markdown/MDX files are
 * split by heading hierarchy; all other files use language-specific boundary
 * detection or a sliding-window fallback.
 */
export function parseFile(file: CrawledFile, options: ParseOptions): NewSnippet[] {
	const language = detectLanguage(file.path);

	const rawSnippets =
		language === 'markdown'
			? parseMarkdown(file.content, file.path)
			: parseCodeFile(file.content, file.path, language);

	return rawSnippets.map((s) => ({
		...s,
		id: crypto.randomUUID(),
		repositoryId: options.repositoryId,
		documentId: options.documentId,
		versionId: options.versionId ?? null,
		createdAt: new Date()
	}));
}

// Re-export helpers for consumers that need them individually
export { detectLanguage } from './language.js';
export { estimateTokens, chunkText, chunkLines, MAX_TOKENS, OVERLAP_TOKENS, MIN_CONTENT_LENGTH } from './chunker.js';
export { parseMarkdown } from './markdown.parser.js';
export { parseCodeFile, BOUNDARY_PATTERNS } from './code.parser.js';
