// SPDX-License-Identifier: Apache-2.0
import { CoreV1Api } from '@kubernetes/client-node';
import { NamespaceName } from '../../../../../types/namespace/namespace-name.js';
export class K8ClientContexts {
    kubeConfig;
    constructor(kubeConfig) {
        this.kubeConfig = kubeConfig;
    }
    list() {
        const contexts = [];
        for (const context of this.kubeConfig.getContexts()) {
            contexts.push(context.name);
        }
        return contexts;
    }
    readCurrent() {
        return this.kubeConfig.getCurrentContext();
    }
    readCurrentNamespace() {
        return NamespaceName.of(this.kubeConfig.getContextObject(this.readCurrent())?.namespace);
    }
    updateCurrent(context) {
        this.kubeConfig.setCurrentContext(context);
    }
    async testContextConnection(context) {
        const originalContextName = this.readCurrent();
        this.kubeConfig.setCurrentContext(context);
        const temporaryKubeClient = this.kubeConfig.makeApiClient(CoreV1Api);
        return await temporaryKubeClient
            .listNamespace()
            .then(() => {
            this.kubeConfig.setCurrentContext(originalContextName);
            return true;
        })
            .catch(() => {
            this.kubeConfig.setCurrentContext(originalContextName);
            return false;
        });
    }
}
//# sourceMappingURL=k8-client-contexts.js.map