"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const node_child_process_1 = require("node:child_process");
class Update extends core_1.Command {
    async run() {
        const packageName = this.config.name;
        const currentVersion = this.config.version;
        this.log(`Current version: ${currentVersion}`);
        process.stdout.write('Checking latest version on npm...');
        let latest;
        try {
            latest = (0, node_child_process_1.execFileSync)('npm', ['view', packageName, 'version'], {
                encoding: 'utf8',
            }).trim();
        }
        catch {
            this.error('Could not reach npm registry. Check your internet connection.');
        }
        process.stdout.write(`\r\x1B[K`);
        if (latest === currentVersion) {
            this.log(`Already on the latest version (${currentVersion}).`);
            return;
        }
        this.log(`New version available: ${currentVersion} → ${latest}`);
        const readline = await Promise.resolve().then(() => __importStar(require('node:readline')));
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout,
        });
        const confirm = await new Promise((resolve) => {
            rl.question(`Update to ${latest}? [Y/n] `, (answer) => {
                rl.close();
                resolve(answer.trim().toLowerCase() !== 'n');
            });
        });
        if (!confirm) {
            this.log('Update cancelled.');
            return;
        }
        this.log(`Installing ${packageName}@${latest}...`);
        const result = (0, node_child_process_1.spawnSync)('npm', ['install', '-g', `${packageName}@${latest}`], {
            stdio: 'inherit',
            shell: true,
        });
        if (result.status !== 0) {
            this.error('Update failed. Try running manually: npm install -g ' + packageName);
        }
        this.log(`Successfully updated to ${latest}.`);
    }
}
Update.description = 'Update the flui CLI to the latest version';
Update.examples = ['<%= config.bin %> <%= command.id %>'];
exports.default = Update;
