"use strict";
/**
 * CLIProxy Binary Installation
 *
 * Handles:
 * - ccs cliproxy (show status)
 * - ccs cliproxy --install <version>
 * - ccs cliproxy --latest
 * - ccs cliproxy --update
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleInstallLatest = exports.handleInstallVersion = exports.showStatus = void 0;
const ui_1 = require("../../utils/ui");
const services_1 = require("../../cliproxy/services");
const platform_detector_1 = require("../../cliproxy/binary/platform-detector");
function getBackendLabel(backend) {
    return backend === 'plus' ? 'CLIProxy Plus' : 'CLIProxy';
}
async function showStatus(verbose, backend) {
    await (0, ui_1.initUI)();
    const status = (0, services_1.getBinaryStatus)(backend);
    console.log('');
    const backendLabel = getBackendLabel(backend);
    console.log((0, ui_1.color)(`${backendLabel} Status`, 'primary'));
    console.log('');
    console.log(`  Backend:    ${(0, ui_1.color)(backend, 'info')}${backend === platform_detector_1.DEFAULT_BACKEND ? (0, ui_1.dim)(' (default)') : ''}`);
    if (status.installed) {
        console.log(`  Installed:  ${(0, ui_1.color)('Yes', 'success')}`);
        const versionLabel = status.pinnedVersion
            ? `${(0, ui_1.color)(`v${status.currentVersion}`, 'info')} ${(0, ui_1.color)('(pinned)', 'warning')}`
            : (0, ui_1.color)(`v${status.currentVersion}`, 'info');
        console.log(`  Version:    ${versionLabel}`);
        console.log(`  Binary:     ${(0, ui_1.dim)(status.binaryPath)}`);
    }
    else {
        console.log(`  Installed:  ${(0, ui_1.color)('No', 'error')}`);
        console.log(`  Fallback:   ${(0, ui_1.color)(`v${status.fallbackVersion}`, 'info')}`);
        console.log(`  ${(0, ui_1.dim)('Run "ccs gemini" or any provider to auto-install')}`);
    }
    const latestCheck = await (0, services_1.checkLatestVersion)(backend);
    if (latestCheck.success && latestCheck.latestVersion) {
        console.log('');
        if (latestCheck.updateAvailable) {
            if (status.pinnedVersion) {
                console.log(`  Latest:     ${(0, ui_1.color)(`v${latestCheck.latestVersion}`, 'success')} ${(0, ui_1.dim)('(pinned to v' + status.pinnedVersion + ')')}`);
                console.log('');
                console.log(`  ${(0, ui_1.dim)('Run "ccs cliproxy --update" to unpin and update')}`);
            }
            else {
                console.log(`  Latest:     ${(0, ui_1.color)(`v${latestCheck.latestVersion}`, 'success')} ${(0, ui_1.dim)('(update available)')}`);
                console.log('');
                console.log(`  ${(0, ui_1.dim)('Run "ccs cliproxy --latest" to update')}`);
            }
        }
        else {
            console.log(`  Latest:     ${(0, ui_1.color)(`v${latestCheck.latestVersion}`, 'success')} ${(0, ui_1.dim)('(up to date)')}`);
        }
    }
    else if (verbose && latestCheck.error) {
        console.log(`  Latest:     ${(0, ui_1.dim)(`Could not fetch (${latestCheck.error})`)}`);
    }
    console.log('');
    console.log((0, ui_1.dim)('Run "ccs cliproxy --help" for all available commands'));
    console.log('');
}
exports.showStatus = showStatus;
async function handleInstallVersion(version, verbose, backend) {
    const label = getBackendLabel(backend);
    console.log((0, ui_1.info)(`Installing ${label} v${version}...`));
    console.log('');
    const result = await (0, services_1.installVersion)(version, verbose, backend);
    if (!result.success) {
        console.error('');
        console.error((0, ui_1.fail)(`Failed to install ${label} v${version}`));
        console.error(`    ${result.error}`);
        console.error('');
        console.error('Possible causes:');
        console.error('  1. Version does not exist on GitHub');
        console.error('  2. Network connectivity issues');
        console.error('  3. GitHub API rate limiting');
        console.error('');
        console.error('Check available versions at:');
        console.error(`  https://github.com/${platform_detector_1.BACKEND_CONFIG[backend].repo}/releases`);
        process.exit(1);
    }
    console.log('');
    console.log((0, ui_1.ok)(`${label} v${version} installed (pinned)`));
    console.log('');
    console.log((0, ui_1.dim)('This version will be used until you run:'));
    console.log(`  ${(0, ui_1.color)('ccs cliproxy --update', 'command')}  ${(0, ui_1.dim)('# Update to latest and unpin')}`);
    console.log('');
}
exports.handleInstallVersion = handleInstallVersion;
async function handleInstallLatest(verbose, backend) {
    const label = getBackendLabel(backend);
    console.log((0, ui_1.info)(`Fetching latest ${label} version...`));
    const result = await (0, services_1.installLatest)(verbose, backend);
    if (!result.success) {
        console.error((0, ui_1.fail)(`Failed to install latest version: ${result.error}`));
        process.exit(1);
    }
    if (result.error?.startsWith('Already')) {
        console.log((0, ui_1.ok)(result.error));
        return;
    }
    console.log('');
    console.log((0, ui_1.ok)(`${label} updated to v${result.version}`));
    console.log((0, ui_1.dim)('Auto-update is now enabled.'));
    console.log('');
}
exports.handleInstallLatest = handleInstallLatest;
//# sourceMappingURL=install-subcommand.js.map