// SPDX-License-Identifier: Apache-2.0
import { container } from 'tsyringe-neo';
import { ResourceReadError, ResourceUpdateError } from '../../../errors/resource-operation-errors.js';
import { ResourceType } from '../../../resources/resource-type.js';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceOperation } from '../../../resources/resource-operation.js';
import { SoloError } from '../../../../../core/errors/solo-error.js';
import { InjectTokens } from '../../../../../core/dependency-injection/inject-tokens.js';
export class K8ClientIngresses {
    networkingApi;
    logger;
    constructor(networkingApi) {
        this.networkingApi = networkingApi;
        this.logger = container.resolve(InjectTokens.SoloLogger);
    }
    async listForAllNamespaces() {
        let result;
        try {
            result = await this.networkingApi.listIngressForAllNamespaces();
        }
        catch (error) {
            throw new ResourceReadError(ResourceType.INGRESS, undefined, '', error);
        }
        KubeApiResponse.check(result.response, ResourceOperation.LIST, ResourceType.INGRESS, undefined, '');
        if (result?.body?.items) {
            const ingressNames = [];
            for (const ingress of result.body.items) {
                ingressNames.push(ingress.metadata?.name ?? '');
            }
            return ingressNames;
        }
        else {
            return [];
        }
    }
    async update(namespace, name, patch) {
        const ingresses = [];
        // find the ingresses that match the specified name
        await this.networkingApi
            .listIngressForAllNamespaces()
            .then(response => {
            for (const ingress of response.body.items) {
                const currentIngressName = ingress.metadata.name;
                if (currentIngressName.includes(name)) {
                    ingresses.push(currentIngressName);
                }
            }
        })
            .catch(error => {
            throw new SoloError(`Error listing Ingresses: ${error}`);
        });
        for (const ingressName of ingresses) {
            let result;
            try {
                result = await this.networkingApi.patchNamespacedIngress(ingressName, namespace.name, patch, undefined, undefined, undefined, undefined, undefined, {
                    headers: { 'Content-Type': 'application/strategic-merge-patch+json' },
                });
                this.logger.info(`Patched Ingress ${ingressName} in namespace ${namespace}, patch: ${JSON.stringify(patch)}`);
            }
            catch (error) {
                throw new ResourceUpdateError(ResourceType.INGRESS, namespace, ingressName, error);
            }
            KubeApiResponse.check(result.response, ResourceOperation.UPDATE, ResourceType.INGRESS, namespace, ingressName);
            if (!result?.body) {
                throw new SoloError(`Failed to update Ingress ${ingressName} in namespace ${namespace}, received no ingress in response to patch`);
            }
        }
    }
}
//# sourceMappingURL=k8-client-ingresses.js.map