"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
require("./bootstrap");
const os_1 = require("os");
const shelljs = require("shelljs");
shelljs.config.silent = true;
shelljs.config.fatal = true;
const errors_1 = require("./common/errors");
const helpers_1 = require("./common/helpers");
errors_1.installUncaughtExceptionListener(process.exit.bind(process, 120));
const logger = $injector.resolve("logger");
const originalProcessOn = process.on;
process.on = (event, listener) => {
    if (event === "SIGINT") {
        logger.trace(new Error(`Trying to handle SIGINT event. CLI overrides this behavior and does not allow handling SIGINT as this causes issues with Ctrl + C in terminal`).stack);
    }
    else {
        return originalProcessOn.apply(process, [event, listener]);
    }
};
(() => __awaiter(this, void 0, void 0, function* () {
    const config = $injector.resolve("$config");
    const err = $injector.resolve("$errors");
    err.printCallStack = config.DEBUG;
    const extensibilityService = $injector.resolve("extensibilityService");
    try {
        yield helpers_1.settlePromises(extensibilityService.loadExtensions());
    }
    catch (err) {
        logger.trace("Unable to load extensions. Error is: ", err);
    }
    const $sysInfo = $injector.resolve("sysInfo");
    const macOSWarning = yield $sysInfo.getMacOSWarningMessage();
    if (macOSWarning) {
        const message = `${os_1.EOL}${macOSWarning.message}${os_1.EOL}`;
        if (macOSWarning.severity === "high") {
            logger.printOnStderr(message.red.bold);
        }
        else {
            logger.warn(message);
        }
    }
    const commandDispatcher = $injector.resolve("commandDispatcher");
    const messages = $injector.resolve("$messagesService");
    messages.pathsToMessageJsonFiles = [];
    if (process.argv[2] === "completion") {
        yield commandDispatcher.completeCommand();
    }
    else {
        yield commandDispatcher.dispatchCommand();
    }
    $injector.dispose();
}))();
