"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.doValidate = doValidate;
exports.validateCommand = validateCommand;
const chalk_1 = __importDefault(require("chalk"));
const dedent_1 = __importDefault(require("dedent"));
const zod_validation_error_1 = require("zod-validation-error");
const logger_1 = __importDefault(require("../logger"));
const telemetry_1 = __importDefault(require("../telemetry"));
const types_1 = require("../types");
const util_1 = require("../util");
const load_1 = require("../util/config/load");
async function doValidate(opts, defaultConfig, defaultConfigPath) {
    (0, util_1.setupEnv)(opts.envPath);
    const configPaths = opts.config || (defaultConfigPath ? [defaultConfigPath] : undefined);
    try {
        const { config, testSuite } = await (0, load_1.resolveConfigs)({ ...opts, config: configPaths }, defaultConfig);
        const configParse = types_1.UnifiedConfigSchema.safeParse(config);
        if (!configParse.success) {
            logger_1.default.error((0, dedent_1.default) `Configuration validation error:
Config file path(s): ${Array.isArray(configPaths) ? configPaths.join(', ') : (configPaths ?? 'N/A')}
${(0, zod_validation_error_1.fromError)(configParse.error).message}`);
            process.exitCode = 1;
            return;
        }
        const suiteParse = types_1.TestSuiteSchema.safeParse(testSuite);
        if (!suiteParse.success) {
            logger_1.default.error((0, dedent_1.default) `Test suite validation error:\n${(0, zod_validation_error_1.fromError)(suiteParse.error).message}`);
            process.exitCode = 1;
            return;
        }
        logger_1.default.info(chalk_1.default.green('Configuration is valid.'));
    }
    catch (err) {
        logger_1.default.error(`Failed to validate configuration: ${err instanceof Error ? err.message : err}`);
        process.exitCode = 1;
    }
}
function validateCommand(program, defaultConfig, defaultConfigPath) {
    program
        .command('validate')
        .description('Validate a trusys configuration file')
        .option('-c, --config <paths...>', 'Path to configuration file. Automatically loads trusysconfig.yaml')
        .action(async (opts) => {
        telemetry_1.default.record('command_used', { name: 'validate' });
        await telemetry_1.default.send();
        await doValidate(opts, defaultConfig, defaultConfigPath);
    });
}
//# sourceMappingURL=validate.js.map