import { execSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { compareSemver } from './update-check.js';
const NPM_PACKAGE = '@defconxt/nerf';
export async function runUpdate() {
    const current = process.env.NERF_VERSION || '0.0.0';
    const bold = '\x1b[1m';
    const dim = '\x1b[2m';
    const green = '\x1b[32m';
    const yellow = '\x1b[33m';
    const reset = '\x1b[0m';
    process.stdout.write(`${dim}Current version:${reset} v${current}\n`);
    process.stdout.write(`${dim}Checking npm registry...${reset}\n`);
    // Fetch latest version
    let latest;
    try {
        latest = execSync(`npm view ${NPM_PACKAGE} version`, {
            encoding: 'utf-8',
            stdio: ['ignore', 'pipe', 'ignore'],
        }).trim();
    }
    catch {
        process.stderr.write(`${yellow}Failed to reach npm registry.${reset}\n`);
        process.exit(1);
    }
    if (compareSemver(latest, current) <= 0) {
        process.stdout.write(`${green}Already up to date.${reset}\n`);
        return;
    }
    process.stdout.write(`${dim}Updating:${reset} v${current} → ${bold}v${latest}${reset}\n`);
    try {
        // Run from a neutral directory so npm doesn't resolve @defconxt/nerf
        // as the local workspace project instead of fetching from the registry.
        execSync(`npm install -g ${NPM_PACKAGE}@latest`, {
            stdio: 'inherit',
            cwd: tmpdir(),
        });
        process.stdout.write(`\n${green}${bold}Updated to v${latest}${reset}\n`);
    }
    catch {
        process.stderr.write(`\n${yellow}Update failed. Try manually: npm install -g ${NPM_PACKAGE}@latest${reset}\n`);
        process.exit(1);
    }
}
