"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BootstrapperForEnterpriseCommands = void 0;
require("reflect-metadata");
const codiac_common_1 = require("@codiac.io/codiac-common");
const relay_container_1 = require("./relay/relay-container");
const codiac_api_1 = require("./apis/codiac-api");
const codiac_relay_1 = require("./apis/codiac-relay");
const uilogic_1 = require("./uilogic");
const entities_1 = require("./ops/entities");
const relay_websocket_client_1 = require("./relay/wsclient/relay-websocket-client");
const lrp_watcher_predef_1 = require("./uilogic/lrp-watcher-predef");
const rootmap_ui_utils_1 = require("./uilogic/rootmap-ui-utils");
const INTERFACES_CLI_1 = require("./ops/INTERFACES_CLI");
const nats_client_1 = require("./apis/nats/nats-client");
const api_client_plus_local_1 = require("./apis/api-plus-local/api-client-plus-local");
const type_safety_utils_1 = require("./uilogic/type-safety-utils");
const codiac_api_nats_1 = require("./apis/codiac-api-nats");
const bootstrapper_for_codiac_cli_1 = require("./bootstrapper-for-codiac-cli");
class BootstrapperForEnterpriseCommands {
    static bootstrap(ioc, cmd) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        if (this.isBootstrapped == true) {
            return;
        }
        cmd.requiresProjectScope = false;
        bootstrapper_for_codiac_cli_1.BootstrapperForCodiacCli.bootstrap(ioc, cmd.config, cmd.requiresProjectScope);
        // -----------------------------------------------------------------------------------------------------------
        // CODIAC API CLIENT
        // -----------------------------------------------------------------------------------------------------------
        // REST VERSION
        codiac_api_1.CodiacRestClient.bootstrap(ioc);
        ioc.bind(codiac_api_1.CodiacRestClient).toDynamicValue(() => { var _a; return codiac_api_1.CodiacRestClient.create(cmd.logger, cmd.codiacApiUrl, (_a = cmd._userCntx) === null || _a === void 0 ? void 0 : _a.currentToken); }); // 
        // NATS VERSION
        const givenNatsUrl = (_b = (_a = cmd.localConfig) === null || _a === void 0 ? void 0 : _a.apiNatsUrl) !== null && _b !== void 0 ? _b : cmd._cliCfg.codiacNatsUrl;
        codiac_api_nats_1.CodiacNatsClient.bootstrap(ioc, givenNatsUrl ? { baseUrl: givenNatsUrl } : undefined);
        ioc.bind(codiac_api_nats_1.CodiacNatsClient).toDynamicValue((ctx) => { var _a; return codiac_api_nats_1.CodiacNatsClient.create(cmd.logger, ctx.container.get(codiac_api_nats_1.CodiacNatsClientConfig), (_a = cmd._userCntx) === null || _a === void 0 ? void 0 : _a.currentToken); });
        let apiClientKind = (_e = (_d = (_c = cmd.localConfig) === null || _c === void 0 ? void 0 : _c.apiClientKind) !== null && _d !== void 0 ? _d : cmd._cliCfg.apiClientKind) !== null && _e !== void 0 ? _e : entities_1.ApiClientKindEnum.rest;
        // Cant do it this way until we make ApiUserCreds.token nullable.
        // ioc.bind<CodiacClientConfig>(CodiacClientConfig).toConstantValue({ apiBaseUrl: this.codiacApiUrl });
        // ioc.bind(ApiUserCreds).toConstantValue(undefined);  // <--- ILLEGAL!
        // host.ioc.bind<CodiacRestClient>(CodiacRestClient).toSelf();
        // IMPORTANT: Read token from user context at injection time, 
        // NOT at bootstrap time, 
        // so that a freshly authenticated context gets passed into the api client.
        // let authToken = (cmd._userCntx) ? cmd._userCntx.currentToken : undefined;  
        if (apiClientKind == entities_1.ApiClientKindEnum.nats) {
            ioc.bind(uilogic_1.INTERFACES_CLI_ENTERPRISE.ICodiacClient)
                .toDynamicValue(ctx => ctx.container.get(codiac_api_nats_1.CodiacNatsClient));
        }
        else {
            const localEntities = type_safety_utils_1.TypeSafetyUtils.toBoolean(/* TEMP HACK!! --> update this class... */ (_g = (_f = cmd.localConfig) === null || _f === void 0 ? void 0 : _f.localEntities) !== null && _g !== void 0 ? _g : (_h = cmd._cliCfg) === null || _h === void 0 ? void 0 : _h.localEntities);
            if (localEntities) {
                const LOCAL_DATA_ROUTE_PREFIX = "/local"; // <--- Must match this same property of the CodiacRelayApi class in the relay.
                ioc.bind(api_client_plus_local_1.ApiClientPlusLocalConfig).toConstantValue({ localDataRoutePrefix: LOCAL_DATA_ROUTE_PREFIX });
                ioc.bind(uilogic_1.INTERFACES_CLI_ENTERPRISE.ICodiacClient)
                    .toDynamicValue(() => { var _a; return api_client_plus_local_1.ApiClientPlusLocal.createPlus(cmd.genRelayClient, cmd.logger, LOCAL_DATA_ROUTE_PREFIX, cmd.codiacApiUrl, (_a = cmd._userCntx) === null || _a === void 0 ? void 0 : _a.currentToken); }); // 
            }
            else {
                ioc.bind(uilogic_1.INTERFACES_CLI_ENTERPRISE.ICodiacClient)
                    .toDynamicValue(ctx => ctx.container.get(codiac_api_1.CodiacRestClient)); // use the codiac rest client
            }
        }
        // -----------------------------------------------------------------------------------------------------------
        // -----------------------------------------------------------------------------------------------------------
        // AUTH CLIENT
        // -----------------------------------------------------------------------------------------------------------
        ioc.bind(uilogic_1.INTERFACES_CLI_ENTERPRISE.IAuthClient)
            .toDynamicValue(ctx => ctx.container.get(codiac_api_1.CodiacRestClient)); // use the codiac rest client
        // -----------------------------------------------------------------------------------------------------------
        codiac_relay_1.RelayClient.bootstrap();
        ioc.bind(codiac_relay_1.RelayClient).toDynamicValue(() => { var _a; return codiac_relay_1.RelayClient.create(cmd.logger, undefined, (_a = cmd._userCntx) === null || _a === void 0 ? void 0 : _a.currentToken); });
        // ioc.bind(ThrowingCodiacClient).toSelf();
        // ioc.bind<UserContextLoader>(UserContextLoader).toSelf();
        // ioc.bind<UserContext>(UserContext).toDynamicValue(ctx => {
        //   let logger = ctx.container.get<IBetterLogger>(INTERFACES.IBetterLogger);
        //   let projCfg = ctx.container.get<ProjectConfig>(ProjectConfig);
        //   let userLoader = new UserContextLoader(logger);
        //   let userCntx = userLoader.loadUserContext(projCfg?.tenantCode) ?? new UserContext();
        //   return userCntx;
        // })
        ioc.bind(uilogic_1.INTERFACES_CLI_ENTERPRISE.IContainerManager).toDynamicValue(ctx => {
            var _a, _b;
            let relayCtrSettings = {
                healthCheckUrl: undefined,
                retryTimeout: undefined
            };
            let relayContainer = new relay_container_1.RelayContainer(ctx.container.get(INTERFACES_CLI_1.INTERFACES_CLI.IOutputStyler), ctx.container.get(entities_1.CliEnviro), ctx.container.get(codiac_common_1.INTERFACES.IBetterLogger), relayCtrSettings.healthCheckUrl, relayCtrSettings.retryTimeout, (_a = ctx.container.get(entities_1.ProjectEnviro)) === null || _a === void 0 ? void 0 : _a.projectRoot, type_safety_utils_1.TypeSafetyUtils.toBoolean((_b = ctx.container.get(entities_1.CliConfig)) === null || _b === void 0 ? void 0 : _b.localEntities));
            return relayContainer;
        });
        // ioc.bind<ExtendedRelayClient>(ExtendedRelayClient).toDynamicValue(ctx => {
        //   // let apiRootUrl: string | undefined;
        //   let relayAuthToken: string | undefined;
        //   let relayClient = new ExtendedRelayClient(
        //     ctx.container,
        //     ctx.container.get(UserContext),
        //     ctx.container.get<IContainerManager>(INTERFACES_CLI_ENTERPRISE.IContainerManager),
        //     ctx.container.get<IOutputStyler>(INTERFACES_CLI.IOutputStyler),
        //     ctx.container.get<IBetterLogger>(INTERFACES.IBetterLogger),
        //     ctx.container.get<CodiacRestClient>(CodiacRestClient),
        //     this.codiacApiUrl /* Careful! This property relies on the rest of the bootstrapping to already be done */,
        //     relayAuthToken
        //   );
        //   return relayClient;
        // });
        // RelaySocketListener.bootstrap(ioc);
        relay_websocket_client_1.RelayWebsocketClient.bootstrap(ioc);
        nats_client_1.NatsClient.bootstrap(ioc, {});
        ioc.bind(INTERFACES_CLI_1.INTERFACES_CLI.IMessagingClient).to(relay_websocket_client_1.RelayWebsocketClient).whenTargetNamed("websockets");
        ioc.bind(INTERFACES_CLI_1.INTERFACES_CLI.IMessagingClient).to(nats_client_1.NatsClient).whenTargetNamed("nats");
        ioc.bind(INTERFACES_CLI_1.INTERFACES_CLI.IMessagingClient).to(nats_client_1.NatsClient).whenTargetIsDefault();
        ioc.bind(lrp_watcher_predef_1.LrpWatcherPredef).toSelf();
        ioc.bind(rootmap_ui_utils_1.RootMapUiUtils).toSelf();
        this.isBootstrapped = true;
    }
}
exports.BootstrapperForEnterpriseCommands = BootstrapperForEnterpriseCommands;
BootstrapperForEnterpriseCommands.isBootstrapped = false;
//# sourceMappingURL=bootstrapper-for-enterprise-commands.js.map