"use strict";
/**
 * capture Command - Get current terminal output
 * Issue #518: [DR1-08] Factory pattern
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCaptureCommand = createCaptureCommand;
const commander_1 = require("commander");
const types_1 = require("../types");
const api_client_1 = require("../utils/api-client");
const command_helpers_1 = require("../utils/command-helpers");
const cli_tool_ids_1 = require("../config/cli-tool-ids");
/**
 * Format capture output as JSON (excluding fullOutput for size).
 */
function formatJson(data) {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const { fullOutput, ...rest } = data;
    return JSON.stringify(rest, null, 2);
}
function createCaptureCommand() {
    const cmd = new commander_1.Command('capture');
    cmd
        .description('Capture current terminal output from a worktree')
        .argument('<worktree-id>', 'Worktree ID')
        .option('--json', 'JSON output (excludes fullOutput)')
        .option('--agent <agent>', 'CLI tool agent (claude, codex, gemini, vibe-local, opencode)')
        .option('--token <token>', command_helpers_1.TOKEN_WARNING)
        .action(async (worktreeId, options) => {
        try {
            // [SEC4-04] Validate worktree ID
            if (!(0, api_client_1.isValidWorktreeId)(worktreeId)) {
                console.error('Error: Invalid worktree ID format.');
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            if (options.agent && !(0, cli_tool_ids_1.isCliToolId)(options.agent)) {
                console.error('Error: Invalid agent.');
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            const client = new api_client_1.ApiClient({ token: options.token });
            // Build path with optional cliTool query parameter
            let path = `/api/worktrees/${worktreeId}/current-output`;
            if (options.agent) {
                path += `?cliTool=${encodeURIComponent(options.agent)}`;
            }
            const data = await client.get(path);
            if (options.json) {
                console.log(formatJson(data));
            }
            else {
                // Default: plain text output (content field)
                console.log(data.content);
            }
        }
        catch (error) {
            (0, command_helpers_1.handleCommandError)(error);
        }
    });
    return cmd;
}
