// 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 DefaultHelmClient_1;
import { HelmExecutionBuilder } from '../execution/helm-execution-builder.js';
import { Repository } from '../model/repository.js';
import { Version } from '../model/version.js';
import { Release } from '../model/chart/release.js';
import { ReleaseItem } from '../model/release/release-item.js';
import { ChartDependencyUpdateRequest } from '../request/chart/chart-dependency-update-request.js';
import { ChartInstallRequest } from '../request/chart/chart-install-request.js';
import { ChartTestRequest } from '../request/chart/chart-test-request.js';
import { ChartUninstallRequest } from '../request/chart/chart-uninstall-request.js';
import { ChartUpgradeRequest } from '../request/chart/chart-upgrade-request.js';
import { VersionRequest } from '../request/common/version-request.js';
import { ReleaseListRequest } from '../request/release/release-list-request.js';
import { RepositoryAddRequest } from '../request/repository/repository-add-request.js';
import { RepositoryListRequest } from '../request/repository/repository-list-request.js';
import { RepositoryRemoveRequest } from '../request/repository/repository-remove-request.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';
let DefaultHelmClient = class DefaultHelmClient {
    static { DefaultHelmClient_1 = this; }
    logger;
    /**
     * The name of the namespace argument.
     */
    static NAMESPACE_ARG_NAME = 'namespace';
    constructor(logger) {
        this.logger = logger;
        this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
    }
    async version() {
        const request = new VersionRequest();
        const builder = new HelmExecutionBuilder();
        this.applyBuilderDefaults(builder);
        request.apply(builder);
        const execution = builder.build();
        if (execution instanceof Promise) {
            throw new TypeError('Unexpected async execution');
        }
        const versionClass = Version;
        const result = await execution.responseAs(versionClass);
        if (!(result instanceof Version)) {
            throw new TypeError('Unexpected response type');
        }
        return result.asSemanticVersion();
    }
    async listRepositories() {
        return this.executeAsList(new RepositoryListRequest(), Repository);
    }
    async addRepository(repository, options) {
        await this.executeAsync(new RepositoryAddRequest(repository, options));
    }
    async removeRepository(repository) {
        await this.executeAsync(new RepositoryRemoveRequest(repository));
    }
    async installChart(releaseName, chart, options) {
        const release = Release;
        const request = new ChartInstallRequest(releaseName, chart, options);
        return this.executeInternal(options.namespace, request, release, async (execution) => {
            const response = await execution.responseAs(release);
            return response;
        });
    }
    async uninstallChart(releaseName, options) {
        await this.executeAsync(new ChartUninstallRequest(releaseName, options));
    }
    async testChart(releaseName, options) {
        await this.executeAsync(new ChartTestRequest(releaseName, options));
    }
    async listReleases(allNamespaces, namespace, kubeContext) {
        return this.executeAsList(new ReleaseListRequest(allNamespaces, namespace, kubeContext), ReleaseItem);
    }
    async dependencyUpdate(chartName) {
        await this.executeAsync(new ChartDependencyUpdateRequest(chartName));
    }
    async upgradeChart(releaseName, chart, options) {
        const request = new ChartUpgradeRequest(releaseName, chart, options);
        return this.executeInternal(options.namespace, request, Release, async (execution) => execution.responseAs(Release));
    }
    /**
     * Applies the default namespace and authentication configuration to the given builder.
     * @param builder - The builder to apply to which the defaults should be applied
     */
    applyBuilderDefaults(builder) { }
    /**
     * Executes the given request and returns the response as the given class.
     * The request is executed using the default namespace.
     *
     * @param request - The request to execute
     * @param responseClass - The class of the response
     * @returns The response
     */
    async executeAsync(request, responseClass) {
        return this.executeInternal(undefined, request, responseClass, async (b) => {
            const response = await b.responseAs(responseClass);
            return response;
        });
    }
    /**
     * Executes the given request and returns the response as a list of the given class.
     * The request is executed using the default namespace.
     *
     * @param request - The request to execute
     * @param responseClass - The class of the response
     * @returns A list of response objects
     */
    async executeAsList(request, responseClass) {
        return this.executeInternal(undefined, request, responseClass, async (b) => {
            const response = await b.responseAsList(responseClass);
            return response;
        });
    }
    async executeInternal(namespace, request, responseClass, responseFunction) {
        if (namespace && !namespace.trim()) {
            throw new Error('namespace must not be blank');
        }
        const builder = new HelmExecutionBuilder();
        this.applyBuilderDefaults(builder);
        request.apply(builder);
        if (namespace) {
            builder.argument(DefaultHelmClient_1.NAMESPACE_ARG_NAME, namespace);
        }
        const execution = builder.build();
        return responseFunction(execution, responseClass);
    }
};
DefaultHelmClient = DefaultHelmClient_1 = __decorate([
    injectable()
    /**
     * The default implementation of the HelmClient interface.
     */
    ,
    __param(0, inject(InjectTokens.SoloLogger)),
    __metadata("design:paramtypes", [Object])
], DefaultHelmClient);
export { DefaultHelmClient };
//# sourceMappingURL=default-helm-client.js.map