"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const __1 = require("../..");
const toNumber_1 = tslib_1.__importDefault(require("lodash-es/toNumber"));
const OcmTNADisclaimer_1 = tslib_1.__importDefault(require("./OcmTNADisclaimer"));
const formik_1 = require("formik");
const isItemEnabled = (value, allowHighlyAvailable, allowTNA) => {
    switch (value) {
        case 1:
        case 3:
            return true;
        case 2:
            return !!allowTNA;
        case 4:
        case 5:
            return !!allowHighlyAvailable;
    }
    return false;
};
const ControlPlaneNodesDropdown = ({ isDisabled = false, allowHighlyAvailable, allowTNA, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const [{ name, value: selectedValue }, , { setValue }] = (0, formik_1.useField)('controlPlaneCount');
    const { setFieldValue } = (0, formik_1.useFormikContext)();
    const [controlPlanelOpen, setControlPlanelOpen] = React.useState(false);
    const fieldId = (0, __1.getFieldId)(name, 'input');
    const options = [
        { value: 1, label: t('ai:1 (Single Node OpenShift - not highly available cluster)') },
        { value: 2, label: t('ai:2 (Two-Nodes Arbiter)') },
        { value: 3, label: t('ai:3 (highly available cluster)') },
        { value: 4, label: t('ai:4 (highly available cluster+)') },
        { value: 5, label: t('ai:5 (highly available cluster++)') },
    ];
    React.useEffect(() => {
        if (!allowHighlyAvailable && [4, 5].includes(selectedValue)) {
            setValue(3);
        }
    }, [allowHighlyAvailable, selectedValue, setValue]);
    const dropdownItems = options.map(({ value, label }) => {
        const isEnabled = isItemEnabled(value, allowHighlyAvailable, allowTNA);
        const disabledReason = t('ai:This option is not available with the selected OpenShift version');
        return (React.createElement(react_core_1.DropdownItem, { key: value, id: value.toString(), isAriaDisabled: !isItemEnabled, isDisabled: !isItemEnabled, selected: selectedValue === value, value: value },
            React.createElement(react_core_1.Tooltip, { hidden: isEnabled, content: disabledReason, position: "top" },
                React.createElement("div", null, label))));
    });
    const onControlPlaneSelect = (e, value) => {
        if (value === 1) {
            setFieldValue('userManagedNetworking', true);
            setFieldValue('platform', 'none');
        }
        else {
            setFieldValue('userManagedNetworking', false);
            setFieldValue('platform', 'baremetal');
        }
        setValue((0, toNumber_1.default)(value));
        setControlPlanelOpen(false);
    };
    return (React.createElement(React.Fragment, null,
        !isDisabled ? (React.createElement(react_core_1.FormGroup, { isInline: true, fieldId: fieldId, label: t('ai:Number of control plane nodes'), isRequired: true },
            React.createElement(react_core_1.Dropdown, { isOpen: controlPlanelOpen, onSelect: onControlPlaneSelect, onOpenChange: () => setControlPlanelOpen(!controlPlanelOpen), toggle: (toggleRef) => (React.createElement(react_core_1.MenuToggle, { className: "pf-v5-u-w-100", ref: toggleRef, isFullWidth: true, onClick: () => setControlPlanelOpen(!controlPlanelOpen), isExpanded: controlPlanelOpen }, selectedValue ? selectedValue : '3')), shouldFocusToggleOnSelect: true },
                React.createElement(react_core_1.DropdownList, null, dropdownItems)))) : (React.createElement(__1.StaticField, { name: 'controlPlaneCount', label: t('ai:Number of control plane nodes'), isRequired: true }, selectedValue)),
        selectedValue === 2 && React.createElement(OcmTNADisclaimer_1.default, null)));
};
exports.default = ControlPlaneNodesDropdown;
//# sourceMappingURL=ControlPlaneNodesDropdown.js.map