// engine/detectors/wasm-detector.ts — WebAssembly toolchain detection
// SPEC-004 tech-debt S4: Detect WASM targets and build tools
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
// === Helpers ===
async function readJsonFile(filePath) {
    const content = await readFile(filePath, 'utf-8');
    return JSON.parse(content);
}
// === Detection logic ===
/**
 * Check package.json for WASM-related dependencies and scripts.
 * Returns tools and targets detectable from Node/browser context.
 */
export function detectWasmFromPackageJson(pkg) {
    const tools = [];
    const targets = new Set();
    const allDeps = {
        ...pkg.dependencies,
        ...pkg.devDependencies,
    };
    if ('@assemblyscript/loader' in allDeps || 'assemblyscript' in allDeps) {
        tools.push('assemblyscript');
        targets.add('browser');
        targets.add('node');
    }
    if ('wasm-pack' in allDeps) {
        tools.push('wasm-pack');
        targets.add('browser');
    }
    // Check scripts for emcc / emscripten references
    for (const scriptCmd of Object.values(pkg.scripts ?? {})) {
        if (/emcc|emscripten/i.test(scriptCmd)) {
            /* v8 ignore start */
            if (!tools.includes('emscripten')) {
                /* v8 ignore stop */
                tools.push('emscripten');
            }
            targets.add('browser');
            targets.add('node');
            break;
        }
    }
    return { tools, targets: [...targets] };
}
/**
 * Check Cargo.toml for wasm-bindgen dependency.
 * Indicates Rust WASM compilation.
 */
export function detectWasmFromCargoToml(content) {
    const tools = [];
    const targets = new Set();
    if (content.includes('wasm-bindgen')) {
        tools.push('wasm-bindgen');
        targets.add('browser');
    }
    if (content.includes('wasm-pack')) {
        /* v8 ignore start */
        if (!tools.includes('wasm-pack')) {
            /* v8 ignore stop */
            tools.push('wasm-pack');
        }
        targets.add('browser');
    }
    // WASI target hint
    if (content.includes('wasi')) {
        targets.add('wasi');
    }
    return { tools, targets: [...targets] };
}
/**
 * Detect WebAssembly compilation targets and toolchain in a project.
 *
 * Checks:
 * - package.json: @assemblyscript/loader, assemblyscript, wasm-pack, emscripten in scripts
 * - Cargo.toml: wasm-bindgen, wasm-pack, wasi targets
 */
export async function detectWasm(projectPath) {
    const allTools = [];
    const allTargets = new Set();
    // Check package.json
    try {
        const pkg = (await readJsonFile(join(projectPath, 'package.json')));
        const { tools, targets } = detectWasmFromPackageJson(pkg);
        for (const t of tools) {
            allTools.push(t);
        }
        for (const t of targets) {
            allTargets.add(t);
        }
    }
    catch {
        // no package.json — skip
    }
    // Check Cargo.toml
    try {
        const content = await readFile(join(projectPath, 'Cargo.toml'), 'utf-8');
        const { tools, targets } = detectWasmFromCargoToml(content);
        for (const t of tools) {
            if (!allTools.includes(t)) {
                allTools.push(t);
            }
        }
        for (const t of targets) {
            allTargets.add(t);
        }
    }
    catch {
        // no Cargo.toml — skip
    }
    const isWasm = allTools.length > 0;
    return { isWasm, targets: [...allTargets], tools: allTools };
}
//# sourceMappingURL=wasm-detector.js.map