// 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 { InjectTokens } from '../dependency-injection/inject-tokens.js';
import { patchInject } from '../dependency-injection/container-helper.js';
import { inject, injectable } from 'tsyringe-neo';
import * as constants from '../../core/constants.js';
let SharedResourceManager = class SharedResourceManager {
    logger;
    helm;
    chartManager;
    postgresEnabled = false;
    redisEnabled = false;
    additionalValuesArgument = '';
    constructor(logger, helm, chartManager) {
        this.logger = logger;
        this.helm = helm;
        this.chartManager = chartManager;
        this.helm = patchInject(helm, InjectTokens.Helm, this.constructor.name);
        this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
        this.chartManager = patchInject(chartManager, InjectTokens.ChartManager, this.constructor.name);
    }
    setAdditionalValuesArgument(additionalArguments) {
        this.additionalValuesArgument = additionalArguments;
    }
    enablePostgres() {
        this.postgresEnabled = true;
    }
    enableRedis() {
        this.redisEnabled = true;
    }
    async uninstallChart(namespace, context) {
        const isChartInstalled = await this.chartManager.isChartInstalled(namespace, constants.SOLO_SHARED_RESOURCES_CHART, context);
        if (!isChartInstalled) {
            this.logger?.info(`Shared resources chart is not installed in namespace ${namespace.name}, skipping uninstallation.`);
            return;
        }
        await this.chartManager.uninstall(namespace, constants.SOLO_SHARED_RESOURCES_CHART, context);
    }
    /**
     *  Installs the shared resources chart in the specified namespace if it is not already installed.
     *  Returns true if the chart was installed, false if it was already installed.
     * @param namespace
     * @param chartDirectory
     * @param soloChartVersion
     * @param context
     * @param valuesArgumentsMap
     */
    async installChart(namespace, chartDirectory, soloChartVersion, context, valuesArgumentsMap) {
        const isChartInstalled = await this.chartManager.isChartInstalled(namespace, constants.SOLO_SHARED_RESOURCES_CHART, context);
        if (isChartInstalled) {
            this.logger?.info(`Shared resources chart is already installed in namespace ${namespace.name}, skipping installation.`);
            return false;
        }
        valuesArgumentsMap = {
            ...valuesArgumentsMap,
            'postgresql.enabled': this.postgresEnabled.toString(),
            'redis.enabled': this.redisEnabled.toString(),
        };
        const values = Object.entries(valuesArgumentsMap)
            .map(([key, value]) => `--set ${key}=${value}`)
            .join(' ');
        const fullValues = this.additionalValuesArgument ? `${values} ${this.additionalValuesArgument}` : values;
        this.additionalValuesArgument = '';
        await this.chartManager.install(namespace, constants.SOLO_SHARED_RESOURCES_CHART, constants.SOLO_SHARED_RESOURCES_CHART, chartDirectory || constants.SOLO_TESTING_CHART_URL, soloChartVersion, fullValues, context);
        return true;
    }
};
SharedResourceManager = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.SoloLogger)),
    __param(1, inject(InjectTokens.Helm)),
    __param(2, inject(InjectTokens.ChartManager)),
    __metadata("design:paramtypes", [Object, Object, Function])
], SharedResourceManager);
export { SharedResourceManager };
//# sourceMappingURL=shared-resource-manager.js.map