"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const apis_1 = require("./apis");
const InfraEnvIdsCacheService_1 = tslib_1.__importDefault(require("./InfraEnvIdsCacheService"));
const dummyData_1 = require("../components/clusterConfiguration/staticIp/data/dummyData");
const InfraEnvsService = {
    getInfraEnvId(clusterId, cpuArchitecture, isSingleClusterFeatureEnabled) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let infraEnvId = InfraEnvIdsCacheService_1.default.getInfraEnvId(clusterId, cpuArchitecture);
            if (infraEnvId instanceof Error) {
                const { data: infraEnvs } = yield apis_1.InfraEnvsAPI.list(!isSingleClusterFeatureEnabled ? clusterId : '');
                if (infraEnvs.length > 0) {
                    InfraEnvIdsCacheService_1.default.updateInfraEnvs(clusterId, infraEnvs);
                    infraEnvId = InfraEnvIdsCacheService_1.default.getInfraEnvId(clusterId, cpuArchitecture);
                }
                if (!infraEnvId) {
                    InfraEnvIdsCacheService_1.default.removeInfraEnvId(clusterId, cpuArchitecture);
                }
            }
            return infraEnvId;
        });
    },
    getInfraEnv(clusterId, cpuArchitecture) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const infraEnvId = yield InfraEnvsService.getInfraEnvId(clusterId, cpuArchitecture);
            if (infraEnvId && !(infraEnvId instanceof Error)) {
                const { data } = yield apis_1.InfraEnvsAPI.get(infraEnvId);
                return data;
            }
            else {
                return new Error(`Failed to retrieve the infraEnv for ${clusterId}`);
            }
        });
    },
    getAllInfraEnvIds(clusterId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const infraEnvs = yield InfraEnvsService.getAllInfraEnvs(clusterId);
            return infraEnvs.map((infraEnv) => infraEnv.id);
        });
    },
    getAllInfraEnvs(clusterId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { data: infraEnvs } = yield apis_1.InfraEnvsAPI.list(clusterId);
            return infraEnvs;
        });
    },
    create(params) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!params.clusterId) {
                throw new Error('Cannot create InfraEnv, clusterId is missing');
            }
            const { data: infraEnv } = yield apis_1.InfraEnvsAPI.register(params);
            if (!infraEnv.id) {
                throw new Error('API returned no ID for the underlying InfraEnv');
            }
            InfraEnvIdsCacheService_1.default.updateInfraEnvs(params.clusterId, [infraEnv]);
            return infraEnv;
        });
    },
    removeAll(clusterId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { data: infraEnvs } = yield apis_1.InfraEnvsAPI.list(clusterId);
            const promises = infraEnvs.map((infraEnv) => {
                return apis_1.InfraEnvsAPI.deregister(infraEnv.id);
            });
            InfraEnvIdsCacheService_1.default.removeInfraEnvs(clusterId);
            return Promise.all(promises);
        });
    },
    updateAllInfraEnvsToDhcp(clusterId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const infraEnvIds = yield InfraEnvsService.getAllInfraEnvIds(clusterId);
            const infraEnvUpdates = infraEnvIds.map((id) => apis_1.InfraEnvsAPI.update(id, {
                staticNetworkConfig: [],
            }));
            return Promise.all(infraEnvUpdates);
        });
    },
    setDummyStaticConfigToInfraEnv(infraEnvId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { data } = yield apis_1.InfraEnvsAPI.update(infraEnvId, {
                staticNetworkConfig: (0, dummyData_1.getDummyInfraEnvField)(),
            });
            return data;
        });
    },
    syncStaticIpConfigs(clusterId, currentInfraEnvId, staticNetworkConfig) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            // Updates staticIpConfigs on "currentInfraEnvId", and then it
            const { data: updatedInfraEnv } = yield apis_1.InfraEnvsAPI.update(currentInfraEnvId, {
                staticNetworkConfig,
            });
            const updateRequests = [];
            const infraEnvIds = yield InfraEnvsService.getAllInfraEnvIds(clusterId);
            infraEnvIds.forEach((infraEnvId) => {
                // Copies the same configuration to the other infraEnvs of the same cluster
                if (infraEnvId !== currentInfraEnvId) {
                    updateRequests.push(apis_1.InfraEnvsAPI.update(infraEnvId, {
                        staticNetworkConfig,
                    }));
                }
            });
            // TODO (multi-arch) what should happen if one of the infraEnvs fails to get the same configuration
            return Promise.all(updateRequests).then(() => {
                return updatedInfraEnv;
            });
        });
    },
    makeInfraEnvName(cpuArchitecture, name) {
        return `${name || ''}_infra-env-${cpuArchitecture}`;
    },
};
exports.default = InfraEnvsService;
//# sourceMappingURL=InfraEnvsService.js.map