"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ACMFeatureSupportLevelProvider = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const common_1 = require("../../../common");
const constants_1 = require("../../config/constants");
const featureStateUtils_1 = require("./featureStateUtils");
const helpers_1 = require("../helpers");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const utils_1 = require("../../../common/utils");
const getFeatureSupportLevelsMap = () => {
    try {
        const featureSupportLevelsMap = {};
        constants_1.featureSupportLevelsACM.supportLevels.forEach((feature) => {
            const featureIdParams = {};
            const featureProps = feature.features;
            featureProps.forEach((ops) => {
                featureIdParams[ops.featureId] = ops.supportLevel;
            });
            featureSupportLevelsMap[feature.openshiftVersion] = featureIdParams;
        });
        return featureSupportLevelsMap;
    }
    catch (err) {
        // console.error(err);
        return {};
    }
};
const ACMFeatureSupportLevelProvider = ({ children, clusterImages, isEditClusterFlow = false, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const supportLevelData = react_1.default.useMemo(() => {
        return getFeatureSupportLevelsMap();
    }, []);
    const ocpVersions = (0, helpers_1.getOCPVersions)(clusterImages);
    const getNormalizedVersion = react_1.default.useCallback((versionName) => {
        const clusterImage = clusterImages.find((clusterImageSet) => { var _a; return ((_a = clusterImageSet.metadata) === null || _a === void 0 ? void 0 : _a.name) === versionName; });
        const version = (0, helpers_1.getVersionFromClusterImageSet)(clusterImage);
        return (0, utils_1.getMajorMinorVersion)(version);
    }, [clusterImages]);
    const getVersionSupportLevelsMap = react_1.default.useCallback((versionName) => {
        const normalized = getNormalizedVersion(versionName);
        return normalized
            ? supportLevelData[normalized]
            : {
            /* empty FeatureIdToSupportLevel */
            };
    }, [supportLevelData, getNormalizedVersion]);
    // TODO(mlibra): Following callbacks can be reused with the OCM flow, just based on providing an application-specific map
    const getFeatureSupportLevel = react_1.default.useCallback((versionName, featureId) => {
        const versionSupportLevelData = getVersionSupportLevelsMap(versionName);
        return versionSupportLevelData ? versionSupportLevelData[featureId] : undefined;
    }, [getVersionSupportLevelsMap]);
    const isFeatureSupportedCallback = react_1.default.useCallback((versionName, featureId) => {
        const supportLevel = getFeatureSupportLevel(versionName, featureId);
        return (0, featureStateUtils_1.isFeatureSupported)(supportLevel);
    }, [getFeatureSupportLevel]);
    const getDisabledReasonCallback = react_1.default.useCallback((versionName, featureId) => {
        const isSupported = isFeatureSupportedCallback(versionName, featureId);
        return (0, featureStateUtils_1.getFeatureDisabledReason)(featureId, isEditClusterFlow, versionName, ocpVersions, isSupported, t);
    }, [isFeatureSupportedCallback, t, ocpVersions, isEditClusterFlow]);
    const isFeatureDisabled = react_1.default.useCallback((version, featureId) => !!getDisabledReasonCallback(version, featureId), [getDisabledReasonCallback]);
    const providerValue = react_1.default.useMemo(() => {
        return {
            getVersionSupportLevelsMap,
            getFeatureSupportLevel,
            isFeatureDisabled,
            getFeatureDisabledReason: getDisabledReasonCallback,
            isFeatureSupported: isFeatureSupportedCallback,
        };
    }, [
        getVersionSupportLevelsMap,
        getFeatureSupportLevel,
        isFeatureDisabled,
        getDisabledReasonCallback,
        isFeatureSupportedCallback,
    ]);
    return (react_1.default.createElement(common_1.FeatureSupportLevelContextProvider, { value: providerValue }, children));
};
exports.ACMFeatureSupportLevelProvider = ACMFeatureSupportLevelProvider;
exports.default = exports.ACMFeatureSupportLevelProvider;
//# sourceMappingURL=ACMFeatureSupportLevelProvider.js.map