"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const diagnostics_1 = require("./diagnostics");
function getVersion() {
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const { version } = require('../../../package.json');
    return `v${version}`;
}
function validateLogLevel(logLevelInput) {
    return (typeof logLevelInput === 'string' &&
        diagnostics_1.logLevels.includes(logLevelInput));
}
;
(() => __awaiter(void 0, void 0, void 0, function* () {
    const program = new commander_1.Command();
    program.name('vti').description('Vetur Terminal Interface').version(getVersion());
    program
        .command('diagnostics [workspace]')
        .description('Print all diagnostics')
        .addOption(new commander_1.Option('-c, --checker-config <checkerConfig>', 'Option overrides to pass to VTI').default('{}'))
        .addOption(new commander_1.Option('-l, --log-level <logLevel>', 'Log level to print')
        .default('WARN')
        // logLevels is readonly array but .choices need read-write array (because of weak typing)
        .choices(diagnostics_1.logLevels))
        .action((workspace, options) => __awaiter(void 0, void 0, void 0, function* () {
        const logLevelOption = options.logLevel;
        if (!validateLogLevel(logLevelOption)) {
            throw new Error(`Invalid log level: ${logLevelOption}`);
        }
        let parsedConfig;
        try {
            parsedConfig = JSON.parse(options.checkerConfig);
        }
        catch (_a) {
            throw new Error(`Unable to parse checker-config JSON: ${options.checkerConfig}`);
        }
        yield (0, diagnostics_1.diagnostics)(workspace, logLevelOption, {
            watch: false,
            verbose: false,
            config: parsedConfig,
        });
    }));
    program.parse(process.argv);
}))().catch((err) => {
    console.error(`VTI operation failed with error`);
    console.error(err.stack);
    process.exit(1);
});
