// SPDX-License-Identifier: Apache-2.0
import { V1ObjectMeta, V1Secret } from '@kubernetes/client-node';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceCreateError, ResourceNotFoundError, ResourceReplaceError, } from '../../../errors/resource-operation-errors.js';
import { ResourceType } from '../../../resources/resource-type.js';
import { ResourceOperation } from '../../../resources/resource-operation.js';
import { Duration } from '../../../../../core/time/duration.js';
export class K8ClientSecrets {
    kubeClient;
    constructor(kubeClient) {
        this.kubeClient = kubeClient;
    }
    async create(namespace, name, secretType, data, labels) {
        return await this.createOrReplaceWithForce(namespace, name, secretType, data, labels, false, true);
    }
    async createOrReplace(namespace, name, secretType, data, labels) {
        return await this.createOrReplaceWithForce(namespace, name, secretType, data, labels, false, false);
    }
    async delete(namespace, name) {
        const resp = await this.kubeClient.deleteNamespacedSecret(name, namespace.name);
        return !KubeApiResponse.isFailingStatus(resp.response);
    }
    async replace(namespace, name, secretType, data, labels) {
        return this.createOrReplaceWithForce(namespace, name, secretType, data, labels, true);
    }
    async read(namespace, name) {
        const { response, body } = await this.kubeClient.readNamespacedSecret(name, namespace.name).catch(error => error);
        KubeApiResponse.check(response, ResourceOperation.READ, ResourceType.SECRET, namespace, name);
        return {
            name: body.metadata.name,
            labels: body.metadata.labels,
            namespace: body.metadata.namespace,
            type: body.type,
            data: body.data,
        };
    }
    async list(namespace, labels) {
        const labelSelector = labels ? labels.join(',') : undefined;
        const secretList = await this.kubeClient.listNamespacedSecret(namespace.toString(), undefined, undefined, undefined, undefined, labelSelector, undefined, undefined, undefined, undefined, Duration.ofMinutes(5).toMillis());
        KubeApiResponse.check(secretList.response, ResourceOperation.LIST, ResourceType.SECRET, namespace, '');
        return secretList.body.items.map((secret) => {
            return {
                name: secret.metadata.name,
                labels: secret.metadata.labels,
                namespace: secret.metadata.namespace,
                type: secret.type,
                data: secret.data,
            };
        });
    }
    async exists(namespace, name) {
        try {
            const cm = await this.read(namespace, name);
            return !!cm;
        }
        catch (error) {
            if (error instanceof ResourceNotFoundError) {
                return false;
            }
            else {
                throw error;
            }
        }
    }
    async createOrReplaceWithForce(namespace, name, secretType, data, labels, forceReplace, forceCreate) {
        const replace = await this.shouldReplace(namespace, name, forceReplace, forceCreate);
        const v1Secret = new V1Secret();
        v1Secret.apiVersion = 'v1';
        v1Secret.kind = 'Secret';
        v1Secret.type = secretType;
        v1Secret.data = data;
        v1Secret.metadata = new V1ObjectMeta();
        v1Secret.metadata.name = name;
        v1Secret.metadata.labels = labels;
        try {
            const resp = replace
                ? await this.kubeClient.replaceNamespacedSecret(name, namespace.name, v1Secret)
                : await this.kubeClient.createNamespacedSecret(namespace.name, v1Secret);
            return !KubeApiResponse.isFailingStatus(resp.response);
        }
        catch (error) {
            const error_ = replace
                ? new ResourceReplaceError(ResourceType.SECRET, namespace, name, error)
                : new ResourceCreateError(ResourceType.SECRET, namespace, name, error);
            throw error_;
        }
    }
    async shouldReplace(namespace, name, forceReplace, forceCreate) {
        if (forceReplace && !forceCreate) {
            return true;
        }
        if (forceCreate) {
            return false;
        }
        return await this.exists(namespace, name);
    }
}
//# sourceMappingURL=k8-client-secrets.js.map