// 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 * as helpers from '../../core/helpers.js';
import * as constants from '../../core/constants.js';
import * as ContextFlags from './flags.js';
import { SoloError } from '../../core/errors/solo-error.js';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from '../../core/dependency-injection/container-helper.js';
import { CommandHandler } from '../../core/command-handler.js';
import { InjectTokens } from '../../core/dependency-injection/inject-tokens.js';
import { LocalConfigRuntimeState } from '../../business/runtime-state/config/local/local-config-runtime-state.js';
let ClusterCommandHandlers = class ClusterCommandHandlers extends CommandHandler {
    tasks;
    localConfig;
    configs;
    constructor(tasks, localConfig, configs) {
        super();
        this.tasks = tasks;
        this.localConfig = localConfig;
        this.configs = configs;
        this.tasks = patchInject(tasks, InjectTokens.ClusterCommandTasks, this.constructor.name);
        this.localConfig = patchInject(localConfig, InjectTokens.LocalConfigRuntimeState, this.constructor.name);
        this.configs = patchInject(configs, InjectTokens.ClusterCommandConfigs, this.constructor.name);
    }
    /**
     * - Setup home directory.
     * - Create new local config if needed.
     * - Add new 'cluster-ref => context' mapping in the local config.
     */
    async connect(argv) {
        argv = helpers.addFlagsToArgv(argv, ContextFlags.CONNECT_FLAGS);
        await this.commandAction(argv, [
            this.tasks.initialize(argv, this.configs.connectConfigBuilder.bind(this.configs)),
            this.tasks.validateClusterRefs(),
            this.tasks.testConnectionToCluster(),
            this.tasks.connectClusterRef(),
        ], constants.LISTR_DEFAULT_OPTIONS.DEFAULT, 'cluster-ref config connect', null, 'cluster-ref config connect');
        return true;
    }
    async disconnect(argv) {
        argv = helpers.addFlagsToArgv(argv, ContextFlags.DEFAULT_FLAGS);
        try {
            await this.commandAction(argv, [
                this.tasks.initialize(argv, this.configs.defaultConfigBuilder.bind(this.configs), false),
                this.tasks.disconnectClusterRef(),
            ], constants.LISTR_DEFAULT_OPTIONS.DEFAULT, 'cluster-ref config disconnect', null, 'cluster-ref config disconnect');
        }
        catch (error) {
            throw new SoloError('Error on cluster-ref config disconnect', error);
        }
        return true;
    }
    async list(argv) {
        argv = helpers.addFlagsToArgv(argv, ContextFlags.NO_FLAGS);
        await this.commandAction(argv, [this.tasks.showClusterList()], constants.LISTR_DEFAULT_OPTIONS.DEFAULT, 'cluster-ref config list', null);
        return true;
    }
    async info(argv) {
        argv = helpers.addFlagsToArgv(argv, ContextFlags.DEFAULT_FLAGS);
        await this.commandAction(argv, [
            this.tasks.initialize(argv, this.configs.defaultConfigBuilder.bind(this.configs), false),
            this.tasks.getClusterInfo(),
        ], constants.LISTR_DEFAULT_OPTIONS.DEFAULT, 'cluster-ref config info', null);
        return true;
    }
    async setup(argv) {
        argv = helpers.addFlagsToArgv(argv, ContextFlags.SETUP_FLAGS);
        try {
            await this.commandAction(argv, [
                this.tasks.initialize(argv, this.configs.setupConfigBuilder.bind(this.configs)),
                this.tasks.installClusterChart(argv),
            ], constants.LISTR_DEFAULT_OPTIONS.DEFAULT, 'cluster-ref config setup', null, 'cluster-ref config setup');
        }
        catch (error) {
            throw new SoloError('Error on cluster setup', error);
        }
        return true;
    }
    async reset(argv) {
        argv = helpers.addFlagsToArgv(argv, ContextFlags.RESET_FLAGS);
        try {
            await this.commandAction(argv, [
                this.tasks.initialize(argv, this.configs.resetConfigBuilder.bind(this.configs)),
                this.tasks.uninstallClusterChart(argv),
            ], constants.LISTR_DEFAULT_OPTIONS.DEFAULT, 'cluster-ref config reset', null, 'cluster-ref config reset');
        }
        catch (error) {
            throw new SoloError('Error on cluster reset', error);
        }
        return true;
    }
};
ClusterCommandHandlers = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.ClusterCommandTasks)),
    __param(1, inject(InjectTokens.LocalConfigRuntimeState)),
    __param(2, inject(InjectTokens.ClusterCommandConfigs)),
    __metadata("design:paramtypes", [Function, LocalConfigRuntimeState, Function])
], ClusterCommandHandlers);
export { ClusterCommandHandlers };
//# sourceMappingURL=handlers.js.map