"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cpuArchitecture_1 = require("../../common//types/cpuArchitecture");
const CACHE_KEY = 'infra-env-ids-cache-v2';
const update = (cache) => {
    localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
};
const read = () => {
    const EMPTY_CACHE = '{}';
    const cache = localStorage.getItem(CACHE_KEY) || EMPTY_CACHE;
    try {
        return JSON.parse(cache);
    }
    catch (_a) {
        return {};
    }
};
const InfraEnvIdsCacheService = {
    key(index) {
        const cache = read();
        return Object.keys(cache)[index];
    },
    get length() {
        const cache = read();
        return Object.keys(cache).length;
    },
    clear() {
        localStorage.removeItem(CACHE_KEY);
    },
    getInfraEnvId(clusterId, cpuArchitecture) {
        if (!clusterId || !cpuArchitecture) {
            return new Error(`clusterId or cpuArchitecture are missing`);
        }
        const cache = read();
        const clusterInfraEnvs = cache[clusterId];
        if (!clusterInfraEnvs) {
            return new Error(`Not infraEnvs found for this cluster ${clusterId}`);
        }
        if (cpuArchitecture !== cpuArchitecture_1.CpuArchitecture.USE_DAY1_ARCHITECTURE &&
            cpuArchitecture !== cpuArchitecture_1.CpuArchitecture.MULTI) {
            return clusterInfraEnvs[cpuArchitecture] || new Error(`InfraEnv can't be found`);
        }
        const architectures = (0, cpuArchitecture_1.getAllCpuArchitectures)().filter((arch) => {
            return clusterInfraEnvs[arch];
        });
        if (architectures.length === 1) {
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            return clusterInfraEnvs[architectures[0]];
        }
        throw new Error(`More than one infraEnv found for cluster ${clusterId} without specified architecture`);
    },
    removeInfraEnvId(clusterId, cpuArchitecture) {
        if (!clusterId) {
            return;
        }
        const cache = read();
        if (cpuArchitecture === cpuArchitecture_1.CpuArchitecture.USE_DAY1_ARCHITECTURE) {
            delete cache[clusterId];
        }
        else if (cache[clusterId]) {
            delete cache[clusterId][cpuArchitecture];
        }
        update(cache);
    },
    removeInfraEnvs(clusterId) {
        if (!clusterId) {
            return;
        }
        const cache = read();
        delete cache[clusterId];
        update(cache);
    },
    updateInfraEnvs(clusterId, infraEnvs) {
        if (!clusterId) {
            return;
        }
        const cache = read();
        if (!cache[clusterId]) {
            cache[clusterId] = {};
        }
        infraEnvs.forEach((infraEnv) => {
            if (infraEnv.cpuArchitecture) {
                cache[clusterId][infraEnv.cpuArchitecture] = infraEnv.id;
            }
        });
        update(cache);
    },
};
exports.default = InfraEnvIdsCacheService;
//# sourceMappingURL=InfraEnvIdsCacheService.js.map