"use strict";
/**
 * auto-yes Command - Control auto-yes for a worktree
 * Issue #518: [DR1-08] Factory pattern
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAutoYesCommand = createAutoYesCommand;
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 createAutoYesCommand() {
    const cmd = new commander_1.Command('auto-yes');
    cmd
        .description('Control auto-yes for a worktree')
        .argument('<worktree-id>', 'Worktree ID')
        .option('--enable', 'Enable auto-yes')
        .option('--disable', 'Disable auto-yes')
        .option('--duration <duration>', `Duration (${duration_constants_1.ALLOWED_DURATIONS.join(', ')})`)
        .option('--stop-pattern <pattern>', 'Stop pattern (regex, max 500 chars)')
        .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);
            }
            // Must specify --enable or --disable
            if (!options.enable && !options.disable) {
                console.error('Error: Specify --enable or --disable.');
                process.exit(types_1.ExitCode.CONFIG_ERROR);
            }
            if (options.enable && options.disable) {
                console.error('Error: Cannot specify both --enable and --disable.');
                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);
            }
            // [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 });
            const body = {
                enabled: !!options.enable,
            };
            if (options.enable) {
                // [DR2-02] Convert duration string to ms
                const durationMs = options.duration
                    ? (0, duration_constants_1.parseDurationToMs)(options.duration)
                    : (0, duration_constants_1.parseDurationToMs)('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);
                }
                body.duration = durationMs;
                if (options.stopPattern) {
                    body.stopPattern = options.stopPattern;
                }
            }
            if (options.agent) {
                body.cliToolId = options.agent;
            }
            await client.post(`/api/worktrees/${worktreeId}/auto-yes`, body);
            if (options.enable) {
                console.error(`Auto-yes enabled for ${worktreeId}.`);
            }
            else {
                console.error(`Auto-yes disabled for ${worktreeId}.`);
            }
        }
        catch (error) {
            (0, command_helpers_1.handleCommandError)(error);
        }
    });
    return cmd;
}
