/**
 * WASM compatibility shim for @ast-grep/napi.
 *
 * This module is aliased in place of @ast-grep/napi during the esbuild lite build.
 * It re-exports the same API surface that ast-grep-query.ts uses, backed by
 * @ast-grep/wasm instead of native NAPI bindings.
 *
 * NOT imported in the normal build — only used via esbuild alias in the lite binary.
 */
import { createRequire } from 'node:module';
// ─── Lang enum (string-valued to match WASM API) ───
/**
 * Mimics the @ast-grep/napi Lang enum.
 * The WASM API uses plain strings; the napi API uses a numeric enum.
 * This object provides the same property names with string values.
 *
 * Note: In the lite build, only TypeScript, Tsx, and JavaScript are
 * registered. Html and Css are included for type compatibility but
 * will throw at runtime (no parsers registered).
 */
export const Lang = {
    TypeScript: 'typescript',
    Tsx: 'tsx',
    JavaScript: 'javascript',
    Html: 'html',
    Css: 'css',
};
// ─── Lazy initialization ────────────────────────────
let initPromise = null;
let initialized = false;
// We store the WASM module reference after dynamic import
let wasmModule = null;
/**
 * Initialize the WASM runtime and register JS/TS language parsers.
 * Must be called (and awaited) before the first `parse()` call.
 * Safe to call multiple times — initialization happens only once.
 */
export async function ensureInit() {
    if (initialized)
        return;
    if (initPromise)
        return initPromise;
    initPromise = doInit();
    await initPromise;
}
async function doInit() {
    // Dynamic import so this module can be bundled by esbuild
    // even when @ast-grep/wasm is resolved at build time
    const mod = await import('@ast-grep/wasm');
    wasmModule = mod;
    // Initialize the tree-sitter WASM runtime
    await mod.initializeTreeSitter();
    // Locate tree-sitter language WASM files via require.resolve
    // In the bundled binary, these paths resolve to embedded assets
    const req = createRequire(import.meta.url);
    let tsWasm;
    let tsxWasm;
    let jsWasm;
    try {
        tsWasm = req.resolve('tree-sitter-typescript/tree-sitter-typescript.wasm');
        tsxWasm = req.resolve('tree-sitter-typescript/tree-sitter-tsx.wasm');
        jsWasm = req.resolve('tree-sitter-javascript/tree-sitter-javascript.wasm');
    }
    catch (err) {
        // In the compiled Bun binary, require.resolve may not work.
        // Fall back to looking relative to the binary location.
        void err;
        const { join, dirname } = await import('node:path');
        const wasmDir = join(dirname(process.execPath), 'wasm');
        tsWasm = join(wasmDir, 'tree-sitter-typescript.wasm');
        tsxWasm = join(wasmDir, 'tree-sitter-tsx.wasm');
        jsWasm = join(wasmDir, 'tree-sitter-javascript.wasm');
    }
    // Register the languages we support
    await mod.registerDynamicLanguage({
        typescript: { libraryPath: tsWasm },
        tsx: { libraryPath: tsxWasm },
        javascript: { libraryPath: jsWasm },
    });
    initialized = true;
}
// ─── Public API (matching @ast-grep/napi) ───────────
/**
 * Parse source code into an AST.
 * Matches the @ast-grep/napi signature: parse(lang: Lang, src: string) => SgRoot
 *
 * The lang parameter accepts a Lang enum value (string).
 * Unlike @ast-grep/napi which uses numeric enums, the WASM API uses strings.
 */
export function parse(lang, src) {
    if (!initialized || !wasmModule) {
        throw new Error('[Totem Error] AST engine not initialized. Call ensureInit() before parse().');
    }
    // Normalize lang — in case someone passes the string directly
    const langStr = typeof lang === 'string' ? lang : String(lang);
    return wasmModule.parse(langStr, src);
}
//# sourceMappingURL=ast-grep-wasm-shim.js.map