"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useSupportLevelsAPI;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const common_1 = require("../../common");
const api_1 = require("../../common/api");
const NewFeatureSupportLevelsAPI_1 = tslib_1.__importDefault(require("../../common/api/assisted-service/NewFeatureSupportLevelsAPI"));
function useSupportLevelsAPI(resourceKind, openshiftVersion, cpuArchitecture, platformType, supportedCpuArchitectures) {
    const [cpuArchitectures, setCpuArchitectures] = react_1.default.useState(null);
    const [features, setFeatures] = react_1.default.useState(null);
    const [supportLevelData, setSupportLevelData] = react_1.default.useState();
    const { addAlert } = (0, common_1.useAlerts)();
    const fetchArchitecturesSupportLevels = react_1.default.useCallback((openshiftVersion) => tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            const { data: architectures } = yield NewFeatureSupportLevelsAPI_1.default.architecturesSupportLevel(openshiftVersion);
            setCpuArchitectures(architectures.architectures);
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => addAlert({
                title: 'Failed to retrieve cpu architectures',
                message: (0, api_1.getApiErrorMessage)(e),
            }));
            setCpuArchitectures(null);
        }
    }), [addAlert]);
    const fetchFeaturesSupportLevels = react_1.default.useCallback((openshiftVersion, cpuArchitecture, platformType) => tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            const { data: features } = yield NewFeatureSupportLevelsAPI_1.default.featuresSupportLevel(openshiftVersion, cpuArchitecture, platformType);
            setFeatures(features.features);
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => addAlert({
                title: 'Failed to retrieve feature support levels',
                message: (0, api_1.getApiErrorMessage)(e),
            }));
            setFeatures(null);
        }
    }), [addAlert]);
    const fetchFeaturesSupportLevelsForAllCpuArchs = react_1.default.useCallback((supportedCpuArchitectures, openshiftVersion, platformType) => tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            const data = {
                x86_64: null,
                arm64: null,
                ppc64le: null,
                s390x: null,
            };
            if (supportedCpuArchitectures) {
                for (const cpuArch of supportedCpuArchitectures) {
                    const { data: features } = yield NewFeatureSupportLevelsAPI_1.default.featuresSupportLevel(openshiftVersion, cpuArch, platformType);
                    data[cpuArch] = features.features;
                }
                setSupportLevelData(data);
            }
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => addAlert({
                title: 'Failed to retrieve feature support levels',
                message: (0, api_1.getApiErrorMessage)(e),
            }));
            setFeatures(null);
        }
    }), [addAlert]);
    react_1.default.useEffect(() => {
        if (openshiftVersion) {
            if (resourceKind === 'architectures') {
                void fetchArchitecturesSupportLevels(openshiftVersion);
            }
            else if (resourceKind === 'featureForAllCpus' && supportedCpuArchitectures) {
                void fetchFeaturesSupportLevelsForAllCpuArchs(supportedCpuArchitectures, openshiftVersion, platformType);
            }
            else {
                void fetchFeaturesSupportLevels(openshiftVersion, cpuArchitecture, platformType);
            }
        }
    }, [
        openshiftVersion,
        cpuArchitecture,
        fetchArchitecturesSupportLevels,
        fetchFeaturesSupportLevels,
        resourceKind,
        platformType,
        supportedCpuArchitectures,
        fetchFeaturesSupportLevelsForAllCpuArchs,
    ]);
    if (resourceKind === 'architectures') {
        return cpuArchitectures;
    }
    else if (resourceKind === 'featureForAllCpus') {
        return supportLevelData;
    }
    else {
        return features;
    }
}
//# sourceMappingURL=useSupportLevelsAPI.js.map