"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliOptionsSchema = exports.UnsupportedCliOptions = exports.DeprecatedCliOptions = exports.CurrentCliOptionsSchema = void 0;
exports.processPositionalCliOptions = processPositionalCliOptions;
exports.validateCliOptions = validateCliOptions;
const v4_1 = __importDefault(require("zod/v4"));
const arg_metadata_1 = require("./arg-metadata");
const errors_1 = require("@mongosh/errors");
exports.CurrentCliOptionsSchema = v4_1.default.object({
    apiVersion: v4_1.default.string().optional(),
    authenticationDatabase: v4_1.default.string().optional(),
    authenticationMechanism: v4_1.default.string().optional(),
    awsAccessKeyId: v4_1.default.string().optional(),
    awsIamSessionToken: v4_1.default.string().optional(),
    awsSecretAccessKey: v4_1.default.string().optional(),
    awsSessionToken: v4_1.default.string().optional(),
    csfleLibraryPath: v4_1.default.string().optional(),
    cryptSharedLibPath: v4_1.default.string().optional(),
    deepInspect: v4_1.default.boolean().default(true).optional(),
    db: v4_1.default.string().optional(),
    gssapiServiceName: v4_1.default.string().optional(),
    sspiHostnameCanonicalization: v4_1.default.string().optional(),
    sspiRealmOverride: v4_1.default.string().optional(),
    jsContext: v4_1.default.enum(['repl', 'plain-vm', 'auto']).optional(),
    host: v4_1.default.string().optional(),
    keyVaultNamespace: v4_1.default.string().optional(),
    kmsURL: v4_1.default.string().optional(),
    locale: v4_1.default.string().optional(),
    oidcFlows: v4_1.default.string().optional(),
    oidcRedirectUri: v4_1.default
        .string()
        .optional()
        .register(arg_metadata_1.argMetadata, {
        alias: ['oidcRedirectUrl'],
    }),
    password: v4_1.default
        .string()
        .optional()
        .register(arg_metadata_1.argMetadata, { alias: ['p'] }),
    port: v4_1.default.string().optional(),
    username: v4_1.default
        .string()
        .optional()
        .register(arg_metadata_1.argMetadata, { alias: ['u'] }),
    tlsCAFile: v4_1.default.string().optional(),
    tlsCertificateKeyFile: v4_1.default.string().optional(),
    tlsCertificateKeyFilePassword: v4_1.default.string().optional(),
    tlsCertificateSelector: v4_1.default.string().optional(),
    tlsCRLFile: v4_1.default.string().optional(),
    tlsDisabledProtocols: v4_1.default.string().optional(),
    apiDeprecationErrors: v4_1.default.boolean().optional(),
    apiStrict: v4_1.default.boolean().optional(),
    buildInfo: v4_1.default
        .boolean()
        .optional()
        .register(arg_metadata_1.argMetadata, { alias: ['build-info'] }),
    exposeAsyncRewriter: v4_1.default.boolean().optional(),
    help: v4_1.default
        .boolean()
        .optional()
        .register(arg_metadata_1.argMetadata, { alias: ['h'] }),
    ipv6: v4_1.default.boolean().optional(),
    nodb: v4_1.default.boolean().optional(),
    norc: v4_1.default.boolean().optional(),
    oidcTrustedEndpoint: v4_1.default.boolean().optional(),
    oidcIdTokenAsAccessToken: v4_1.default
        .boolean()
        .optional()
        .register(arg_metadata_1.argMetadata, {
        alias: ['oidcIDTokenAsAccessToken'],
    }),
    oidcNoNonce: v4_1.default.boolean().optional(),
    quiet: v4_1.default.boolean().optional(),
    retryWrites: v4_1.default.boolean().optional(),
    shell: v4_1.default.boolean().optional(),
    skipStartupWarnings: v4_1.default.boolean().optional(),
    verbose: v4_1.default.boolean().optional(),
    version: v4_1.default.boolean().optional(),
    smokeTests: v4_1.default.boolean().optional(),
    perfTests: v4_1.default.boolean().optional(),
    tls: v4_1.default.boolean().optional(),
    tlsAllowInvalidCertificates: v4_1.default.boolean().optional(),
    tlsAllowInvalidHostnames: v4_1.default.boolean().optional(),
    tlsFIPSMode: v4_1.default.boolean().optional(),
    tlsUseSystemCA: v4_1.default.boolean().optional(),
    eval: v4_1.default.array(v4_1.default.string()).optional(),
    file: v4_1.default
        .array(v4_1.default.string())
        .optional()
        .register(arg_metadata_1.argMetadata, { alias: ['f'] }),
    json: v4_1.default.union([v4_1.default.boolean(), v4_1.default.enum(['relaxed', 'canonical'])]).optional(),
    oidcDumpTokens: v4_1.default
        .union([v4_1.default.boolean(), v4_1.default.enum(['redacted', 'include-secrets'])])
        .optional(),
    browser: v4_1.default.union([v4_1.default.literal(false), v4_1.default.string()]).optional(),
});
exports.DeprecatedCliOptions = v4_1.default.object({
    ssl: v4_1.default
        .boolean()
        .optional()
        .register(arg_metadata_1.argMetadata, { deprecationReplacement: 'tls' }),
    sslAllowInvalidCertificates: v4_1.default.boolean().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsAllowInvalidCertificates',
    }),
    sslAllowInvalidHostnames: v4_1.default.boolean().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsAllowInvalidHostnames',
    }),
    sslPEMKeyFile: v4_1.default.string().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsCertificateKeyFile',
    }),
    sslPEMKeyPassword: v4_1.default.string().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsCertificateKeyFilePassword',
    }),
    sslCAFile: v4_1.default.string().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsCAFile',
    }),
    sslCertificateSelector: v4_1.default.string().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsCertificateSelector',
    }),
    sslCRLFile: v4_1.default.string().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsCRLFile',
    }),
    sslDisabledProtocols: v4_1.default.string().optional().register(arg_metadata_1.argMetadata, {
        deprecationReplacement: 'tlsDisabledProtocols',
    }),
});
exports.UnsupportedCliOptions = v4_1.default.object({
    gssapiHostName: v4_1.default
        .string()
        .optional()
        .register(arg_metadata_1.argMetadata, { unsupported: true }),
    sslFIPSMode: v4_1.default.boolean().optional().register(arg_metadata_1.argMetadata, {
        unsupported: true,
    }),
});
exports.CliOptionsSchema = v4_1.default.object({
    ...exports.CurrentCliOptionsSchema.shape,
    ...exports.DeprecatedCliOptions.shape,
    ...exports.UnsupportedCliOptions.shape,
});
function processPositionalCliOptions({ parsed, positional, }) {
    const processed = { ...parsed };
    if (typeof positional[0] === 'string') {
        if (!processed.nodb && isConnectionSpecifier(positional[0])) {
            processed.connectionSpecifier = positional.shift();
        }
    }
    processed.fileNames = [
        ...(processed.file ?? []),
        ...positional,
    ];
    return processed;
}
function validateCliOptions(parsed) {
    const jsonValidation = exports.CliOptionsSchema.shape.json.safeParse(parsed.json);
    if (!jsonValidation.success) {
        throw new errors_1.MongoshUnimplementedError('--json can only have the values relaxed, canonical', errors_1.CommonErrors.InvalidArgument);
    }
    const oidcDumpTokensValidation = exports.CliOptionsSchema.shape.oidcDumpTokens.safeParse(parsed.oidcDumpTokens);
    if (!oidcDumpTokensValidation.success) {
        throw new errors_1.MongoshUnimplementedError('--oidcDumpTokens can only have the values redacted, include-secrets', errors_1.CommonErrors.InvalidArgument);
    }
    const jsContextValidation = exports.CliOptionsSchema.shape.jsContext.safeParse(parsed.jsContext);
    if (!jsContextValidation.success) {
        throw new errors_1.MongoshUnimplementedError('--jsContext can only have the values repl, plain-vm, auto', errors_1.CommonErrors.InvalidArgument);
    }
    const browserValidation = exports.CliOptionsSchema.shape.browser.safeParse(parsed.browser);
    if (!browserValidation.success) {
        throw new errors_1.MongoshUnimplementedError('--browser can only be true or a string', errors_1.CommonErrors.InvalidArgument);
    }
}
function isConnectionSpecifier(arg) {
    return (typeof arg === 'string' &&
        (arg.startsWith('mongodb://') ||
            arg.startsWith('mongodb+srv://') ||
            !(arg.endsWith('.js') || arg.endsWith('.mongodb'))));
}
//# sourceMappingURL=cli-options.js.map