// 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); }
};
var ClusterCommandConfigs_1;
import { Flags as flags } from '../flags.js';
import { ListrInquirerPromptAdapter } from '@listr2/prompt-adapter-inquirer';
import { confirm as confirmPrompt } from '@inquirer/prompts';
import { UserBreak } from '../../core/errors/user-break.js';
import { inject, injectable } from 'tsyringe-neo';
import { InjectTokens } from '../../core/dependency-injection/inject-tokens.js';
import { patchInject } from '../../core/dependency-injection/container-helper.js';
import { LocalConfigRuntimeState } from '../../business/runtime-state/config/local/local-config-runtime-state.js';
let ClusterCommandConfigs = class ClusterCommandConfigs {
    static { ClusterCommandConfigs_1 = this; }
    configManager;
    logger;
    chartManager;
    localConfig;
    k8Factory;
    static CONNECT_CONFIGS_NAME = 'connectConfig';
    static DEFAULT_CONFIGS_NAME = 'defaultConfig';
    constructor(configManager, logger, chartManager, localConfig, k8Factory) {
        this.configManager = configManager;
        this.logger = logger;
        this.chartManager = chartManager;
        this.localConfig = localConfig;
        this.k8Factory = k8Factory;
        this.configManager = patchInject(configManager, InjectTokens.ConfigManager, this.constructor.name);
        this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
        this.chartManager = patchInject(chartManager, InjectTokens.ChartManager, this.constructor.name);
        this.localConfig = patchInject(localConfig, InjectTokens.LocalConfigRuntimeState, this.constructor.name);
        this.k8Factory = patchInject(k8Factory, InjectTokens.K8Factory, this.constructor.name);
    }
    async connectConfigBuilder(argv, context_, task) {
        // Apply changes to argv[context] before the config is initiated, because the `context` field is immutable
        if (!argv[flags.context.name]) {
            const isQuiet = this.configManager.getFlag(flags.quiet);
            if (isQuiet) {
                argv[flags.context.name] = this.k8Factory.default().contexts().readCurrent();
            }
            else {
                const kubeContexts = this.k8Factory.default().contexts().list();
                argv[flags.context.name] = await flags.context.prompt(task, kubeContexts, argv[flags.clusterRef.name]);
            }
        }
        this.configManager.update(argv);
        context_.config = this.configManager.getConfig(ClusterCommandConfigs_1.CONNECT_CONFIGS_NAME, argv.flags, []);
        return context_.config;
    }
    async defaultConfigBuilder(argv, context_) {
        this.configManager.update(argv);
        context_.config = this.configManager.getConfig(ClusterCommandConfigs_1.DEFAULT_CONFIGS_NAME, argv.flags, []);
        return context_.config;
    }
    async setupConfigBuilder(argv, context_, task) {
        this.configManager.update(argv);
        flags.disablePrompts([flags.chartDirectory]);
        await this.configManager.executePrompt(task, [
            flags.chartDirectory,
            flags.clusterSetupNamespace,
            flags.deployMinio,
            flags.deployPrometheusStack,
            flags.deployGrafanaAgent,
        ]);
        const config = {
            chartDirectory: this.configManager.getFlag(flags.chartDirectory),
            clusterSetupNamespace: this.configManager.getFlag(flags.clusterSetupNamespace),
            deployMinio: this.configManager.getFlag(flags.deployMinio),
            deployPrometheusStack: this.configManager.getFlag(flags.deployPrometheusStack),
            deployGrafanaAgent: this.configManager.getFlag(flags.deployGrafanaAgent),
            soloChartVersion: this.configManager.getFlag(flags.soloChartVersion),
            clusterRef: this.configManager.getFlag(flags.clusterRef),
        };
        config.context =
            this.localConfig.configuration.clusterRefs.get(config.clusterRef)?.toString() ??
                this.k8Factory.default().contexts().readCurrent();
        context_.config = config;
        return context_.config;
    }
    async resetConfigBuilder(argv, context_, task) {
        if (!argv[flags.force.name]) {
            const confirmResult = await task.prompt(ListrInquirerPromptAdapter).run(confirmPrompt, {
                default: false,
                message: 'Are you sure you would like to uninstall solo-cluster-setup chart?',
            });
            if (!confirmResult) {
                throw new UserBreak('Aborted application by user prompt');
            }
        }
        this.configManager.update(argv);
        context_.config = {
            clusterReference: this.configManager.getFlag(flags.clusterRef),
            clusterSetupNamespace: this.configManager.getFlag(flags.clusterSetupNamespace),
        };
        context_.config.clusterReference ??= this.k8Factory.default().clusters().readCurrent();
        context_.config.context = this.localConfig.configuration.clusterRefs
            .get(context_.config.clusterReference)
            ?.toString();
        if (!context_.config.context) {
            throw new Error(`Cluster "${context_.config.clusterReference}" not found in the LocalConfig`);
        }
        return context_.config;
    }
};
ClusterCommandConfigs = ClusterCommandConfigs_1 = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.ConfigManager)),
    __param(1, inject(InjectTokens.SoloLogger)),
    __param(2, inject(InjectTokens.ChartManager)),
    __param(3, inject(InjectTokens.LocalConfigRuntimeState)),
    __param(4, inject(InjectTokens.K8Factory)),
    __metadata("design:paramtypes", [Function, Object, Function, LocalConfigRuntimeState, Object])
], ClusterCommandConfigs);
export { ClusterCommandConfigs };
//# sourceMappingURL=configs.js.map