/**
 * Language detection for the document parser (TRUEREF-0005).
 *
 * Maps file extensions to canonical language names used throughout the parser.
 */

import { extname } from 'node:path';

// ---------------------------------------------------------------------------
// Language map
// ---------------------------------------------------------------------------

export const LANGUAGE_MAP: Record<string, string> = {
	'.ts': 'typescript',
	'.tsx': 'typescript',
	'.js': 'javascript',
	'.jsx': 'javascript',
	'.py': 'python',
	'.rb': 'ruby',
	'.go': 'go',
	'.rs': 'rust',
	'.java': 'java',
	'.cs': 'csharp',
	'.cpp': 'cpp',
	'.c': 'c',
	'.h': 'c',
	'.swift': 'swift',
	'.kt': 'kotlin',
	'.php': 'php',
	'.scala': 'scala',
	'.sh': 'bash',
	'.bash': 'bash',
	'.zsh': 'bash',
	'.md': 'markdown',
	'.mdx': 'markdown',
	'.json': 'json',
	'.yaml': 'yaml',
	'.yml': 'yaml',
	'.toml': 'toml',
	'.html': 'html',
	'.css': 'css',
	'.svelte': 'svelte',
	'.vue': 'vue',
	'.sql': 'sql',
	'.txt': 'text',
	'.rst': 'text'
};

/**
 * Detect the canonical language name from a file path.
 * Returns 'text' when the extension is unknown.
 */
export function detectLanguage(filePath: string): string {
	const ext = extname(filePath).toLowerCase();
	return LANGUAGE_MAP[ext] ?? 'text';
}
