/**
 * Rust installation check and auto-install helper
 * Checks if Rust/Cargo is installed and helps users install it if needed
 */
import { execSync } from 'child_process';
import * as os from 'os';
/**
 * Check if Rust/Cargo is installed
 */
export function isRustInstalled() {
    try {
        execSync('cargo --version', { stdio: 'ignore' });
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Get platform-specific Rust installation command
 */
export function getRustInstallCommand() {
    const platform = os.platform();
    if (platform === 'win32') {
        // Try winget first (Windows 10/11)
        try {
            execSync('winget --version', { stdio: 'ignore' });
            return 'winget install Rustlang.Rustup';
        }
        catch {
            // Fallback to chocolatey or direct download
            try {
                execSync('choco --version', { stdio: 'ignore' });
                return 'choco install rust';
            }
            catch {
                return 'Download and run rustup-init.exe from https://rustup.rs/';
            }
        }
    }
    else if (platform === 'darwin') {
        // macOS - try homebrew first
        try {
            execSync('brew --version', { stdio: 'ignore' });
            return 'brew install rust';
        }
        catch {
            return 'curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh';
        }
    }
    else {
        // Linux
        return 'curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh';
    }
}
/**
 * Try to install Rust automatically (if possible)
 */
export async function tryInstallRust() {
    const platform = os.platform();
    try {
        if (platform === 'win32') {
            // Try winget
            try {
                execSync('winget install Rustlang.Rustup --accept-package-agreements --accept-source-agreements', {
                    stdio: 'inherit'
                });
                return true;
            }
            catch {
                // Try chocolatey
                try {
                    execSync('choco install rust -y', { stdio: 'inherit' });
                    return true;
                }
                catch {
                    return false;
                }
            }
        }
        else if (platform === 'darwin') {
            // Try homebrew
            try {
                execSync('brew install rust', { stdio: 'inherit' });
                return true;
            }
            catch {
                // Fallback to rustup
                try {
                    execSync('curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y', {
                        stdio: 'inherit'
                    });
                    return true;
                }
                catch {
                    return false;
                }
            }
        }
        else {
            // Linux - use rustup
            try {
                execSync('curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y', {
                    stdio: 'inherit'
                });
                return true;
            }
            catch {
                return false;
            }
        }
    }
    catch {
        return false;
    }
}
/**
 * Check Rust and prompt/install if needed
 */
export async function checkAndInstallRust(autoInstall = false) {
    if (isRustInstalled()) {
        return true;
    }
    const platform = os.platform();
    const installCmd = getRustInstallCommand();
    if (platform === 'win32') {
        process.stderr.write('\n⚠️  Rust is not installed. Rust is required to build mk3-tui on Windows.\n\n');
    }
    else {
        process.stderr.write('\n⚠️  Rust is not installed. Rust is required to build mk3-tui.\n\n');
    }
    if (autoInstall) {
        process.stderr.write('🔄 Attempting to install Rust automatically...\n\n');
        const installed = await tryInstallRust();
        if (installed) {
            process.stderr.write('\n✅ Rust installed successfully!\n');
            process.stderr.write('   Please restart your terminal and run "4r" again.\n\n');
            return true;
        }
        else {
            process.stderr.write('\n❌ Automatic installation failed.\n');
            process.stderr.write('   Please install Rust manually:\n');
            process.stderr.write(`   ${installCmd}\n\n`);
            return false;
        }
    }
    else {
        process.stderr.write('To install Rust, run:\n');
        process.stderr.write(`  ${installCmd}\n\n`);
        if (platform === 'win32') {
            process.stderr.write('Or visit: https://rustup.rs/\n');
            process.stderr.write('After installing, restart your terminal and run "4r" again.\n\n');
        }
        else {
            process.stderr.write('After installing, restart your terminal and run "4r" again.\n');
            process.stderr.write('Or set AUTO_INSTALL_RUST=1 to attempt automatic installation.\n\n');
        }
        return false;
    }
}
//# sourceMappingURL=rust-check.js.map