"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const common_1 = require("../../../common");
const api_1 = require("../../../common/api");
const formik_1 = require("formik");
const OpenshiftVersionHelperText_1 = require("./OpenshiftVersionHelperText");
const OpenShiftVersionsContext_1 = require("../clusterWizard/OpenShiftVersionsContext");
const filterMinVersions = (versions, minVersion) => versions.filter((v) => (0, common_1.getComparableVersionInt)(v.value) >= minVersion);
const OcmOpenShiftVersionSelect = ({ minVersionAllowed }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { values: { customOpenshiftSelect }, } = (0, formik_1.useFormikContext)();
    const { allVersions: allVersionsCtx, latestVersions } = (0, OpenShiftVersionsContext_1.useOpenShiftVersionsContext)();
    const allVersions = minVersionAllowed
        ? filterMinVersions(allVersionsCtx, minVersionAllowed)
        : allVersionsCtx;
    const versions = minVersionAllowed
        ? filterMinVersions(latestVersions, minVersionAllowed)
        : latestVersions;
    const [isOpenshiftVersionModalOpen, setIsOpenshiftVersionModalOpen] = react_1.default.useState(false);
    const customItem = react_1.default.useMemo(() => {
        if (customOpenshiftSelect &&
            !versions.some((version) => version.value === customOpenshiftSelect)) {
            return allVersions.find((version) => version.value === customOpenshiftSelect);
        }
        return undefined;
    }, [allVersions, customOpenshiftSelect, versions]);
    const getHelperText = (value, inModal) => {
        return (0, OpenshiftVersionHelperText_1.getOpenshiftVersionHelperText)(allVersions, value, t, inModal);
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(common_1.OpenShiftVersionDropdown, { name: "openshiftVersion", versions: versions, getHelperText: getHelperText, showReleasesLink: api_1.isInOcm, showOpenshiftVersionModal: () => setIsOpenshiftVersionModalOpen(true), customVersion: customItem }),
        isOpenshiftVersionModalOpen && (react_1.default.createElement(common_1.OpenShiftVersionModal, { allVersions: allVersions, onClose: () => setIsOpenshiftVersionModalOpen(false), getHelperText: getHelperText }))));
};
exports.default = OcmOpenShiftVersionSelect;
//# sourceMappingURL=OcmOpenShiftVersionSelect.js.map