// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Flags as flags } from '../../src/commands/flags.js';
import { InjectTokens } from '../../src/core/dependency-injection/inject-tokens.js';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from '../../src/core/dependency-injection/container-helper.js';
let CommandInvoker = class CommandInvoker {
    middlewares;
    configManager;
    remoteConfig;
    k8Factory;
    logger;
    constructor(middlewares, configManager, remoteConfig, k8Factory, logger) {
        this.middlewares = middlewares;
        this.configManager = configManager;
        this.remoteConfig = remoteConfig;
        this.k8Factory = k8Factory;
        this.logger = logger;
        this.middlewares = patchInject(middlewares, InjectTokens.Middlewares, this.constructor.name);
        this.configManager = patchInject(configManager, InjectTokens.ConfigManager, this.constructor.name);
        this.remoteConfig = patchInject(remoteConfig, InjectTokens.RemoteConfigRuntimeState, this.constructor.name);
        this.k8Factory = patchInject(k8Factory, InjectTokens.K8Factory, this.constructor.name);
        this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
    }
    async invoke({ callback, argv, command, subcommand, action, }) {
        // unload the remote config from the manager
        // this.remoteConfig.unload(); // TODO: unload using runtime state
        if (!argv.getArg(flags.context)) {
            argv.setArg(flags.context, this.k8Factory.default().contexts().readCurrent());
        }
        const middlewares = [this.updateConfigManager()];
        argv.setCommand(command, subcommand, action);
        for (const executable of middlewares) {
            await executable(argv.build());
        }
        try {
            await callback(argv.build());
        }
        catch (error) {
            this.logger.showUserError(error);
            throw error;
        }
    }
    updateConfigManager() {
        const self = this;
        return async (argv) => {
            self.configManager.update(argv);
            return argv;
        };
    }
};
CommandInvoker = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.Middlewares)),
    __param(1, inject(InjectTokens.ConfigManager)),
    __param(2, inject(InjectTokens.RemoteConfigRuntimeState)),
    __param(3, inject(InjectTokens.K8Factory)),
    __param(4, inject(InjectTokens.SoloLogger)),
    __metadata("design:paramtypes", [Function, Function, Object, Object, Object])
], CommandInvoker);
export { CommandInvoker };
//# sourceMappingURL=command-invoker.js.map