import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import os from 'os';
export function resolvePlatformBinary(platform, arch) {
    const p = platform ?? process.platform;
    const a = arch ?? process.arch;
    if (p === 'win32')
        return 'mcp-server-win32-x64.exe';
    return `mcp-server-${p}-${a}`;
}
export function detectExtension(prefix, extensionsDir) {
    const dir = extensionsDir ?? path.join(os.homedir(), '.vscode', 'extensions');
    if (!fs.existsSync(dir))
        return null;
    const entries = fs.readdirSync(dir)
        .filter(e => e.startsWith(prefix))
        .sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }));
    if (entries.length === 0)
        return null;
    const latest = entries[entries.length - 1];
    return {
        version: latest.slice(prefix.length),
        extensionPath: path.join(dir, latest),
    };
}
function detectCli(command, versionFlag) {
    try {
        const output = execSync(`${command} ${versionFlag}`, { encoding: 'utf-8', timeout: 5000, stdio: ['pipe', 'pipe', 'pipe'] }).trim();
        const match = output.match(/(\d+\.\d+[\.\d]*)/);
        return { found: true, version: match?.[1] ?? 'unknown' };
    }
    catch {
        return { found: false };
    }
}
export function detectMcp(def, extensionsDir) {
    const base = { id: def.id, name: def.name };
    const { method } = def.detection;
    if (method === 'cli' && def.detection.cli) {
        const { command, version_flag } = def.detection.cli;
        const cli = detectCli(command, version_flag);
        if (cli.found) {
            return { ...base, status: 'detected', detail: `${command} v${cli.version} found`, version: cli.version };
        }
        return { ...base, status: 'unavailable', detail: `${command} not found in PATH` };
    }
    if (method === 'npm' && def.detection.npm) {
        return { ...base, status: 'available', detail: `Available via npm (${def.detection.npm.package})` };
    }
    if (method === 'extension' && def.detection.extension) {
        const ext = detectExtension(def.detection.extension.prefix, extensionsDir);
        if (ext) {
            return { ...base, status: 'detected', detail: `VSCode extension v${ext.version}`, version: ext.version, extensionPath: ext.extensionPath };
        }
        return { ...base, status: 'unavailable', detail: 'VSCode extension not found' };
    }
    if (method === 'hybrid') {
        if (def.detection.cli) {
            const { command, version_flag } = def.detection.cli;
            const cli = detectCli(command, version_flag);
            if (cli.found) {
                return { ...base, status: 'detected', detail: `${command} v${cli.version} found`, version: cli.version };
            }
        }
        if (def.detection.npm) {
            return { ...base, status: 'available', detail: `Available via npm (${def.detection.npm.package})` };
        }
        return { ...base, status: 'unavailable', detail: 'No CLI or npm package available' };
    }
    return { ...base, status: 'unavailable', detail: 'Unknown detection method' };
}
export function detectAllMcps(definitions, extensionsDir) {
    return definitions.map(def => detectMcp(def, extensionsDir));
}
