// import Parser from 'web-tree-sitter';

// Cache parsers by grammar path to avoid re-initialization.
import { Language, Parser } from 'web-tree-sitter';

const parsers = new Map<string, Parser>();

export async function initializeParser(grammarPath: string): Promise<Parser> {
  if (parsers.has(grammarPath)) {
    return parsers.get(grammarPath)!;
  }
  await Parser.init();
  const newParser = new Parser();
  const language = await Language.load(grammarPath);
  newParser.setLanguage(language);
  parsers.set(grammarPath, newParser);
  return newParser;
}
