"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatDiagnosticReport = formatDiagnosticReport;
exports.collectAndLogDiagnostics = collectAndLogDiagnostics;
const node_child_process_1 = require("node:child_process");
const logger_1 = require("./logger");
const platform_1 = require("./platform");
const TOOLS_TO_CHECK = [
    { name: 'git', versionArgs: ['--version'] },
    { name: 'gh', versionArgs: ['--version'] },
    { name: 'node', versionArgs: ['--version'] },
    { name: 'agent', versionArgs: ['--version'] }, // Cursor Agent CLI; required for cursor runner (install: irm 'https://cursor.com/install?win32=true' | iex on Windows)
    { name: 'claude', versionArgs: ['--version'] },
    { name: 'windsurf', versionArgs: ['--version'] },
    { name: 'docker', versionArgs: ['--version'] }, // Required for windsurf on Windows (headless via windsurfinabox)
];
function getToolVersion(name, versionArgs) {
    try {
        const result = (0, platform_1.spawnCommand)(name, versionArgs, {
            encoding: 'utf8',
            timeout: 10000,
        });
        if (result.status === 0 && result.stdout) {
            return { name, available: true, version: result.stdout.trim().split('\n')[0] };
        }
        if (result.error) {
            return { name, available: false, version: null };
        }
        return { name, available: true, version: result.stderr?.trim().split('\n')[0] || 'unknown' };
    }
    catch {
        return { name, available: false, version: null };
    }
}
function getGitBranch() {
    try {
        const result = (0, node_child_process_1.spawnSync)('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
            encoding: 'utf8',
            timeout: 5000,
        });
        return result.status === 0 ? result.stdout.trim() : null;
    }
    catch {
        return null;
    }
}
function getGitStatus() {
    try {
        const result = (0, node_child_process_1.spawnSync)('git', ['status', '--short'], {
            encoding: 'utf8',
            timeout: 5000,
        });
        return result.status === 0 ? result.stdout.trim() : '(unable to retrieve git status)';
    }
    catch {
        return '(unable to retrieve git status)';
    }
}
function collectReport() {
    const tools = TOOLS_TO_CHECK.map((t) => getToolVersion(t.name, t.versionArgs));
    return {
        tools,
        gitBranch: getGitBranch(),
        gitStatus: getGitStatus(),
    };
}
function formatDiagnosticReport(report) {
    const lines = ['--- Environment Diagnostic Report ---'];
    lines.push('');
    lines.push('Tools:');
    for (const tool of report.tools) {
        const status = tool.available
            ? `✓ ${tool.version}`
            : '✗ not found';
        lines.push(`  ${tool.name}: ${status}`);
    }
    lines.push('');
    lines.push(`Git branch: ${report.gitBranch ?? '(detached or unknown)'}`);
    lines.push(`Git status:\n${report.gitStatus || '(clean)'}`);
    lines.push('--- End Diagnostic Report ---');
    return lines.join('\n');
}
function collectAndLogDiagnostics() {
    const report = collectReport();
    const formatted = formatDiagnosticReport(report);
    for (const line of formatted.split('\n')) {
        logger_1.logger.info(line);
    }
    return formatted;
}
//# sourceMappingURL=diagnostics.js.map