"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BootstrapperForCodiacCli = void 0;
require("reflect-metadata");
const path = require("path");
const guid_typescript_1 = require("guid-typescript");
const codiac_common_1 = require("@codiac.io/codiac-common");
const IBetterLogger_1 = require("@codiac.io/codiac-common/IBetterLogger");
const better_logger_1 = require("@codiac.io/codiac-common/better-logger");
const cli_tools_1 = require("@codiac.io/cli-tools");
const codiac_relay_contracts_1 = require("@codiac.io/codiac-relay-contracts");
const codiac_ops_common_1 = require("@codiac.io/codiac-ops-common");
const ops_1 = require("./ops");
const entities_1 = require("./ops/entities");
const data_1 = require("./ops/data");
const built_in_cli_user_input_1 = require("./cli-common/built-in-cli-user-input");
const INTERFACES_CLI_1 = require("./ops/INTERFACES_CLI");
const codiac_constants_1 = require("./codiac-constants");
const relay_container_1 = require("./relay/relay-container");
const log4js_logger_1 = require("./log4js-logger");
class BootstrapperForCodiacCli {
    /** (IDEMPOTENT) Initializes ioc (mutates it) with typical cli/codiac project entities. */
    static bootstrap(ioc, oclifConfig, requiresProjectScope) {
        if (this.isBootstrapped != true) {
            process.removeAllListeners('warning');
            const localProgramFolder = path.normalize(codiac_ops_common_1.OpsUtils.resolveHomeDir(path.posix.join("~", codiac_constants_1.CliConstants.LOCAL_CONFIG_FOLDER_NAME)));
            ioc.bind(entities_1.CliEnviro).toDynamicValue(() => {
                return new entities_1.CliEnviro(oclifConfig.configDir, localProgramFolder, oclifConfig.version, relay_container_1.DEFAULT_IMAGE_TAG);
            }).inSingletonScope();
            ioc.bind(data_1.CliGlobalConfigRepo).toSelf(); // (() => new CliContextRepo(this.config.configDir, this._projectRoot));
            ioc.bind(entities_1.CliConfig).toDynamicValue(ctxt => {
                let cfgRepo = ctxt.container.get(data_1.CliGlobalConfigRepo);
                return cfgRepo.read_sync() || new entities_1.CliConfig();
            }); // .inSingletonScope();
            // PROJECT SCOPE (all nullables)
            ioc.bind(entities_1.ProjectEnviro).toDynamicValue(() => {
                let projRoot = ops_1.CliPathWalker.getProjectRoot(undefined, requiresProjectScope);
                return (projRoot === "") ? undefined : new entities_1.ProjectEnviro(projRoot);
            }).inSingletonScope();
            ioc.bind(data_1.ProjectContextRepo).toDynamicValue(ctx => {
                let projEnv = ctx.container.get(entities_1.ProjectEnviro);
                return projEnv ? new data_1.ProjectContextRepo(projEnv) : undefined;
            }); // toSelf();  // (() => new CliContextRepo(this.config.configDir, this._projectRoot));
            ioc.bind(codiac_relay_contracts_1.ProjectConfig).toDynamicValue(ctxt => {
                let cfgRepo = ctxt.container.get(data_1.ProjectContextRepo);
                return cfgRepo === null || cfgRepo === void 0 ? void 0 : cfgRepo.readProjectConfig();
            }); // .inSingletonScope();
            ioc.bind(codiac_relay_contracts_1.ProjectUserConfig).toDynamicValue(ctxt => {
                let cfgRepo = ctxt.container.get(data_1.ProjectContextRepo);
                return cfgRepo === null || cfgRepo === void 0 ? void 0 : cfgRepo.readProjectUserConfig();
            }); // .inSingletonScope();
            ioc.bind(entities_1.OperationMeta).toDynamicValue(ctxt => {
                return {
                    correlationId: guid_typescript_1.Guid.create().toString()
                };
            }).inSingletonScope(); // <-- singleton for CLI ONLY!!!
            ioc.bind(INTERFACES_CLI_1.INTERFACES_CLI.IUserInputProvider).to(built_in_cli_user_input_1.BuiltInCliUserInput);
            ioc.bind(INTERFACES_CLI_1.INTERFACES_CLI.IOutputStyler).to(cli_tools_1.TtyOutputStyler);
            ioc.bind(IBetterLogger_1.LoggerSettings).toDynamicValue(ctxt => {
                // INITIALIZE LOGGER
                let settings = new IBetterLogger_1.LoggerSettings([IBetterLogger_1.LogLevel.notice, IBetterLogger_1.LogLevel.warn, IBetterLogger_1.LogLevel.error, IBetterLogger_1.LogLevel.fatal]);
                const cliCfg = ctxt.container.get(entities_1.CliConfig);
                if (cliCfg.verbosity) {
                    switch (cliCfg.verbosity) {
                        case entities_1.Verbosity.detail:
                            settings.LogLevelsToShow.push(IBetterLogger_1.LogLevel.detail);
                            break;
                        case entities_1.Verbosity.debug:
                            settings.LogLevelsToShow.push(IBetterLogger_1.LogLevel.detail);
                            settings.LogLevelsToShow.push(IBetterLogger_1.LogLevel.debug);
                            settings.LogLevelsToShow.push(IBetterLogger_1.LogLevel.trace);
                            break;
                    }
                }
                return settings;
            }).inSingletonScope();
            // ioc.bind<LoggerSettings>(LoggerSettings).toConstantValue(settings);
            if (ioc.isBound(codiac_common_1.INTERFACES.IBetterLogger) !== true) {
                ioc.bind(codiac_common_1.INTERFACES.IBetterLogger).to(better_logger_1.BetterConsoleLogger);
            }
            // TODO: move this to BootstrapperForCodiacCli
            log4js_logger_1.Log4jsLogger.bootstrap(ioc, {
                appenders: {
                    "console": { type: "console" },
                    "applog": { type: "file", maxLogSize: "2M", backups: 3, compress: false, keepFileExt: true, filename: path.posix.join(localProgramFolder, "logs", "cli.log") },
                    "errlog": { type: "file", maxLogSize: "2M", backups: 3, compress: false, keepFileExt: true, filename: path.posix.join(localProgramFolder, "logs", "cli-error.log") }
                },
                categories: {
                    default: { appenders: ["applog"], level: "trace" },
                    errors: { appenders: ["errlog"], level: "error" },
                    console: { appenders: ["console"], level: "off" }
                }
            });
            codiac_ops_common_1.OpsCommon.bootstrap(ioc);
            this.isBootstrapped = true;
        }
    }
}
exports.BootstrapperForCodiacCli = BootstrapperForCodiacCli;
BootstrapperForCodiacCli.isBootstrapped = false;
//# sourceMappingURL=bootstrapper-for-codiac-cli.js.map