"use strict";
/**
 * Auth Commands (Facade)
 *
 * CLI interface for CCS multi-account management.
 * Commands: create, list, show, remove, default, reset-default
 *
 * Login-per-profile model: Each profile is an isolated Claude instance.
 * Users login directly in each instance (no credential copying).
 *
 * Implementation Note: This is a facade that delegates to modular command handlers.
 * See ./commands/ for individual command implementations.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const profile_registry_1 = __importDefault(require("./profile-registry"));
const instance_manager_1 = require("../management/instance-manager");
const ui_1 = require("../utils/ui");
const account_context_1 = require("./account-context");
const package_json_1 = __importDefault(require("../../package.json"));
// Import command handlers from modular structure
const commands_1 = require("./commands");
/**
 * Auth Commands Class (Facade)
 *
 * Maintains class API for backward compatibility while delegating
 * to modular command handlers.
 */
class AuthCommands {
    constructor() {
        this.version = package_json_1.default.version;
        this.registry = new profile_registry_1.default();
        this.instanceMgr = new instance_manager_1.InstanceManager();
    }
    /**
     * Get command context for handlers
     */
    getContext() {
        return {
            registry: this.registry,
            instanceMgr: this.instanceMgr,
            version: this.version,
        };
    }
    /**
     * Show help for auth commands
     */
    async showHelp() {
        await (0, ui_1.initUI)();
        console.log((0, ui_1.header)('CCS Concurrent Account Management'));
        console.log('');
        console.log((0, ui_1.subheader)('Usage'));
        console.log(`  ${(0, ui_1.color)('ccs auth', 'command')} <command> [options]`);
        console.log('');
        console.log((0, ui_1.subheader)('Commands'));
        console.log(`  ${(0, ui_1.color)('create <profile>', 'command')}        Create new profile and login`);
        console.log(`  ${(0, ui_1.color)('backup <profile>', 'command')}        Backup local continuity for an account`);
        console.log(`  ${(0, ui_1.color)('list', 'command')}                   List all saved profiles`);
        console.log(`  ${(0, ui_1.color)('show <profile>', 'command')}         Show profile details`);
        console.log(`  ${(0, ui_1.color)('resources <profile>', 'command')}    Show or change shared resource mode`);
        console.log(`  ${(0, ui_1.color)('remove <profile>', 'command')}       Remove saved profile`);
        console.log(`  ${(0, ui_1.color)('default <profile>', 'command')}      Set default profile`);
        console.log(`  ${(0, ui_1.color)('reset-default', 'command')}          Clear default (restore original CCS)`);
        console.log('');
        console.log((0, ui_1.subheader)('Examples'));
        console.log(`  ${(0, ui_1.dim)('# Create two isolated accounts and choose one explicitly at runtime')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth create work', 'command')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth create personal', 'command')}`);
        console.log(`  ${(0, ui_1.color)('ccs work "review code"', 'command')}`);
        console.log(`  ${(0, ui_1.color)('ccs personal "write tests"', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Optional: share local project history while credentials stay isolated')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth create work2 --share-context', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Share context only within a specific group')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth create backup --share-context --context-group sprint-a', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Advanced: deeper shared continuity for session history artifacts')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth create backup --share-context --context-group sprint-a --deeper-continuity', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Create clean profile without shared commands/skills/agents')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth create sandbox --bare', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Change shared resources for an existing account')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth resources work --mode profile-local', 'command')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth resources work --mode shared', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Set work as default')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth default work', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Backup the local continuity lane for an account or plain ccs')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth backup work', 'command')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth backup default', 'command')}  ${(0, ui_1.dim)('# backup plain ccs lane')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# Restore original CCS behavior')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth reset-default', 'command')}`);
        console.log('');
        console.log(`  ${(0, ui_1.dim)('# List all profiles')}`);
        console.log(`  ${(0, ui_1.color)('ccs auth list', 'command')}`);
        console.log('');
        console.log((0, ui_1.subheader)('Options'));
        console.log(`  ${(0, ui_1.color)('--force', 'command')}                   Allow overwriting existing profile (create)`);
        console.log(`  ${(0, ui_1.color)('--share-context', 'command')}           Share project workspace context across accounts`);
        console.log(`  ${(0, ui_1.color)('--context-group <name>', 'command')}    Share context only within a named group`);
        console.log(`  ${(0, ui_1.color)('--deeper-continuity', 'command')}       Advanced shared mode: sync additional continuity artifacts`);
        console.log(`  ${(0, ui_1.color)('--bare', 'command')}                    Create clean profile without shared symlinks (no CK/commands/skills)`);
        console.log(`  ${(0, ui_1.color)('--mode <mode>', 'command')}              Shared resource mode for resources: shared|profile-local`);
        console.log(`  ${(0, ui_1.color)('--yes, -y', 'command')}                 Skip confirmation prompts (remove)`);
        console.log(`  ${(0, ui_1.color)('--json', 'command')}                    Output in JSON format (list, show)`);
        console.log(`  ${(0, ui_1.color)('--verbose', 'command')}                 Show additional details (list)`);
        console.log('');
        console.log((0, ui_1.subheader)('Note'));
        console.log(`  By default, ${(0, ui_1.color)('ccs', 'command')} uses Claude CLI defaults from ~/.claude/`);
        console.log(`  Recommended two-account route: create ${(0, ui_1.color)('work', 'command')} and ${(0, ui_1.color)('personal', 'command')}, then run the profile you want.`);
        console.log(`  Use ${(0, ui_1.color)('ccs auth default <profile>', 'command')} to change the default profile.`);
        console.log(`  Account logins, tokens, and .anthropic stay isolated for every profile.`);
        console.log(`  Non-bare account profiles share basic ${(0, ui_1.color)('settings.json', 'path')} with ${(0, ui_1.color)('~/.claude/settings.json', 'path')}; ${(0, ui_1.color)('ccs auth show <profile>', 'command')} shows the link state.`);
        console.log(`  Shared Resources control plugins/commands/skills/agents/settings.json; History Sync controls project/session continuity only.`);
        console.log(`  History sync is opt-in: both accounts need shared mode and the same ${(0, ui_1.color)('context_group', 'path')}.`);
        console.log(`  ${(0, ui_1.color)('--deeper-continuity', 'command')} requires shared mode and syncs session-env/file-history/todos/shell-snapshots.`);
        console.log(`  To make future plain ${(0, ui_1.color)('ccs', 'command')} resume with an account, set ${(0, ui_1.color)('ccs auth default <profile>', 'command')}; back up the current native lane first with ${(0, ui_1.color)('ccs auth backup default', 'command')}.`);
        console.log(`  Existing history sync: open ${(0, ui_1.color)('ccs config', 'command')} -> Accounts -> Edit Context.`);
        console.log(`  Existing shared resources: use ${(0, ui_1.color)('ccs auth resources <profile> --mode shared|profile-local', 'command')}.`);
        console.log(`  Shared context groups are normalized (trim + lowercase) and spaces become "-".`);
        console.log(`  ${(0, ui_1.color)('context_group', 'path')} must be non-empty and <= ${account_context_1.MAX_CONTEXT_GROUP_LENGTH} chars in shared mode.`);
        console.log('');
    }
    /**
     * Create new profile - delegates to create-command.ts
     */
    async handleCreate(args) {
        return (0, commands_1.handleCreate)(this.getContext(), args);
    }
    async handleBackup(args) {
        return (0, commands_1.handleBackup)(this.getContext(), args);
    }
    /**
     * List all profiles - delegates to list-command.ts
     */
    async handleList(args) {
        return (0, commands_1.handleList)(this.getContext(), args);
    }
    /**
     * Show profile details - delegates to show-command.ts
     */
    async handleShow(args) {
        return (0, commands_1.handleShow)(this.getContext(), args);
    }
    async handleResources(args) {
        return (0, commands_1.handleResources)(this.getContext(), args);
    }
    /**
     * Remove profile - delegates to remove-command.ts
     */
    async handleRemove(args) {
        return (0, commands_1.handleRemove)(this.getContext(), args);
    }
    /**
     * Set default profile - delegates to default-command.ts
     */
    async handleDefault(args) {
        return (0, commands_1.handleDefault)(this.getContext(), args);
    }
    /**
     * Reset default profile - delegates to default-command.ts
     */
    async handleResetDefault(args = []) {
        return (0, commands_1.handleResetDefault)(this.getContext(), args);
    }
    /**
     * Route auth command to appropriate handler
     */
    async route(args) {
        if (args.length === 0 || args[0] === '--help' || args[0] === '-h' || args[0] === 'help') {
            await this.showHelp();
            return;
        }
        const command = args[0];
        const commandArgs = args.slice(1);
        switch (command) {
            case 'create':
                await this.handleCreate(commandArgs);
                break;
            case 'save':
                // Deprecated - redirect to create
                await (0, ui_1.initUI)();
                console.log((0, ui_1.warn)('Command "save" is deprecated'));
                console.log(`    Use: ${(0, ui_1.color)('ccs auth create <profile>', 'command')} instead`);
                console.log('');
                await this.handleCreate(commandArgs);
                break;
            case 'list':
                await this.handleList(commandArgs);
                break;
            case 'backup':
                await this.handleBackup(commandArgs);
                break;
            case 'show':
                await this.handleShow(commandArgs);
                break;
            case 'resources':
                await this.handleResources(commandArgs);
                break;
            case 'remove':
                await this.handleRemove(commandArgs);
                break;
            case 'default':
                await this.handleDefault(commandArgs);
                break;
            case 'reset-default':
                await this.handleResetDefault(commandArgs);
                break;
            case 'current':
                await (0, ui_1.initUI)();
                console.log((0, ui_1.warn)('Command "current" has been removed'));
                console.log('');
                console.log('Each profile has its own login in an isolated instance.');
                console.log(`Use ${(0, ui_1.color)('ccs auth list', 'command')} to see all profiles.`);
                console.log('');
                break;
            case 'cleanup':
                await (0, ui_1.initUI)();
                console.log((0, ui_1.warn)('Command "cleanup" has been removed'));
                console.log('');
                console.log('No cleanup needed - no separate vault files.');
                console.log(`Use ${(0, ui_1.color)('ccs auth list', 'command')} to see all profiles.`);
                console.log('');
                break;
            default:
                await (0, ui_1.initUI)();
                console.log((0, ui_1.fail)(`Unknown command: ${command}`));
                console.log('');
                console.log('Run for help:');
                console.log(`  ${(0, ui_1.color)('ccs auth --help', 'command')}`);
                process.exit(1);
        }
    }
}
exports.default = AuthCommands;
//# sourceMappingURL=auth-commands.js.map