// SPDX-License-Identifier: Apache-2.0
import { SoloError } from '../../../../../core/errors/solo-error.js';
import { NamespaceName } from '../../../../../types/namespace/namespace-name.js';
import { sleep } from '../../../../../core/helpers.js';
import { Duration } from '../../../../../core/time/duration.js';
export class K8ClientNamespaces {
    kubeClient;
    constructor(kubeClient) {
        this.kubeClient = kubeClient;
    }
    async create(namespace) {
        const body = {
            metadata: {
                name: namespace.name,
            },
        };
        await this.kubeClient.createNamespace({ body });
        return true;
    }
    async delete(namespace) {
        try {
            await this.kubeClient.deleteNamespace({ name: namespace.name });
            try {
                let namespaceExists = true;
                while (namespaceExists) {
                    const response = await this.kubeClient.readNamespace({ name: namespace.name });
                    if (response?.metadata?.deletionTimestamp) {
                        await sleep(Duration.ofSeconds(1));
                    }
                    else {
                        namespaceExists = false;
                    }
                }
            }
            catch {
                // The namespace has been deleted
            }
            return true;
        }
        catch {
            return false;
        }
    }
    async has(namespace) {
        const namespaces = await this.list();
        return namespaces.some((namespaces) => namespaces.equals(namespace));
    }
    async list() {
        const response = await this.kubeClient.listNamespace();
        if (response && response.items) {
            const namespaces = [];
            for (const item of response.items) {
                namespaces.push(NamespaceName.of(item.metadata.name));
            }
            return namespaces;
        }
        throw new SoloError('incorrect response received from kubernetes API. Unable to list namespaces');
    }
}
//# sourceMappingURL=k8-client-namespaces.js.map