"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewFeatureSupportLevelProvider = exports.getFeatureSupported = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const common_1 = require("../../../common");
const hooks_1 = require("../../hooks");
const featureStateUtils_1 = require("./featureStateUtils");
const useInfraEnv_1 = tslib_1.__importDefault(require("../../hooks/useInfraEnv"));
const newFeatureSupportLevels_1 = require("../../../common/components/newFeatureSupportLevels");
const useSupportLevelsAPI_1 = tslib_1.__importDefault(require("../../hooks/useSupportLevelsAPI"));
const OpenShiftVersionsContext_1 = require("../clusterWizard/OpenShiftVersionsContext");
const getFeatureSupported = (featureSupportLevels, featureId) => {
    return featureSupportLevels && (0, featureStateUtils_1.isFeatureSupportedAndAvailable)(featureSupportLevels[featureId]);
};
exports.getFeatureSupported = getFeatureSupported;
const NewFeatureSupportLevelProvider = ({ cluster, children, loadingUi, cpuArchitecture, openshiftVersion, platformType, }) => {
    const { loading: loadingOCPVersions } = (0, OpenShiftVersionsContext_1.useOpenShiftVersionsContext)();
    const pullSecret = (0, hooks_1.usePullSecret)();
    const { infraEnv, isLoading: isInfraEnvLoading } = (0, useInfraEnv_1.default)((cluster === null || cluster === void 0 ? void 0 : cluster.id) || '', (cluster === null || cluster === void 0 ? void 0 : cluster.cpuArchitecture)
        ? cluster.cpuArchitecture
        : common_1.CpuArchitecture.USE_DAY1_ARCHITECTURE, cluster === null || cluster === void 0 ? void 0 : cluster.name, pullSecret, cluster === null || cluster === void 0 ? void 0 : cluster.openshiftVersion);
    const featureSupportLevels = (0, useSupportLevelsAPI_1.default)('features', openshiftVersion, cpuArchitecture, platformType);
    const supportLevelData = react_1.default.useMemo(() => {
        if (!featureSupportLevels) {
            return {};
        }
        return featureSupportLevels;
    }, [featureSupportLevels]);
    const isLoading = !supportLevelData || loadingOCPVersions || isInfraEnvLoading;
    const activeFeatureConfiguration = react_1.default.useMemo(() => ({
        underlyingCpuArchitecture: ((infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.cpuArchitecture) ||
            (cluster === null || cluster === void 0 ? void 0 : cluster.cpuArchitecture) ||
            (0, common_1.getDefaultCpuArchitecture)()),
        hasStaticIpNetworking: !!(infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.staticNetworkConfig),
    }), [cluster === null || cluster === void 0 ? void 0 : cluster.cpuArchitecture, infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.cpuArchitecture, infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.staticNetworkConfig]);
    const getFeatureSupportLevels = react_1.default.useCallback(() => {
        return supportLevelData;
    }, [supportLevelData]);
    const getFeatureSupportLevel = react_1.default.useCallback((featureId, supportLevelDataNew) => {
        if (supportLevelDataNew) {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            return supportLevelDataNew ? supportLevelDataNew[featureId] : undefined;
        }
        else {
            if (supportLevelData) {
                // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                // @ts-ignore
                return supportLevelData ? supportLevelData[featureId] : undefined;
            }
        }
    }, [supportLevelData]);
    const isFeatureSupportedCallback = react_1.default.useCallback((featureId, supportLevelDataNew) => {
        const supportLevel = getFeatureSupportLevel(featureId, supportLevelDataNew);
        return (0, featureStateUtils_1.isFeatureSupportedAndAvailable)(supportLevel);
    }, [getFeatureSupportLevel]);
    const getDisabledReasonCallback = react_1.default.useCallback((featureId, supportLevelDataNew, cpuArchitecture, platformType) => {
        const isSupported = isFeatureSupportedCallback(featureId, supportLevelDataNew);
        return (0, featureStateUtils_1.getNewFeatureDisabledReason)(featureId, cluster, activeFeatureConfiguration, isSupported, cpuArchitecture, platformType);
    }, [isFeatureSupportedCallback, cluster, activeFeatureConfiguration]);
    const isFeatureDisabled = react_1.default.useCallback((featureId, supportLevelDataNew) => !!getDisabledReasonCallback(featureId, supportLevelDataNew), [getDisabledReasonCallback]);
    const providerValue = react_1.default.useMemo(() => {
        return {
            getFeatureSupportLevels: getFeatureSupportLevels,
            getFeatureSupportLevel: getFeatureSupportLevel,
            isFeatureDisabled: isFeatureDisabled,
            getFeatureDisabledReason: getDisabledReasonCallback,
            isFeatureSupported: isFeatureSupportedCallback,
            activeFeatureConfiguration,
        };
    }, [
        getFeatureSupportLevels,
        getFeatureSupportLevel,
        isFeatureDisabled,
        getDisabledReasonCallback,
        isFeatureSupportedCallback,
        activeFeatureConfiguration,
    ]);
    return (react_1.default.createElement(newFeatureSupportLevels_1.NewFeatureSupportLevelContextProvider, { value: providerValue }, isLoading ? loadingUi : children));
};
exports.NewFeatureSupportLevelProvider = NewFeatureSupportLevelProvider;
exports.default = exports.NewFeatureSupportLevelProvider;
//# sourceMappingURL=FeatureSupportLevelProvider.js.map