"use strict";
/**
 * CLIProxy Sync Command Handler
 *
 * Handles `ccs cliproxy sync` command for syncing API profiles to local CLIProxy config.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleSync = exports.parseSyncArgs = void 0;
const sync_1 = require("../cliproxy/sync");
const ui_1 = require("../utils/ui");
/**
 * Parse sync command arguments.
 */
function parseSyncArgs(args) {
    return {
        dryRun: args.includes('--dry-run') || args.includes('-n'),
        verbose: args.includes('--verbose') || args.includes('-v'),
    };
}
exports.parseSyncArgs = parseSyncArgs;
/**
 * Handle `ccs cliproxy sync` command.
 */
async function handleSync(args) {
    await (0, ui_1.initUI)();
    const parsed = parseSyncArgs(args);
    console.log((0, ui_1.header)('CLIProxy Profile Sync'));
    console.log('');
    // Check status
    const status = (0, sync_1.getLocalSyncStatus)();
    if (!status.configExists) {
        console.log((0, ui_1.fail)('CLIProxy config not found'));
        console.log('');
        console.log('Run to generate config:');
        console.log(`  ${(0, ui_1.color)('ccs doctor --fix', 'command')}`);
        console.log('');
        process.exit(1);
    }
    // Get preview
    const preview = (0, sync_1.generateSyncPreview)();
    if (preview.length === 0) {
        console.log((0, ui_1.warn)('No API profiles configured to sync'));
        console.log('');
        console.log('Configure API profiles with:');
        console.log(`  ${(0, ui_1.color)('ccs api create', 'command')}`);
        console.log('');
        return;
    }
    // Show preview
    console.log((0, ui_1.subheader)(`Profiles to Sync (${preview.length})`));
    console.log('');
    const rows = preview.map((p) => {
        const model = p.modelName ? (0, ui_1.color)(p.modelName, 'info') : (0, ui_1.dim)('default');
        const url = p.baseUrl ? (0, ui_1.dim)(p.baseUrl) : (0, ui_1.dim)('-');
        return [p.name, url, model];
    });
    console.log((0, ui_1.table)(rows, { head: ['Profile', 'Base URL', 'Model'], colWidths: [15, 40, 20] }));
    console.log('');
    if (parsed.verbose) {
        console.log((0, ui_1.dim)(`Config path: ${status.configPath}`));
        console.log((0, ui_1.dim)(`Current keys: ${status.currentKeyCount}`));
        console.log('');
    }
    // Dry-run mode
    if (parsed.dryRun) {
        console.log((0, ui_1.info)('Dry-run mode - no changes will be made'));
        console.log('');
        console.log(`Would sync ${preview.length} profile(s) to:`);
        console.log(`  ${(0, ui_1.dim)(status.configPath)}`);
        console.log('');
        console.log('Run without --dry-run to apply changes:');
        console.log(`  ${(0, ui_1.color)('ccs cliproxy sync', 'command')}`);
        console.log('');
        return;
    }
    // Execute sync
    console.log((0, ui_1.info)('Syncing profiles to local config...'));
    const result = (0, sync_1.syncToLocalConfig)();
    if (!result.success) {
        console.log('');
        console.log((0, ui_1.fail)(`Sync failed: ${result.error}`));
        console.log('');
        process.exit(1);
    }
    console.log('');
    console.log((0, ui_1.ok)(`Synced ${result.syncedCount} profile(s)`));
    console.log(`  ${(0, ui_1.dim)(result.configPath)}`);
    console.log('');
    // Show synced profiles
    for (const p of preview) {
        console.log(`  ${(0, ui_1.ok)('')} ${p.name}`);
    }
    console.log('');
    console.log((0, ui_1.info)('Restart CLIProxy to apply changes:'));
    console.log(`  ${(0, ui_1.color)('ccs cliproxy stop && ccs gemini', 'command')}`);
    console.log('');
}
exports.handleSync = handleSync;
//# sourceMappingURL=cliproxy-sync-handler.js.map