import Parser from 'tree-sitter';
import JavaScript from 'tree-sitter-javascript';
import TypeScript from 'tree-sitter-typescript';
import Python from 'tree-sitter-python';
import Java from 'tree-sitter-java';
import C from 'tree-sitter-c';
import CPP from 'tree-sitter-cpp';
// Explicit subpath import: tree-sitter-c-sharp declares `type: "module"` with
// `main: "bindings/node"` (no extension) and no `exports` field, which triggers
// Node 22's DEP0151 deprecation warning on the bare-package import. Importing
// the built entrypoint directly bypasses the deprecated ESM main-field
// resolution. (#1013)
import CSharp from 'tree-sitter-c-sharp/bindings/node/index.js';
import Go from 'tree-sitter-go';
import Rust from 'tree-sitter-rust';
import PHP from 'tree-sitter-php';
import Ruby from 'tree-sitter-ruby';
import { createRequire } from 'node:module';
import { SupportedLanguages } from '../../_shared/index.js';
// tree-sitter-swift and tree-sitter-dart are optionalDependencies — may not be installed
const _require = createRequire(import.meta.url);
let Swift = null;
try {
    Swift = _require('tree-sitter-swift');
}
catch { }
let Dart = null;
try {
    Dart = _require('tree-sitter-dart');
}
catch { }
// tree-sitter-kotlin is an optionalDependency — may not be installed
let Kotlin = null;
try {
    Kotlin = _require('tree-sitter-kotlin');
}
catch { }
let parser = null;
const languageMap = {
    [SupportedLanguages.JavaScript]: JavaScript,
    [SupportedLanguages.TypeScript]: TypeScript.typescript,
    [`${SupportedLanguages.TypeScript}:tsx`]: TypeScript.tsx,
    [SupportedLanguages.Python]: Python,
    [SupportedLanguages.Java]: Java,
    [SupportedLanguages.C]: C,
    [SupportedLanguages.CPlusPlus]: CPP,
    [SupportedLanguages.CSharp]: CSharp,
    [SupportedLanguages.Go]: Go,
    [SupportedLanguages.Rust]: Rust,
    ...(Kotlin ? { [SupportedLanguages.Kotlin]: Kotlin } : {}),
    [SupportedLanguages.PHP]: PHP.php_only,
    [SupportedLanguages.Ruby]: Ruby,
    [SupportedLanguages.Vue]: TypeScript.typescript,
    ...(Dart ? { [SupportedLanguages.Dart]: Dart } : {}),
    ...(Swift ? { [SupportedLanguages.Swift]: Swift } : {}),
};
export const isLanguageAvailable = (language) => language in languageMap;
export const resolveLanguageKey = (language, filePath) => language === SupportedLanguages.TypeScript && filePath?.endsWith('.tsx')
    ? `${language}:tsx`
    : language;
export const getLanguageGrammar = (language, filePath) => {
    const key = resolveLanguageKey(language, filePath);
    const lang = languageMap[key];
    if (!lang) {
        throw new Error(`Unsupported language: ${language}`);
    }
    return lang;
};
export const loadParser = async () => {
    if (parser)
        return parser;
    parser = new Parser();
    return parser;
};
export const loadLanguage = async (language, filePath) => {
    if (!parser)
        await loadParser();
    parser.setLanguage(getLanguageGrammar(language, filePath));
};
export const createParserForLanguage = async (language, filePath) => {
    const freshParser = new Parser();
    freshParser.setLanguage(getLanguageGrammar(language, filePath));
    return freshParser;
};
