// SPDX-License-Identifier: Apache-2.0
import { PatchStrategy, setHeaderOptions, } from '@kubernetes/client-node';
import { container } from 'tsyringe-neo';
import { ResourceType } from '../../../resources/resource-type.js';
import { SoloError } from '../../../../../core/errors/solo-error.js';
import { InjectTokens } from '../../../../../core/dependency-injection/inject-tokens.js';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceOperation } from '../../../resources/resource-operation.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) {
            if (KubeApiResponse.isNotFound(error)) {
                return [];
            }
            KubeApiResponse.throwError(error, ResourceOperation.LIST, ResourceType.INGRESS, undefined, '');
        }
        if (result?.items) {
            const ingressNames = [];
            for (const ingress of result.items) {
                ingressNames.push(ingress.metadata?.name ?? '');
            }
            return ingressNames;
        }
        else {
            return [];
        }
    }
    async update(namespace, name, patch) {
        const ingresses = [];
        try {
            const result = await this.networkingApi.listIngressForAllNamespaces();
            for (const ingress of result.items) {
                const currentIngressName = ingress.metadata.name;
                if (currentIngressName.includes(name)) {
                    ingresses.push(currentIngressName);
                }
            }
        }
        catch (error) {
            if (!KubeApiResponse.isNotFound(error)) {
                KubeApiResponse.throwError(error, ResourceOperation.UPDATE, ResourceType.INGRESS, namespace, name);
            }
        }
        for (const ingressName of ingresses) {
            let result;
            try {
                result = await this.networkingApi.patchNamespacedIngress({
                    name: ingressName,
                    namespace: namespace.name,
                    body: patch,
                }, setHeaderOptions('Content-Type', PatchStrategy.MergePatch));
                this.logger.info(`Patched Ingress ${ingressName} in namespace ${namespace}, patch: ${JSON.stringify(patch)}`);
            }
            catch (error) {
                KubeApiResponse.throwError(error, ResourceOperation.UPDATE, ResourceType.INGRESS, namespace, ingressName);
            }
            if (!result) {
                throw new SoloError(`Failed to update Ingress ${ingressName} in namespace ${namespace}, received no ingress in response to patch`);
            }
        }
    }
}
//# sourceMappingURL=k8-client-ingresses.js.map