"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isCPNDropdownItemEnabled = exports.ControlPlaneNodesLabel = exports.DEFAULT_VALUE_CPN = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const toNumber_1 = tslib_1.__importDefault(require("lodash-es/toNumber"));
const common_1 = require("../../../common");
const use_feature_1 = require("../../hooks/use-feature");
const newFeatureSupportLevels_1 = require("../../../common/components/newFeatureSupportLevels");
const featureStateUtils_1 = require("../featureSupportLevels/featureStateUtils");
const OcmTNADisclaimer_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/OcmTNADisclaimer"));
const OcmSNODisclaimer_1 = tslib_1.__importDefault(require("./OcmSNODisclaimer"));
const INPUT_NAME = 'controlPlaneCount';
const fieldId = (0, common_1.getFieldId)(INPUT_NAME, 'input');
exports.DEFAULT_VALUE_CPN = 3;
const ControlPlaneNodesLabel = () => {
    return (react_1.default.createElement(react_1.default.Fragment, null,
        "Number of control plane nodes",
        ' ',
        react_1.default.createElement(common_1.PopoverIcon, { bodyContent: react_1.default.createElement(react_1.default.Fragment, null, "Control plane nodes manage workloads, maintain cluster state, and ensure stability. Using more than three nodes boosts fault tolerance and availability, reducing downtime during failures.") })));
};
exports.ControlPlaneNodesLabel = ControlPlaneNodesLabel;
const isCPNDropdownItemEnabled = (controlPlaneNodeCount, isNonStandardControlPlaneEnabled, isTnaEnabled) => {
    if (controlPlaneNodeCount === 4 || controlPlaneNodeCount === 5) {
        return isNonStandardControlPlaneEnabled;
    }
    if (controlPlaneNodeCount === 2) {
        return isTnaEnabled;
    }
    return true;
};
exports.isCPNDropdownItemEnabled = isCPNDropdownItemEnabled;
const ControlPlaneNodesDropdown = ({ featureSupportLevelData, }) => {
    const [field, , { setValue }] = (0, formik_1.useField)(INPUT_NAME);
    const [isOpen, setOpen] = react_1.default.useState(false);
    const newFeatureSupportLevelContext = (0, newFeatureSupportLevels_1.useNewFeatureSupportLevel)();
    const isSingleClusterFeatureEnabled = (0, use_feature_1.useFeature)('ASSISTED_INSTALLER_SINGLE_CLUSTER_FEATURE');
    const snoExpansion = newFeatureSupportLevelContext.isFeatureSupported('SINGLE_NODE_EXPANSION', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
    const disabledReason = 'This option is not available with the current configurations. Make sure that OpenShift version is 4.19 or newer and no external platform integration is selected.';
    const isNonStandardControlPlaneEnabled = newFeatureSupportLevelContext.isFeatureSupported('NON_STANDARD_HA_CONTROL_PLANE', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
    const tnaSupport = newFeatureSupportLevelContext.getFeatureSupportLevel('TNA', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
    const isTnaEnabled = (0, featureStateUtils_1.isFeatureSupportedAndAvailable)(tnaSupport || 'unsupported');
    const options = [
        { value: 1, label: '1 (Single Node OpenShift)' },
        {
            value: 2,
            label: '2 (Two-Nodes Arbiter)',
            badge: tnaSupport === 'tech-preview' ? (react_1.default.createElement(common_1.TechnologyPreview, { position: common_1.PreviewBadgePosition.default })) : undefined,
            hidden: isSingleClusterFeatureEnabled,
        },
        { value: 3, label: '3 (highly available cluster)' },
        { value: 4, label: '4 (highly available cluster+)' },
        { value: 5, label: '5 (highly available cluster++)' },
    ].filter(({ hidden }) => !hidden);
    react_1.default.useEffect(() => {
        if (!field.value) {
            setValue(exports.DEFAULT_VALUE_CPN);
        }
    }, [field.value, setValue]);
    const onSelect = (event) => {
        const selectedValue = event === null || event === void 0 ? void 0 : event.currentTarget.id;
        setValue((0, toNumber_1.default)(selectedValue));
        setOpen(false);
    };
    const dropdownItems = options.map(({ value, label, badge }) => {
        const isItemEnabled = (0, exports.isCPNDropdownItemEnabled)(value, isNonStandardControlPlaneEnabled, isTnaEnabled);
        return (react_1.default.createElement(react_core_1.DropdownItem, { key: value, id: value.toString(), isAriaDisabled: !isItemEnabled },
            react_1.default.createElement(react_core_1.Tooltip, { hidden: isItemEnabled, content: disabledReason, position: "top" },
                react_1.default.createElement("div", null,
                    label,
                    " ",
                    badge))));
    });
    const toggle = (toggleRef) => {
        var _a;
        return (react_1.default.createElement(react_core_1.MenuToggle, { id: fieldId, ref: toggleRef, onClick: () => setOpen(!isOpen), isExpanded: isOpen, className: "pf-v6-u-w-100", "data-testid": "control-plane-nodes-dropdown-toggle" }, ((_a = options.find((opt) => opt.value === field.value)) === null || _a === void 0 ? void 0 : _a.label) || 'Select'));
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.FormGroup, { id: `form-control__${fieldId}`, label: react_1.default.createElement(exports.ControlPlaneNodesLabel, null), fieldId: fieldId },
            react_1.default.createElement(react_core_1.Dropdown, { id: `${fieldId}-dropdown`, isOpen: isOpen, toggle: toggle, onOpenChange: () => setOpen(!isOpen), onSelect: onSelect },
                react_1.default.createElement(react_core_1.DropdownList, null, dropdownItems))),
        field.value === 1 && react_1.default.createElement(OcmSNODisclaimer_1.default, { snoExpansionSupported: snoExpansion }),
        field.value === 2 && react_1.default.createElement(OcmTNADisclaimer_1.default, null)));
};
exports.default = ControlPlaneNodesDropdown;
//# sourceMappingURL=ControlPlaneNodesDropdown.js.map