"use strict";
/**
 * Version Command Handler
 *
 * Handle --version command for CCS.
 */
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleVersionCommand = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const ui_1 = require("../utils/ui");
const config_manager_1 = require("../utils/config-manager");
const version_1 = require("../utils/version");
const profile_compat_1 = require("../utils/profile-compat");
const config_loader_facade_1 = require("../config/config-loader-facade");
/**
 * Handle version command
 */
async function handleVersionCommand() {
    await (0, ui_1.initUI)();
    console.log((0, ui_1.header)(`CCS (Claude Code Switch) v${(0, version_1.getVersion)()}`));
    console.log('');
    console.log((0, ui_1.subheader)('Installation:'));
    const installLocation = process.argv[1] || '(not found)';
    console.log(`  ${(0, ui_1.color)('Location:'.padEnd(17), 'info')} ${installLocation}`);
    const ccsDir = (0, config_loader_facade_1.getCcsDir)();
    console.log(`  ${(0, ui_1.color)('CCS Directory:'.padEnd(17), 'info')} ${ccsDir}`);
    const configPath = (0, config_manager_1.getActiveConfigPath)();
    console.log(`  ${(0, ui_1.color)('Config:'.padEnd(17), 'info')} ${configPath}`);
    const profilesJson = path.join(ccsDir, 'profiles.json');
    console.log(`  ${(0, ui_1.color)('Profiles:'.padEnd(17), 'info')} ${profilesJson}`);
    // Delegation status
    const delegationSessionsPath = path.join(ccsDir, 'delegation-sessions.json');
    const delegationConfigured = fs.existsSync(delegationSessionsPath);
    const readyProfiles = [];
    // Check for profiles with valid API keys
    for (const profile of ['glm', 'km']) {
        const settingsPath = (0, profile_compat_1.getProfileLookupCandidates)(profile)
            .map((candidate) => path.join(ccsDir, `${candidate}.settings.json`))
            .find((candidatePath) => fs.existsSync(candidatePath));
        if (!settingsPath) {
            continue;
        }
        try {
            const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
            const apiKey = settings.env?.ANTHROPIC_AUTH_TOKEN;
            if (apiKey && !apiKey.match(/YOUR_.*_API_KEY_HERE/) && !apiKey.match(/sk-test.*/)) {
                readyProfiles.push(profile);
            }
        }
        catch (_error) {
            // Invalid JSON, skip
        }
    }
    const hasValidApiKeys = readyProfiles.length > 0;
    const delegationEnabled = delegationConfigured || hasValidApiKeys;
    if (delegationEnabled) {
        console.log(`  ${(0, ui_1.color)('Delegation:'.padEnd(17), 'info')} Enabled`);
    }
    else {
        console.log(`  ${(0, ui_1.color)('Delegation:'.padEnd(17), 'info')} Not configured`);
    }
    console.log('');
    if (readyProfiles.length > 0) {
        console.log((0, ui_1.subheader)('Delegation Ready:'));
        console.log(`  ${(0, ui_1.color)('[OK]', 'warning')} ${readyProfiles.join(', ')} profiles are ready for delegation`);
        console.log('');
    }
    else if (delegationEnabled) {
        console.log((0, ui_1.subheader)('Delegation Ready:'));
        console.log((0, ui_1.warn)('Delegation configured but no valid API keys found'));
        console.log('');
    }
    console.log(`${(0, ui_1.subheader)('Documentation:')} ${(0, ui_1.color)('https://docs.ccs.kaitran.ca', 'path')}`);
    console.log(`${(0, ui_1.subheader)('License:')} MIT`);
    console.log('');
    console.log((0, ui_1.color)("Run 'ccs --help' for usage information", 'command'));
    process.exit(0);
}
exports.handleVersionCommand = handleVersionCommand;
//# sourceMappingURL=version-command.js.map