"use strict";
/**
 * send Command - Send a message to a worktree agent
 * Issue #518: [DR1-08] Factory pattern
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSendCommand = createSendCommand;
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 duration_constants_1 = require("../config/duration-constants");
const cli_tool_ids_1 = require("../config/cli-tool-ids");
function createSendCommand() {
    const cmd = new commander_1.Command('send');
    cmd
        .description('Send a message to a worktree agent')
        .argument('<worktree-id>', 'Worktree ID')
        .argument('<message>', 'Message to send')
        .option('--agent <agent>', 'CLI tool agent (claude, codex, gemini, vibe-local, opencode)')
        .option('--auto-yes', 'Enable auto-yes before sending')
        .option('--duration <duration>', `Auto-yes duration (${duration_constants_1.ALLOWED_DURATIONS.join(', ')})`)
        .option('--stop-pattern <pattern>', 'Auto-yes stop pattern (regex)')
        .option('--token <token>', command_helpers_1.TOKEN_WARNING)
        .action(async (worktreeId, message, 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. Must be one of: ${cli_tool_ids_1.CLI_TOOL_IDS.join(', ')}`);
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            // [SEC4-06] Validate stop-pattern length
            if (options.stopPattern && options.stopPattern.length > api_client_1.MAX_STOP_PATTERN_LENGTH) {
                console.error(`Error: stop-pattern exceeds maximum length of ${api_client_1.MAX_STOP_PATTERN_LENGTH} characters.`);
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            const client = new api_client_1.ApiClient({ token: options.token });
            // --auto-yes: enable auto-yes first [DR2-02]
            if (options.autoYes) {
                const durationMs = options.duration
                    ? (0, duration_constants_1.parseDurationToMs)(options.duration)
                    : (0, duration_constants_1.parseDurationToMs)('1h'); // default 1h
                if (durationMs === null) {
                    console.error(`Error: Invalid duration. Must be one of: ${duration_constants_1.ALLOWED_DURATIONS.join(', ')}`);
                    process.exit(types_1.ExitCode.CONFIG_ERROR);
                }
                const autoYesBody = {
                    enabled: true,
                    duration: durationMs,
                };
                if (options.agent) {
                    autoYesBody.cliToolId = options.agent;
                }
                if (options.stopPattern) {
                    autoYesBody.stopPattern = options.stopPattern;
                }
                await client.post(`/api/worktrees/${worktreeId}/auto-yes`, autoYesBody);
                console.error('Auto-yes enabled.');
            }
            // [DR2-05] Send API uses "content" not "message"
            const sendBody = { content: message };
            if (options.agent) {
                sendBody.cliToolId = options.agent;
            }
            await client.post(`/api/worktrees/${worktreeId}/send`, sendBody);
            console.error('Message sent.');
        }
        catch (error) {
            (0, command_helpers_1.handleCommandError)(error);
        }
    });
    return cmd;
}
