"use strict";
/**
 * Shell Completion Command Handler
 *
 * Handle --shell-completion 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.handleShellCompletionCommand = exports.createShellCompletionCommandContract = exports.parseShellCompletionArgs = exports.showShellCompletionHelp = void 0;
const ui_1 = require("../utils/ui");
const command_execution_contract_1 = require("./command-execution-contract");
function showShellCompletionHelp(writeLine = console.log) {
    writeLine((0, ui_1.header)('Shell Completion'));
    writeLine('');
    writeLine((0, ui_1.color)('Usage:', 'info'));
    writeLine('  ccs --shell-completion              # Auto-detect shell and install');
    writeLine('  ccs --shell-completion --bash       # Install for bash');
    writeLine('  ccs --shell-completion --zsh        # Install for zsh');
    writeLine('  ccs --shell-completion --fish       # Install for fish');
    writeLine('  ccs --shell-completion --powershell # Install for PowerShell');
    writeLine('  ccs --shell-completion --force      # Reinstall/refresh the active shell setup');
    writeLine('');
    writeLine((0, ui_1.color)('Test:', 'info'));
    writeLine('  ccs <TAB>');
    writeLine('  ccs help <TAB>');
    writeLine('  ccs auth <TAB>');
    writeLine('');
}
exports.showShellCompletionHelp = showShellCompletionHelp;
function parseShellCompletionArgs(args) {
    let targetShell = null;
    const force = args.includes('--force') || args.includes('-f');
    if (args.includes('--bash'))
        targetShell = 'bash';
    else if (args.includes('--zsh'))
        targetShell = 'zsh';
    else if (args.includes('--fish'))
        targetShell = 'fish';
    else if (args.includes('--powershell'))
        targetShell = 'powershell';
    return { targetShell, force };
}
exports.parseShellCompletionArgs = parseShellCompletionArgs;
function createShellCompletionCommandContract(installer) {
    return {
        parse: parseShellCompletionArgs,
        validate: () => {
            // No validation at this stage to preserve existing behavior exactly.
        },
        execute: (parsed) => installer.install(parsed.targetShell, { force: parsed.force }),
        render: (result, context) => {
            if (result.alreadyInstalled && !context.parsedArgs.force) {
                console.log((0, ui_1.ok)('Shell completion already installed'));
                console.log(`    Use ${(0, ui_1.color)('--force', 'warning')} to reinstall`);
                console.log('');
                return;
            }
            console.log((0, ui_1.ok)('Shell completion installed successfully!'));
            console.log('');
            console.log(result.message);
            console.log('');
            console.log((0, ui_1.color)('To activate:', 'info'));
            console.log(`  ${result.reload}`);
            console.log('');
            console.log((0, ui_1.color)('Then test:', 'info'));
            console.log('  ccs <TAB>        # See available profiles');
            console.log('  ccs auth <TAB>   # See auth subcommands');
            console.log('');
        },
    };
}
exports.createShellCompletionCommandContract = createShellCompletionCommandContract;
/**
 * Handle shell completion command
 */
async function handleShellCompletionCommand(args) {
    await (0, ui_1.initUI)();
    if (args.includes('--help') || args.includes('-h')) {
        showShellCompletionHelp();
        return;
    }
    try {
        const { ShellCompletionInstaller } = await Promise.resolve().then(() => __importStar(require('../utils/shell-completion')));
        console.log((0, ui_1.header)('Shell Completion Installer'));
        console.log('');
        const installer = new ShellCompletionInstaller();
        const contract = createShellCompletionCommandContract(installer);
        await (0, command_execution_contract_1.runCommandWithContract)(args, contract);
    }
    catch (error) {
        const err = error;
        console.error((0, ui_1.fail)(`Error: ${err.message}`));
        console.error('');
        showShellCompletionHelp(console.error);
        process.exit(1);
    }
}
exports.handleShellCompletionCommand = handleShellCompletionCommand;
//# sourceMappingURL=shell-completion-command.js.map