"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimingCategories = exports.CliUserConfigValidator = exports.CliUserConfig = exports.SnippetShellUserConfigValidator = exports.SnippetShellUserConfig = exports.ShellUserConfigValidator = exports.ShellUserConfig = void 0;
const path_1 = __importDefault(require("path"));
class ShellUserConfig {
    constructor() {
        this.displayBatchSize = 20;
        this.maxTimeMS = null;
        this.enableTelemetry = false;
        this.editor = null;
        this.disableSchemaSampling = false;
    }
}
exports.ShellUserConfig = ShellUserConfig;
class ShellUserConfigValidator {
    static async validate(key, value) {
        switch (key) {
            case 'displayBatchSize':
                if (typeof value !== 'number' || value <= 0) {
                    return `${key} must be a positive integer`;
                }
                return null;
            case 'maxTimeMS':
                if (value !== null && (typeof value !== 'number' || value <= 0)) {
                    return `${key} must be null or a positive integer`;
                }
                return null;
            case 'disableSchemaSampling':
            case 'enableTelemetry':
                if (typeof value !== 'boolean') {
                    return `${key} must be a boolean`;
                }
                return null;
            case 'editor':
                if (typeof value !== 'string' && value !== null) {
                    return `${key} must be a string or null`;
                }
                return null;
            default:
                return `${key} is not a known config option`;
        }
    }
}
exports.ShellUserConfigValidator = ShellUserConfigValidator;
class SnippetShellUserConfig extends ShellUserConfig {
    constructor() {
        super(...arguments);
        this.snippetIndexSourceURLs = 'https://compass.mongodb.com/mongosh/snippets-index.bson.br';
        this.snippetRegistryURL = 'https://registry.npmjs.org';
        this.snippetAutoload = true;
    }
}
exports.SnippetShellUserConfig = SnippetShellUserConfig;
class SnippetShellUserConfigValidator extends ShellUserConfigValidator {
    static async validate(key, value) {
        switch (key) {
            case 'snippetIndexSourceURLs':
                if (typeof value !== 'string' ||
                    value.split(';').some((url) => url && !isValidUrl(url))) {
                    return `${key} must be a ;-separated list of valid URLs`;
                }
                return null;
            case 'snippetRegistryURL':
                if (typeof value !== 'string' || !isValidUrl(value)) {
                    return `${key} must be a valid URL`;
                }
                return null;
            case 'snippetAutoload':
                if (typeof value !== 'boolean') {
                    return `${key} must be a boolean`;
                }
                return null;
            default:
                return super.validate(key, value);
        }
    }
}
exports.SnippetShellUserConfigValidator = SnippetShellUserConfigValidator;
class CliUserConfig extends SnippetShellUserConfig {
    constructor() {
        super(...arguments);
        this.userId = '';
        this.telemetryAnonymousId = '';
        this.disableGreetingMessage = false;
        this.forceDisableTelemetry = false;
        this.inspectCompact = 3;
        this.inspectDepth = 6;
        this.historyLength = 1000;
        this.showStackTraces = false;
        this.redactHistory = 'remove';
        this.oidcRedirectURI = undefined;
        this.oidcTrustedEndpoints = undefined;
        this.browser = undefined;
        this.updateURL = 'https://downloads.mongodb.com/compass/mongosh.json';
        this.disableLogging = false;
        this.logLocation = undefined;
        this.logRetentionDays = 30;
        this.logMaxFileCount = 100;
        this.logCompressionEnabled = false;
        this.logRetentionGB = undefined;
    }
}
exports.CliUserConfig = CliUserConfig;
class CliUserConfigValidator extends SnippetShellUserConfigValidator {
    static async validate(key, value) {
        switch (key) {
            case 'userId':
            case 'telemetryAnonymousId':
            case 'disableGreetingMessage':
                return null;
            case 'inspectCompact':
                if (typeof value !== 'boolean' &&
                    (typeof value !== 'number' || value < 0)) {
                    return `${key} must be a boolean or a positive integer`;
                }
                return null;
            case 'inspectDepth':
            case 'historyLength':
            case 'logRetentionDays':
            case 'logMaxFileCount':
                if (typeof value !== 'number' || value < 0) {
                    return `${key} must be a positive integer`;
                }
                return null;
            case 'logRetentionGB':
                if (value !== undefined && (typeof value !== 'number' || value < 0)) {
                    return `${key} must be a positive number or undefined`;
                }
                return null;
            case 'disableLogging':
            case 'forceDisableTelemetry':
            case 'showStackTraces':
            case 'logCompressionEnabled':
                if (typeof value !== 'boolean') {
                    return `${key} must be a boolean`;
                }
                return null;
            case 'redactHistory':
                if (value !== 'keep' &&
                    value !== 'remove' &&
                    value !== 'remove-redact') {
                    return `${key} must be one of 'keep', 'remove', or 'remove-redact'`;
                }
                return null;
            case 'oidcRedirectURI':
                if (value !== undefined &&
                    (typeof value !== 'string' || !isValidUrl(value))) {
                    return `${key} must be undefined or a valid URL`;
                }
                return null;
            case 'oidcTrustedEndpoints':
                if (value !== undefined &&
                    (!Array.isArray(value) || value.some((v) => typeof v !== 'string'))) {
                    return `${key} must be undefined or an array of hostnames`;
                }
                return null;
            case 'browser':
                if (value !== undefined &&
                    value !== false &&
                    typeof value !== 'string') {
                    return `${key} must be undefined, false, or a command string`;
                }
                return null;
            case 'updateURL':
                if (typeof value !== 'string' || (value.trim() && !isValidUrl(value))) {
                    return `${key} must be a valid URL or empty`;
                }
                return null;
            case 'logLocation':
                if (value !== undefined &&
                    (typeof value !== 'string' || !path_1.default.isAbsolute(value))) {
                    return `${key} must be a valid absolute path or undefined`;
                }
                return null;
            default:
                return super.validate(key, value);
        }
    }
}
exports.CliUserConfigValidator = CliUserConfigValidator;
function isValidUrl(url) {
    if (typeof URL === 'function') {
        try {
            new URL(url);
            return true;
        }
        catch {
            return false;
        }
    }
    return true;
}
exports.TimingCategories = {
    REPLInstantiation: 'REPLInstantiation',
    UserConfigLoading: 'UserConfigLoading',
    DriverSetup: 'DriverSetup',
    Logging: 'Logging',
    SnippetLoading: 'SnippetLoading',
    Snapshot: 'Snapshot',
    ResourceFileLoading: 'ResourceFileLoading',
    AsyncRewrite: 'AsyncRewrite',
    Eval: 'Eval',
    EvalFile: 'EvalFile',
    Telemetry: 'Telemetry',
    Main: 'Main',
};
//# sourceMappingURL=index.js.map