/**
 * WSL2 detection utilities.
 *
 * Results are cached for the session lifetime to avoid repeated spawns.
 */
import { spawnSync } from 'child_process';
let _wsl2Available;
const _distroCache = new Map();
/**
 * Check if WSL2 is available on this system.
 * Cached after first call.
 */
export function isWsl2Available() {
    if (_wsl2Available !== undefined)
        return _wsl2Available;
    try {
        const result = spawnSync('wsl', ['--status'], {
            timeout: 5000,
            encoding: 'utf-8',
            windowsHide: true
        });
        // wsl --status exits 0 when WSL is installed. We need WSL2 specifically.
        // wsl --status may emit UTF-16LE with NUL bytes — strip them.
        const output = ((result.stdout ?? '') + (result.stderr ?? '')).replace(/\0/g, '');
        _wsl2Available =
            result.status === 0 &&
                (/Default Version\s*:\s*2/i.test(output) ||
                    /WSL\s*2/i.test(output) ||
                    /kernel version/i.test(output));
    }
    catch {
        _wsl2Available = false;
    }
    return _wsl2Available;
}
/**
 * Check if a specific WSL distro is installed.
 * Cached per distro name.
 */
export function isDistroInstalled(name) {
    if (_distroCache.has(name))
        return _distroCache.get(name);
    try {
        const result = spawnSync('wsl', ['-l', '-v'], {
            timeout: 5000,
            encoding: 'utf-8',
            windowsHide: true
        });
        const output = (result.stdout ?? '').replace(/\0/g, '');
        const tokens = output.split(/\s+/);
        const installed = tokens.some((t) => t === name);
        _distroCache.set(name, installed);
        return installed;
    }
    catch {
        _distroCache.set(name, false);
        return false;
    }
}
/** Clear the detection cache. Call after importing/unregistering a distro. */
export function clearDetectionCache() {
    _wsl2Available = undefined;
    _distroCache.clear();
}
//# sourceMappingURL=wsl-detect.js.map