// SPDX-License-Identifier: Apache-2.0
import { V1ObjectMeta, V1PersistentVolumeClaim, V1PersistentVolumeClaimSpec, V1VolumeResourceRequirements, } from '@kubernetes/client-node';
import { Duration } from '../../../../../core/time/duration.js';
import { SoloError } from '../../../../../core/errors/solo-error.js';
import { K8ClientPvc } from './k8-client-pvc.js';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceOperation } from '../../../resources/resource-operation.js';
import { ResourceType } from '../../../resources/resource-type.js';
export class K8ClientPvcs {
    kubeClient;
    constructor(kubeClient) {
        this.kubeClient = kubeClient;
    }
    async delete(pvcReference) {
        try {
            await this.kubeClient.deleteNamespacedPersistentVolumeClaim({
                name: pvcReference.name.toString(),
                namespace: pvcReference.namespace.toString(),
            });
        }
        catch (error) {
            KubeApiResponse.throwError(error, ResourceOperation.DELETE, ResourceType.PERSISTENT_VOLUME_CLAIM, pvcReference.namespace, pvcReference.name.toString());
        }
        return true;
    }
    async list(namespace, labels) {
        const pvcs = [];
        const labelSelector = labels ? labels.join(',') : undefined;
        let resp;
        try {
            resp = await this.kubeClient.listNamespacedPersistentVolumeClaim({
                namespace: namespace.name,
                labelSelector,
                timeoutSeconds: Duration.ofMinutes(5).toMillis(),
            });
        }
        catch (error) {
            KubeApiResponse.throwError(error, ResourceOperation.LIST, ResourceType.PERSISTENT_VOLUME_CLAIM, namespace, '');
        }
        for (const item of resp.items) {
            pvcs.push(item.metadata.name);
        }
        return pvcs;
    }
    async create(pvcReference, labels, accessModes) {
        const v1VolumeResourceRequirements = new V1VolumeResourceRequirements();
        v1VolumeResourceRequirements.requests = labels;
        const v1Spec = new V1PersistentVolumeClaimSpec();
        v1Spec.accessModes = accessModes;
        v1Spec.resources = v1VolumeResourceRequirements;
        const v1Metadata = new V1ObjectMeta();
        v1Metadata.name = pvcReference.name.toString();
        const v1Pvc = new V1PersistentVolumeClaim();
        v1Pvc.spec = v1Spec;
        v1Pvc.metadata = v1Metadata;
        let result;
        try {
            result = await this.kubeClient.createNamespacedPersistentVolumeClaim({
                namespace: pvcReference.namespace.toString(),
                body: v1Pvc,
            });
        }
        catch (error) {
            KubeApiResponse.throwError(error, ResourceOperation.CREATE, ResourceType.PERSISTENT_VOLUME_CLAIM, pvcReference.namespace, pvcReference.name.toString());
        }
        if (result) {
            return new K8ClientPvc(pvcReference);
        }
        else {
            throw new SoloError('Failed to create pvc');
        }
    }
}
//# sourceMappingURL=k8-client-pvcs.js.map