"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var grammars_exports = {};
__export(grammars_exports, {
  GRAMMAR_FILE_MAP: () => GRAMMAR_FILE_MAP,
  clearParserCache: () => clearParserCache,
  getParser: () => getParser,
  getSupportedLanguages: () => getSupportedLanguages,
  hasWasmGrammar: () => hasWasmGrammar,
  initGrammars: () => initGrammars,
  isGrammarLoaded: () => isGrammarLoaded,
  loadAllGrammars: () => loadAllGrammars,
  loadGrammarsForLanguages: () => loadGrammarsForLanguages
});
module.exports = __toCommonJS(grammars_exports);
var path = __toESM(require("path"));
let parserLib = null;
let parserInitPromise = null;
const loadedGrammars = /* @__PURE__ */ new Map();
let grammarLoadChain = Promise.resolve();
const GRAMMAR_FILE_MAP = {
  typescript: "tree-sitter-typescript",
  tsx: "tree-sitter-tsx",
  javascript: "tree-sitter-javascript",
  jsx: "tree-sitter-javascript",
  python: "tree-sitter-python",
  go: "tree-sitter-go",
  rust: "tree-sitter-rust",
  java: "tree-sitter-java",
  c: "tree-sitter-c",
  cpp: "tree-sitter-cpp",
  csharp: "tree-sitter-c_sharp",
  php: "tree-sitter-php",
  ruby: "tree-sitter-ruby",
  swift: "tree-sitter-swift",
  kotlin: "tree-sitter-kotlin",
  dart: "tree-sitter-dart",
  svelte: "tree-sitter-svelte",
  elixir: "tree-sitter-elixir",
  scala: "tree-sitter-scala",
  lua: "tree-sitter-lua",
  zig: "tree-sitter-zig",
  bash: "tree-sitter-bash",
  ocaml: "tree-sitter-ocaml",
  elm: "tree-sitter-elm",
  solidity: "tree-sitter-solidity",
  vue: "tree-sitter-vue",
  objc: "tree-sitter-objc",
  yaml: "tree-sitter-yaml",
  // HCL (Terraform) is bundled in src/extraction/wasm/ (not in tree-sitter-wasms)
  hcl: "tree-sitter-hcl",
  // CSS is in tree-sitter-wasms; SCSS is bundled in src/extraction/wasm/
  css: "tree-sitter-css",
  scss: "tree-sitter-scss",
  html: "tree-sitter-html",
  // Pascal is bundled in src/extraction/wasm/ (not in tree-sitter-wasms)
  pascal: "tree-sitter-pascal",
  // No WASM available
  liquid: "",
  unknown: ""
};
async function initGrammars() {
  if (parserLib) return;
  if (parserInitPromise) return parserInitPromise;
  parserInitPromise = (async () => {
    const TreeSitter = require("web-tree-sitter");
    await TreeSitter.Parser.init();
    parserLib = TreeSitter;
  })();
  return parserInitPromise;
}
function resolveWasmPath(lang) {
  if (lang === "pascal") {
    return path.join(__dirname, "wasm", "tree-sitter-pascal.wasm");
  }
  if (lang === "hcl") {
    return path.join(__dirname, "wasm", "tree-sitter-hcl.wasm");
  }
  if (lang === "scss") {
    return path.join(__dirname, "wasm", "tree-sitter-scss.wasm");
  }
  const grammarFile = GRAMMAR_FILE_MAP[lang];
  if (!grammarFile) return null;
  try {
    return require.resolve(`tree-sitter-wasms/out/${grammarFile}.wasm`);
  } catch {
    return null;
  }
}
async function loadGrammarsForLanguages(languages) {
  for (const lang of languages) {
    if (loadedGrammars.has(lang)) continue;
    await new Promise((resolve) => {
      grammarLoadChain = grammarLoadChain.then(async () => {
        if (loadedGrammars.has(lang)) {
          resolve();
          return;
        }
        const wasmPath = resolveWasmPath(lang);
        if (!wasmPath) {
          resolve();
          return;
        }
        try {
          await initGrammars();
          const langObj = await parserLib.Language.load(wasmPath);
          loadedGrammars.set(lang, langObj);
        } catch {
        }
        resolve();
      });
    });
  }
}
async function getParser(language) {
  if (loadedGrammars.has(language)) {
    await initGrammars();
    const parser2 = new parserLib.Parser();
    parser2.setLanguage(loadedGrammars.get(language));
    return parser2;
  }
  if (!resolveWasmPath(language)) return null;
  await loadGrammarsForLanguages([language]);
  if (!loadedGrammars.has(language)) return null;
  await initGrammars();
  const parser = new parserLib.Parser();
  parser.setLanguage(loadedGrammars.get(language));
  return parser;
}
function isGrammarLoaded(language) {
  return loadedGrammars.has(language);
}
function hasWasmGrammar(language) {
  return resolveWasmPath(language) !== null;
}
function getSupportedLanguages() {
  return Object.keys(GRAMMAR_FILE_MAP).filter(
    (lang) => GRAMMAR_FILE_MAP[lang] !== ""
  );
}
function clearParserCache() {
  parserLib = null;
  parserInitPromise = null;
  loadedGrammars.clear();
  grammarLoadChain = Promise.resolve();
}
async function loadAllGrammars() {
  return loadGrammarsForLanguages(getSupportedLanguages());
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  GRAMMAR_FILE_MAP,
  clearParserCache,
  getParser,
  getSupportedLanguages,
  hasWasmGrammar,
  initGrammars,
  isGrammarLoaded,
  loadAllGrammars,
  loadGrammarsForLanguages
});
//# sourceMappingURL=grammars.js.map
