"use strict";
/**
 * respond Command - Respond to an agent's prompt
 * Issue #518: [DR1-08] Factory pattern
 *
 * Uses prompt-response API (not respond API) [DR2-06]
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRespondCommand = createRespondCommand;
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");
function createRespondCommand() {
    const cmd = new commander_1.Command('respond');
    cmd
        .description("Respond to an agent's prompt (yes/no, number, or text)")
        .argument('<worktree-id>', 'Worktree ID')
        .argument('<answer>', 'Response answer (yes, no, number, or free text)')
        .option('--agent <agent>', 'CLI tool agent (claude, codex, gemini, vibe-local, opencode)')
        .option('--token <token>', command_helpers_1.TOKEN_WARNING)
        .action(async (worktreeId, answer, 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);
            }
            // Validate agent if provided
            if (options.agent && !(0, cli_tool_ids_1.isCliToolId)(options.agent)) {
                console.error('Error: Invalid agent.');
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            // Validate answer is not empty
            if (!answer.trim()) {
                console.error('Error: Answer cannot be empty.');
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            const client = new api_client_1.ApiClient({ token: options.token });
            // [DR2-06] Use prompt-response API with cliTool (not cliToolId)
            const body = { answer };
            if (options.agent) {
                body.cliTool = options.agent;
            }
            const result = await client.post(`/api/worktrees/${worktreeId}/prompt-response`, body);
            if (result && !result.success) {
                // [DR2-06] Check reason for failure
                const reason = result.reason || 'unknown';
                console.error(`Warning: Response may not have been applied. Reason: ${reason}`);
                process.exit(types_1.ExitCode.UNEXPECTED_ERROR);
            }
            console.error('Response sent.');
        }
        catch (error) {
            (0, command_helpers_1.handleCommandError)(error);
        }
    });
    return cmd;
}
