"use strict";
/**
 * Shared types and utilities for codex-auth command handlers.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.rejectUnsupportedOptions = exports.parseArgs = exports.getProfileNameError = exports.isValidCodexProfileName = exports.formatRelativeTime = void 0;
const ui_1 = require("../../utils/ui");
const errors_1 = require("../../errors");
const exit_codes_1 = require("../../errors/exit-codes");
const types_1 = require("../types");
Object.defineProperty(exports, "isValidCodexProfileName", { enumerable: true, get: function () { return types_1.isValidCodexProfileName; } });
// Re-export for convenience in command modules
var time_1 = require("../../utils/time");
Object.defineProperty(exports, "formatRelativeTime", { enumerable: true, get: function () { return time_1.formatRelativeTime; } });
exports.getProfileNameError = types_1.getCodexProfileNameError;
// ── Arg parsing ───────────────────────────────────────────────────────────────
function parseArgs(args) {
    const result = { unknownFlags: [], seenOptions: [] };
    const positional = [];
    const markSeen = (flag) => result.seenOptions?.push(flag);
    for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (arg === '--yes' || arg === '-y') {
            markSeen('--yes');
            result.yes = true;
        }
        else if (arg === '--json') {
            markSeen('--json');
            result.json = true;
        }
        else if (arg === '--force') {
            markSeen('--force');
            result.force = true;
        }
        else if (arg === '--shell') {
            markSeen('--shell');
            result.shell = args[++i] ?? '';
        }
        else if (arg.startsWith('--shell=')) {
            markSeen('--shell');
            result.shell = arg.slice('--shell='.length);
        }
        else if (arg.startsWith('-') && arg !== '--') {
            if (result.unknownFlags)
                result.unknownFlags.push(arg);
        }
        else if (arg !== '--') {
            positional.push(arg);
        }
    }
    if (positional.length > 0) {
        result.profileName = positional[0];
    }
    if (positional.length > 1) {
        result.extraPositionals = positional.slice(1);
    }
    return result;
}
exports.parseArgs = parseArgs;
function rejectUnsupportedOptions(parsed, usage, allowed = {}) {
    const unsupported = new Set(parsed.unknownFlags ?? []);
    const seen = new Set(parsed.seenOptions ?? []);
    if (seen.has('--yes') && !allowed.yes)
        unsupported.add('--yes');
    if (seen.has('--json') && !allowed.json)
        unsupported.add('--json');
    if (seen.has('--force') && !allowed.force)
        unsupported.add('--force');
    if (seen.has('--shell') && !allowed.shell)
        unsupported.add('--shell');
    const extraPositionals = parsed.extraPositionals ?? [];
    if (unsupported.size > 0 || extraPositionals.length > 0) {
        const flags = [...unsupported].join(', ');
        process.stderr.write(`Usage: ${(0, ui_1.color)(usage, 'command')}\n`);
        const details = [
            flags ? `Unknown options: ${flags}` : null,
            extraPositionals.length > 0
                ? `Unexpected arguments: ${extraPositionals.map((arg) => `"${arg}"`).join(', ')}`
                : null,
        ].filter(Boolean);
        (0, errors_1.exitWithError)(details.join('; '), exit_codes_1.ExitCode.GENERAL_ERROR);
    }
}
exports.rejectUnsupportedOptions = rejectUnsupportedOptions;
//# sourceMappingURL=types.js.map