"use strict";
/**
 * CLIProxy Executor Arg Parser
 *
 * Extracted from index.ts (Concern A):
 *   - readOptionValue
 *   - hasGitLabTokenLoginFlag / getGitLabTokenLoginFlagName
 *   - CCS_FLAGS constant + filterCcsFlags()
 *   - parseExecutorFlags() — flag extraction block (lines ~411-639 in original)
 *   - validateFlagCombinations() — cross-flag guard block (lines ~531-585)
 *
 * IMPORTANT: process.exit semantics are kept identical to original index.ts.
 * All console.error messages are byte-identical.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateFlagCombinations = exports.parseExecutorFlags = exports.filterCcsFlags = exports.CCS_FLAGS = exports.getGitLabTokenLoginFlagName = exports.hasGitLabTokenLoginFlag = exports.readOptionValue = void 0;
const indicators_1 = require("../../utils/ui/indicators");
const auth_types_1 = require("../auth/auth-types");
const proxy_config_resolver_1 = require("../proxy/proxy-config-resolver");
const thinking_arg_parser_1 = require("./thinking-arg-parser");
// Inlined from antigravity-responsibility.ts to avoid pulling in unified-config-loader
// (which requires js-yaml at runtime). Keep in sync if ANTIGRAVITY_ACCEPT_RISK_FLAGS changes.
const ANTIGRAVITY_ACCEPT_RISK_FLAGS_LOCAL = [
    '--accept-agr-risk',
    '--accept-antigravity-risk',
];
function hasAntigravityRiskAcceptanceFlag(args) {
    return args.some((arg) => ANTIGRAVITY_ACCEPT_RISK_FLAGS_LOCAL.includes(arg));
}
// ── Simple Helpers ────────────────────────────────────────────────────────────
/**
 * Parse a flag value from args supporting both `--flag value` and `--flag=value` forms.
 * Returns the shape used throughout the executor:
 *   { present, value?, missingValue }
 */
function readOptionValue(args, flag) {
    const inlinePrefix = `${flag}=`;
    const inlineArg = args.find((arg) => arg.startsWith(inlinePrefix));
    if (inlineArg !== undefined) {
        const value = inlineArg.slice(inlinePrefix.length).trim();
        return {
            present: true,
            value: value.length > 0 ? value : undefined,
            missingValue: value.length === 0,
        };
    }
    const index = args.indexOf(flag);
    if (index === -1) {
        return { present: false, missingValue: false };
    }
    const next = args[index + 1];
    if (!next || next.startsWith('-')) {
        return { present: true, missingValue: true };
    }
    return { present: true, value: next.trim(), missingValue: false };
}
exports.readOptionValue = readOptionValue;
/** Returns true if args contain a GitLab token-login flag. */
function hasGitLabTokenLoginFlag(args) {
    return args.includes('--gitlab-token-login') || args.includes('--token-login');
}
exports.hasGitLabTokenLoginFlag = hasGitLabTokenLoginFlag;
/** Returns the specific flag name present in args (used for error messages). */
function getGitLabTokenLoginFlagName(args) {
    return args.includes('--gitlab-token-login') ? '--gitlab-token-login' : '--token-login';
}
exports.getGitLabTokenLoginFlagName = getGitLabTokenLoginFlagName;
// ── CCS Flags Filter ──────────────────────────────────────────────────────────
/**
 * CCS-specific flags that must not be forwarded to the underlying Claude CLI.
 * The list is kept here as the single source of truth (previously inlined in index.ts).
 */
exports.CCS_FLAGS = [
    '--auth',
    '--paste-callback',
    '--port-forward',
    '--headless',
    '--logout',
    '--config',
    '--add',
    '--accounts',
    '--use',
    '--nickname',
    '--kiro-auth-method',
    '--kiro-idc-start-url',
    '--kiro-idc-region',
    '--kiro-idc-flow',
    '--thinking',
    '--effort',
    '--1m',
    '--no-1m',
    '--incognito',
    '--no-incognito',
    '--import',
    '--accept-agr-risk',
    '--accept-antigravity-risk',
    '--settings',
    ...proxy_config_resolver_1.PROXY_CLI_FLAGS,
];
/**
 * Filter all CCS-specific flags (and their value arguments) from args
 * before forwarding to the Claude CLI.
 * Mirrors the filter logic from index.ts lines ~1328-1349.
 */
function filterCcsFlags(args) {
    return args.filter((arg, idx) => {
        if (exports.CCS_FLAGS.includes(arg))
            return false;
        if (arg.startsWith('--kiro-auth-method='))
            return false;
        if (arg.startsWith('--kiro-idc-start-url='))
            return false;
        if (arg.startsWith('--kiro-idc-region='))
            return false;
        if (arg.startsWith('--kiro-idc-flow='))
            return false;
        if (arg.startsWith('--thinking='))
            return false;
        if (arg.startsWith('--effort='))
            return false;
        if (arg.startsWith('--1m=') || arg.startsWith('--no-1m='))
            return false;
        if (args[idx - 1] === '--use' ||
            args[idx - 1] === '--nickname' ||
            args[idx - 1] === '--kiro-auth-method' ||
            args[idx - 1] === '--kiro-idc-start-url' ||
            args[idx - 1] === '--kiro-idc-region' ||
            args[idx - 1] === '--kiro-idc-flow' ||
            args[idx - 1] === '--thinking' ||
            args[idx - 1] === '--effort')
            return false;
        return true;
    });
}
exports.filterCcsFlags = filterCcsFlags;
/**
 * Parse all CCS executor flags from args.
 *
 * Exits with code 1 (process.exitCode = 1 + return) on invalid flag values.
 * Exits with process.exit(1) on conflicting flag combinations — identical to
 * the original index.ts behavior.
 *
 * @param args     args AFTER proxy flags have been stripped (argsWithoutProxy)
 * @param context  provider context needed for kiro/incognito defaults
 */
function parseExecutorFlags(args, context) {
    const { provider, unifiedConfig } = context;
    const forceAuth = args.includes('--auth');
    const pasteCallback = args.includes('--paste-callback');
    const portForward = args.includes('--port-forward');
    const forceHeadless = args.includes('--headless');
    if (pasteCallback && portForward) {
        console.error((0, indicators_1.fail)('Cannot use --paste-callback with --port-forward'));
        console.error('    --paste-callback: Manually paste OAuth redirect URL');
        console.error('    --port-forward: Use SSH port forwarding for callback');
        process.exit(1);
    }
    const forceLogout = args.includes('--logout');
    const forceConfig = args.includes('--config');
    const addAccount = args.includes('--add');
    const showAccounts = args.includes('--accounts');
    const forceImport = args.includes('--import');
    const gitlabTokenLogin = hasGitLabTokenLoginFlag(args);
    const acceptAgyRisk = hasAntigravityRiskAcceptanceFlag(args);
    const incognitoFlag = args.includes('--incognito');
    const noIncognitoFlag = args.includes('--no-incognito');
    const kiroNoIncognitoConfig = provider === 'kiro' ? (unifiedConfig.cliproxy?.kiro_no_incognito ?? true) : false;
    const noIncognito = incognitoFlag ? false : noIncognitoFlag || kiroNoIncognitoConfig;
    // Parse --use flag
    let useAccount;
    const useIdx = args.indexOf('--use');
    if (useIdx !== -1 && args[useIdx + 1] && !args[useIdx + 1].startsWith('-')) {
        useAccount = args[useIdx + 1];
    }
    // Parse --nickname flag
    let setNickname;
    const nicknameIdx = args.indexOf('--nickname');
    if (nicknameIdx !== -1 && args[nicknameIdx + 1] && !args[nicknameIdx + 1].startsWith('-')) {
        setNickname = args[nicknameIdx + 1];
    }
    // Parse --kiro-auth-method flag
    let kiroAuthMethod;
    const kiroMethodValue = readOptionValue(args, '--kiro-auth-method');
    if (kiroMethodValue.present) {
        const rawMethod = kiroMethodValue.value;
        if (kiroMethodValue.missingValue || !rawMethod) {
            console.error((0, indicators_1.fail)('--kiro-auth-method requires a value'));
            console.error('    Supported values: aws, aws-authcode, google, github, idc');
            process.exitCode = 1;
            // Caller must check process.exitCode = 1 and bail — matching original return behavior
            return buildPartialFlags({
                forceAuth,
                pasteCallback,
                portForward,
                forceHeadless,
                forceLogout,
                forceConfig,
                addAccount,
                showAccounts,
                forceImport,
                gitlabTokenLogin,
                acceptAgyRisk,
                incognitoFlag,
                noIncognitoFlag,
                noIncognito,
                useAccount,
                setNickname,
                kiroAuthMethod: undefined,
                kiroIDCStartUrl: undefined,
                kiroIDCRegion: undefined,
                kiroIDCFlow: undefined,
                gitlabBaseUrl: undefined,
                extendedContextOverride: undefined,
                thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
            });
        }
        const normalized = rawMethod.trim().toLowerCase();
        if (!(0, auth_types_1.isKiroAuthMethod)(normalized)) {
            console.error((0, indicators_1.fail)(`Invalid --kiro-auth-method value: ${rawMethod}`));
            console.error('    Supported values: aws, aws-authcode, google, github, idc');
            process.exitCode = 1;
            return buildPartialFlags({
                forceAuth,
                pasteCallback,
                portForward,
                forceHeadless,
                forceLogout,
                forceConfig,
                addAccount,
                showAccounts,
                forceImport,
                gitlabTokenLogin,
                acceptAgyRisk,
                incognitoFlag,
                noIncognitoFlag,
                noIncognito,
                useAccount,
                setNickname,
                kiroAuthMethod: undefined,
                kiroIDCStartUrl: undefined,
                kiroIDCRegion: undefined,
                kiroIDCFlow: undefined,
                gitlabBaseUrl: undefined,
                extendedContextOverride: undefined,
                thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
            });
        }
        kiroAuthMethod = (0, auth_types_1.normalizeKiroAuthMethod)(normalized);
    }
    let kiroIDCStartUrl;
    const kiroIDCStartUrlValue = readOptionValue(args, '--kiro-idc-start-url');
    if (kiroIDCStartUrlValue.present && kiroIDCStartUrlValue.value) {
        kiroIDCStartUrl = kiroIDCStartUrlValue.value;
    }
    else if (kiroIDCStartUrlValue.present) {
        console.error((0, indicators_1.fail)('--kiro-idc-start-url requires a value'));
        process.exitCode = 1;
        return buildPartialFlags({
            forceAuth,
            pasteCallback,
            portForward,
            forceHeadless,
            forceLogout,
            forceConfig,
            addAccount,
            showAccounts,
            forceImport,
            gitlabTokenLogin,
            acceptAgyRisk,
            incognitoFlag,
            noIncognitoFlag,
            noIncognito,
            useAccount,
            setNickname,
            kiroAuthMethod,
            kiroIDCStartUrl: undefined,
            kiroIDCRegion: undefined,
            kiroIDCFlow: undefined,
            gitlabBaseUrl: undefined,
            extendedContextOverride: undefined,
            thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
        });
    }
    let kiroIDCRegion;
    const kiroIDCRegionValue = readOptionValue(args, '--kiro-idc-region');
    if (kiroIDCRegionValue.present && kiroIDCRegionValue.value) {
        kiroIDCRegion = kiroIDCRegionValue.value;
    }
    else if (kiroIDCRegionValue.present) {
        console.error((0, indicators_1.fail)('--kiro-idc-region requires a value'));
        process.exitCode = 1;
        return buildPartialFlags({
            forceAuth,
            pasteCallback,
            portForward,
            forceHeadless,
            forceLogout,
            forceConfig,
            addAccount,
            showAccounts,
            forceImport,
            gitlabTokenLogin,
            acceptAgyRisk,
            incognitoFlag,
            noIncognitoFlag,
            noIncognito,
            useAccount,
            setNickname,
            kiroAuthMethod,
            kiroIDCStartUrl,
            kiroIDCRegion: undefined,
            kiroIDCFlow: undefined,
            gitlabBaseUrl: undefined,
            extendedContextOverride: undefined,
            thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
        });
    }
    let kiroIDCFlow;
    const kiroIDCFlowValue = readOptionValue(args, '--kiro-idc-flow');
    if (kiroIDCFlowValue.present) {
        const rawFlow = kiroIDCFlowValue.value;
        if (kiroIDCFlowValue.missingValue || !rawFlow) {
            console.error((0, indicators_1.fail)('--kiro-idc-flow requires a value'));
            console.error('    Supported values: authcode, device');
            process.exitCode = 1;
            return buildPartialFlags({
                forceAuth,
                pasteCallback,
                portForward,
                forceHeadless,
                forceLogout,
                forceConfig,
                addAccount,
                showAccounts,
                forceImport,
                gitlabTokenLogin,
                acceptAgyRisk,
                incognitoFlag,
                noIncognitoFlag,
                noIncognito,
                useAccount,
                setNickname,
                kiroAuthMethod,
                kiroIDCStartUrl,
                kiroIDCRegion,
                kiroIDCFlow: undefined,
                gitlabBaseUrl: undefined,
                extendedContextOverride: undefined,
                thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
            });
        }
        const normalized = rawFlow.trim().toLowerCase();
        if (!(0, auth_types_1.isKiroIDCFlow)(normalized)) {
            console.error((0, indicators_1.fail)(`Invalid --kiro-idc-flow value: ${rawFlow}`));
            console.error('    Supported values: authcode, device');
            process.exitCode = 1;
            return buildPartialFlags({
                forceAuth,
                pasteCallback,
                portForward,
                forceHeadless,
                forceLogout,
                forceConfig,
                addAccount,
                showAccounts,
                forceImport,
                gitlabTokenLogin,
                acceptAgyRisk,
                incognitoFlag,
                noIncognitoFlag,
                noIncognito,
                useAccount,
                setNickname,
                kiroAuthMethod,
                kiroIDCStartUrl,
                kiroIDCRegion,
                kiroIDCFlow: undefined,
                gitlabBaseUrl: undefined,
                extendedContextOverride: undefined,
                thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
            });
        }
        kiroIDCFlow = (0, auth_types_1.normalizeKiroIDCFlow)(normalized);
    }
    let gitlabBaseUrl;
    const gitlabBaseUrlValue = readOptionValue(args, '--gitlab-url');
    if (gitlabBaseUrlValue.present && gitlabBaseUrlValue.value) {
        gitlabBaseUrl = gitlabBaseUrlValue.value.trim();
    }
    else if (gitlabBaseUrlValue.present) {
        console.error((0, indicators_1.fail)('--gitlab-url requires a value'));
        process.exitCode = 1;
        return buildPartialFlags({
            forceAuth,
            pasteCallback,
            portForward,
            forceHeadless,
            forceLogout,
            forceConfig,
            addAccount,
            showAccounts,
            forceImport,
            gitlabTokenLogin,
            acceptAgyRisk,
            incognitoFlag,
            noIncognitoFlag,
            noIncognito,
            useAccount,
            setNickname,
            kiroAuthMethod,
            kiroIDCStartUrl,
            kiroIDCRegion,
            kiroIDCFlow,
            gitlabBaseUrl: undefined,
            extendedContextOverride: undefined,
            thinkingParse: (0, thinking_arg_parser_1.parseThinkingOverride)(args),
        });
    }
    // Parse --thinking / --effort flags (aliases; first occurrence wins)
    const thinkingParse = (0, thinking_arg_parser_1.parseThinkingOverride)(args);
    if (thinkingParse.error) {
        const { flag } = thinkingParse.error;
        console.error((0, indicators_1.fail)(`${flag} requires a value`));
        if (provider === 'codex') {
            console.error('    Codex examples: --effort xhigh, --effort high, --effort medium');
            console.error('    Alias: --thinking xhigh (same behavior)');
        }
        else {
            console.error('    Examples: --thinking low, --thinking 8192, --thinking off');
            console.error('    Levels: minimal, low, medium, high, xhigh, max, auto');
        }
        process.exit(1);
    }
    // Parse --1m / --no-1m flags for extended context (1M token window)
    let extendedContextOverride;
    const has1mFlag = args.includes('--1m') || args.some((arg) => arg.startsWith('--1m='));
    const hasNo1mFlag = args.includes('--no-1m') || args.some((arg) => arg.startsWith('--no-1m='));
    if (has1mFlag && hasNo1mFlag) {
        console.error((0, indicators_1.fail)('Cannot use both --1m and --no-1m flags'));
        process.exit(1);
    }
    else if (has1mFlag) {
        extendedContextOverride = true;
    }
    else if (hasNo1mFlag) {
        extendedContextOverride = false;
    }
    // Auto-set kiroAuthMethod = 'idc' if IDC sub-flags present without explicit method
    if (!kiroAuthMethod && (kiroIDCStartUrl || kiroIDCRegion || kiroIDCFlow)) {
        kiroAuthMethod = 'idc';
    }
    return {
        forceAuth,
        pasteCallback,
        portForward,
        forceHeadless,
        forceLogout,
        forceConfig,
        addAccount,
        showAccounts,
        forceImport,
        gitlabTokenLogin,
        acceptAgyRisk,
        incognitoFlag,
        noIncognitoFlag,
        noIncognito,
        useAccount,
        setNickname,
        kiroAuthMethod,
        kiroIDCStartUrl,
        kiroIDCRegion,
        kiroIDCFlow,
        gitlabBaseUrl,
        extendedContextOverride,
        thinkingParse,
    };
}
exports.parseExecutorFlags = parseExecutorFlags;
/** Internal helper — builds a ParsedExecutorFlags from raw fields (avoids repeating the full struct). */
function buildPartialFlags(fields) {
    return fields;
}
// ── Cross-Flag Validation ─────────────────────────────────────────────────────
/**
 * Validate flag combinations that are mutually exclusive or provider-scoped.
 * Calls process.exit(1) on any violation — identical to original index.ts.
 * Call AFTER parseExecutorFlags() and only if process.exitCode is still 0.
 *
 * @param parsed   Result of parseExecutorFlags()
 * @param context  Provider context (provider string + compositeProviders list)
 * @param args     Raw argsWithoutProxy (needed for getGitLabTokenLoginFlagName)
 */
function validateFlagCombinations(parsed, context, args) {
    const { provider, compositeProviders } = context;
    const { kiroAuthMethod, kiroIDCStartUrl, kiroIDCRegion, kiroIDCFlow, gitlabTokenLogin, gitlabBaseUrl, } = parsed;
    if (kiroAuthMethod && provider !== 'kiro' && !compositeProviders.includes('kiro')) {
        console.error((0, indicators_1.fail)('--kiro-auth-method is only valid for ccs kiro'));
        process.exitCode = 1;
        return;
    }
    if ((kiroIDCStartUrl || kiroIDCRegion || kiroIDCFlow) &&
        provider !== 'kiro' &&
        !compositeProviders.includes('kiro')) {
        console.error((0, indicators_1.fail)('--kiro-idc-start-url, --kiro-idc-region, and --kiro-idc-flow are only valid for ccs kiro'));
        process.exitCode = 1;
        return;
    }
    if (kiroAuthMethod === 'idc' && !kiroIDCStartUrl) {
        console.error((0, indicators_1.fail)('Kiro IDC login requires --kiro-idc-start-url'));
        console.error('    Example: ccs kiro --auth --kiro-auth-method idc --kiro-idc-start-url https://d-xxx.awsapps.com/start');
        process.exitCode = 1;
        return;
    }
    if (kiroAuthMethod &&
        kiroAuthMethod !== 'idc' &&
        (kiroIDCStartUrl || kiroIDCRegion || kiroIDCFlow)) {
        console.error((0, indicators_1.fail)('--kiro-idc-start-url, --kiro-idc-region, and --kiro-idc-flow require --kiro-auth-method idc'));
        process.exitCode = 1;
        return;
    }
    if ((gitlabTokenLogin || gitlabBaseUrl) && provider !== 'gitlab') {
        const flagName = gitlabTokenLogin ? getGitLabTokenLoginFlagName(args) : '--gitlab-url';
        console.error((0, indicators_1.fail)(`${flagName} is only valid for ccs gitlab`));
        process.exitCode = 1;
        return;
    }
}
exports.validateFlagCombinations = validateFlagCombinations;
//# sourceMappingURL=arg-parser.js.map