"use strict";
/**
 * Sync Command Handler
 *
 * Handle sync 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.handleSyncCommand = void 0;
const ui_1 = require("../utils/ui");
/**
 * Handle sync command
 */
async function handleSyncCommand() {
    await (0, ui_1.initUI)();
    console.log('');
    console.log((0, ui_1.header)('Syncing CCS Components...'));
    console.log('');
    // First, copy .claude/ directory from package to ~/.ccs/.claude/
    const { ClaudeDirInstaller } = await Promise.resolve().then(() => __importStar(require('../utils/claude-dir-installer')));
    const installer = new ClaudeDirInstaller();
    installer.install();
    console.log('');
    const cleanupResult = installer.cleanupDeprecated();
    if (cleanupResult.success && cleanupResult.cleanedFiles.length > 0) {
        console.log('');
    }
    // Then, create symlinks from ~/.ccs/.claude/ to ~/.claude/
    const { ClaudeSymlinkManager } = await Promise.resolve().then(() => __importStar(require('../utils/claude-symlink-manager')));
    const manager = new ClaudeSymlinkManager();
    manager.install(false);
    console.log('');
    // Repair shared symlinks (~/.ccs/shared/ → ~/.claude/)
    // This fixes symlinks broken by Claude CLI's atomic writes (e.g., toggle thinking)
    const SharedManager = (await Promise.resolve().then(() => __importStar(require('../management/shared-manager')))).default;
    const sharedManager = new SharedManager();
    sharedManager.ensureSharedDirectories();
    console.log((0, ui_1.ok)('Shared symlinks verified'));
    // Sync MCP servers from global ~/.claude.json to all non-bare instances
    const { InstanceManager } = await Promise.resolve().then(() => __importStar(require('../management/instance-manager')));
    const instanceMgr = new InstanceManager();
    const ProfileRegistry = (await Promise.resolve().then(() => __importStar(require('../auth/profile-registry')))).default;
    const { isProfileLocalSharedResourceMode } = await Promise.resolve().then(() => __importStar(require('../auth/shared-resource-policy')));
    const registry = new ProfileRegistry();
    const allProfiles = registry.getAllProfilesMerged();
    let mcpSynced = 0;
    for (const [name, profile] of Object.entries(allProfiles)) {
        if (isProfileLocalSharedResourceMode(profile)) {
            continue; // Skip profile-local shared-resource profiles
        }
        if (!instanceMgr.hasInstance(name)) {
            continue;
        }
        const instancePath = instanceMgr.getInstancePath(name);
        if (instanceMgr.syncMcpServers(instancePath)) {
            mcpSynced++;
        }
    }
    if (mcpSynced > 0) {
        console.log((0, ui_1.ok)(`MCP servers synced to ${mcpSynced} instance(s)`));
    }
    else {
        console.log((0, ui_1.info)('No instances to sync MCP servers'));
    }
    console.log('');
    console.log((0, ui_1.ok)('Sync complete!'));
    console.log('');
    process.exit(0);
}
exports.handleSyncCommand = handleSyncCommand;
//# sourceMappingURL=sync-command.js.map