"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 });
exports.originalProcessOn = void 0;
require("./bootstrap");
const shelljs = require("shelljs");
shelljs.config.silent = true;
shelljs.config.fatal = true;
const errors_1 = require("./common/errors");
const helpers_1 = require("./common/helpers");
const yok_1 = require("./common/yok");
(0, errors_1.installUncaughtExceptionListener)(process.exit.bind(process, 120));
const logger = yok_1.injector.resolve("logger");
exports.originalProcessOn = process.on.bind(process);
process.on = (event, listener) => {
    if (event === "SIGINT") {
        logger.trace(`Trying to handle SIGINT event. CLI overrides this behavior and does not allow handling SIGINT as this causes issues with Ctrl + C in terminal.`);
        const msg = "The stackTrace of the location trying to handle SIGINT is:";
        const stackTrace = new Error(msg).stack || "";
        logger.trace(stackTrace.replace(`Error: ${msg}`, msg));
    }
    else {
        return (0, exports.originalProcessOn)(event, listener);
    }
};
(() => __awaiter(void 0, void 0, void 0, function* () {
    const config = yok_1.injector.resolve("$config");
    const err = yok_1.injector.resolve("$errors");
    err.printCallStack = config.DEBUG;
    const $initializeService = yok_1.injector.resolve("initializeService");
    yield $initializeService.initialize();
    const extensibilityService = yok_1.injector.resolve("extensibilityService");
    try {
        yield (0, helpers_1.settlePromises)(extensibilityService.loadExtensions());
    }
    catch (err) {
        logger.trace("Unable to load extensions. Error is: ", err);
    }
    const commandDispatcher = yok_1.injector.resolve("commandDispatcher");
    const messages = yok_1.injector.resolve("$messagesService");
    messages.pathsToMessageJsonFiles = [];
    if (process.argv[2] === "completion") {
        yield commandDispatcher.completeCommand();
    }
    else {
        yield commandDispatcher.dispatchCommand();
    }
    yok_1.injector.dispose();
}))();
