// 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 '../commands/flags.js';
import chalk from 'chalk';
import { SilentBreak } from './errors/silent-break.js';
import { patchInject } from './dependency-injection/container-helper.js';
import { InjectTokens } from './dependency-injection/inject-tokens.js';
import { inject, injectable } from 'tsyringe-neo';
import { LocalConfigRuntimeState } from '../business/runtime-state/config/local/local-config-runtime-state.js';
let Middlewares = class Middlewares {
    configManager;
    remoteConfig;
    k8Factory;
    logger;
    localConfig;
    helpRenderer;
    taskList;
    constructor(configManager, remoteConfig, k8Factory, logger, localConfig, helpRenderer, taskList) {
        this.configManager = configManager;
        this.remoteConfig = remoteConfig;
        this.k8Factory = k8Factory;
        this.logger = logger;
        this.localConfig = localConfig;
        this.helpRenderer = helpRenderer;
        this.taskList = taskList;
        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);
        this.localConfig = patchInject(localConfig, InjectTokens.LocalConfigRuntimeState, this.constructor.name);
        this.helpRenderer = patchInject(helpRenderer, InjectTokens.HelpRenderer, this.constructor.name);
        this.taskList = patchInject(taskList, InjectTokens.TaskList, this.constructor.name);
    }
    printCustomHelp(rootCmd) {
        /**
         * @param argv - listr Argv
         */
        return (argv) => {
            if (!argv['help']) {
                return;
            }
            rootCmd.showHelp((output) => {
                this.helpRenderer.render(rootCmd, output);
            });
            throw new SilentBreak('printed help, exiting');
        };
    }
    setLoggerDevFlag() {
        const logger = this.logger;
        /**
         * @param argv - listr Argv
         */
        return (argv) => {
            if (argv.dev) {
                logger.debug('Setting logger dev flag');
                logger.setDevMode(argv.dev);
            }
            return argv;
        };
    }
    /**
     * Processes the Argv and display the command header
     *
     * @returns callback function to be executed from listr
     */
    processArgumentsAndDisplayHeader() {
        const k8Factory = this.k8Factory;
        const configManager = this.configManager;
        const logger = this.logger;
        /**
         * @param argv - listr Argv
         * @param yargs - listr Yargs
         */
        return (argv, yargs) => {
            logger.debug('Processing arguments and displaying header');
            // set cluster and namespace in the global configManager from kubernetes context
            // so that we don't need to prompt the user
            const k8 = k8Factory.default();
            const contextNamespace = k8.contexts().readCurrentNamespace();
            const currentClusterName = k8.clusters().readCurrent();
            const contextName = k8.contexts().readCurrent();
            // reset config on `solo init` command
            if (argv._[0] === 'init') {
                configManager.reset();
            }
            const clusterName = configManager.getFlag(flags.clusterRef) || currentClusterName;
            // Set namespace if not provided
            if (contextNamespace?.name) {
                configManager.setFlag(flags.namespace, contextNamespace);
            }
            // apply precedence for flags
            argv = configManager.applyPrecedence(argv, yargs.parsed.aliases);
            // update config manager
            configManager.update(argv);
            // Build data to be displayed
            const currentCommand = argv._.join(' ');
            const commandArguments = flags.stringifyArgv(argv);
            const commandData = (currentCommand + ' ' + commandArguments).trim();
            if (this.taskList.parentTaskListMap.size === 0) {
                // Display command header
                logger.showUser(chalk.cyan('\n******************************* Solo *********************************************'));
                logger.showUser(chalk.cyan('Version\t\t\t:'), chalk.yellow(configManager.getVersion()));
                logger.showUser(chalk.cyan('Kubernetes Context\t:'), chalk.yellow(contextName));
                logger.showUser(chalk.cyan('Kubernetes Cluster\t:'), chalk.yellow(clusterName));
                logger.showUser(chalk.cyan('Current Command\t\t:'), chalk.yellow(commandData));
                if (configManager.getFlag(flags.namespace)?.name) {
                    logger.showUser(chalk.cyan('Kubernetes Namespace\t:'), chalk.yellow(configManager.getFlag(flags.namespace)));
                }
                logger.showUser(chalk.cyan('**********************************************************************************'));
            }
            return argv;
        };
    }
};
Middlewares = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.ConfigManager)),
    __param(1, inject(InjectTokens.RemoteConfigRuntimeState)),
    __param(2, inject(InjectTokens.K8Factory)),
    __param(3, inject(InjectTokens.SoloLogger)),
    __param(4, inject(InjectTokens.LocalConfigRuntimeState)),
    __param(5, inject(InjectTokens.HelpRenderer)),
    __param(6, inject(InjectTokens.TaskList)),
    __metadata("design:paramtypes", [Function, Object, Object, Object, LocalConfigRuntimeState, Function, Object])
], Middlewares);
export { Middlewares };
//# sourceMappingURL=middlewares.js.map