"use strict";
/**
 * Auth Commands Type Definitions
 *
 * Shared interfaces for auth command modules.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseArgs = exports.rejectUnsupportedAuthOptions = exports.formatRelativeTime = void 0;
const errors_1 = require("../../errors");
const exit_codes_1 = require("../../errors/exit-codes");
const ui_1 = require("../../utils/ui");
// Re-export for backward compatibility
var time_1 = require("../../utils/time");
Object.defineProperty(exports, "formatRelativeTime", { enumerable: true, get: function () { return time_1.formatRelativeTime; } });
function rejectUnsupportedAuthOptions(parsed, options) {
    const unsupportedOptions = [
        ...(parsed.unknownFlags ?? []),
        ...(!options.allowMode && parsed.mode !== undefined ? ['--mode'] : []),
    ];
    if (unsupportedOptions.length === 0) {
        return;
    }
    const unknownList = unsupportedOptions.map((flag) => `"${flag}"`).join(', ');
    console.log((0, ui_1.fail)(`Unknown option(s): ${unknownList}`));
    console.log('');
    console.log(`Usage: ${(0, ui_1.color)(options.usage, 'command')}`);
    console.log(`Help:  ${(0, ui_1.color)('ccs auth --help', 'command')}`);
    console.log('');
    (0, errors_1.exitWithError)(`Unknown option(s): ${unknownList}`, exit_codes_1.ExitCode.PROFILE_ERROR);
}
exports.rejectUnsupportedAuthOptions = rejectUnsupportedAuthOptions;
/**
 * Parse command arguments from raw args array
 */
function parseArgs(args, options = {}) {
    let profileName;
    let contextGroup;
    let mode;
    const unknownFlags = new Set();
    const knownBooleanFlags = new Set([
        '--force',
        '--verbose',
        '--json',
        '--yes',
        '-y',
        '--share-context',
        '--deeper-continuity',
        '--bare',
    ]);
    const knownValueFlags = new Set(['--context-group']);
    if (options.allowMode) {
        knownValueFlags.add('--mode');
    }
    for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (arg === '--context-group') {
            const next = args[i + 1];
            if (!next || next.startsWith('-')) {
                contextGroup = '';
                continue;
            }
            contextGroup = next;
            i++;
            continue;
        }
        if (options.allowMode && arg === '--mode') {
            const next = args[i + 1];
            if (!next || next.startsWith('-')) {
                mode = '';
                continue;
            }
            mode = next;
            i++;
            continue;
        }
        if (arg.startsWith('--context-group=')) {
            contextGroup = arg.slice('--context-group='.length);
            continue;
        }
        if (options.allowMode && arg.startsWith('--mode=')) {
            mode = arg.slice('--mode='.length);
            continue;
        }
        if (arg.startsWith('-')) {
            const normalizedFlag = arg.includes('=') ? arg.slice(0, arg.indexOf('=')) : arg;
            const isKnownFlag = knownBooleanFlags.has(normalizedFlag) || knownValueFlags.has(normalizedFlag);
            if (!isKnownFlag) {
                unknownFlags.add(normalizedFlag);
                // Best effort: unknown flags often take a value token.
                // Skip one following non-flag token to avoid mis-parsing profile name.
                const next = args[i + 1];
                if (!arg.includes('=') && next && !next.startsWith('-')) {
                    i++;
                }
            }
            continue;
        }
        if (!profileName) {
            profileName = arg;
        }
    }
    return {
        profileName,
        force: args.includes('--force'),
        verbose: args.includes('--verbose'),
        json: args.includes('--json'),
        yes: args.includes('--yes') || args.includes('-y'),
        shareContext: args.includes('--share-context'),
        deeperContinuity: args.includes('--deeper-continuity'),
        bare: args.includes('--bare'),
        mode,
        contextGroup,
        unknownFlags: [...unknownFlags],
    };
}
exports.parseArgs = parseArgs;
//# sourceMappingURL=types.js.map