"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkTypeDropDown = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const formik_1 = require("formik");
const react_core_1 = require("@patternfly/react-core");
const ui_1 = require("../../../../common/components/ui");
const newFeatureSupportLevels_1 = require("../../../../common/components/newFeatureSupportLevels");
const NewFeatureSupportLevelBadge_1 = tslib_1.__importDefault(require("../../../../common/components/newFeatureSupportLevels/NewFeatureSupportLevelBadge"));
const networkType_1 = require("../../../../common/types/networkType");
const hooks_1 = require("../../../../common/hooks");
const common_1 = require("../../../../common");
const NetworkTypeDropDown = ({ isDisabled = false, featureSupportLevelData, }) => {
    const { t } = (0, hooks_1.useTranslation)();
    const [field, , { setValue }] = (0, formik_1.useField)('networkType');
    const [isOpen, setOpen] = react_1.default.useState(false);
    const { values } = (0, formik_1.useFormikContext)();
    const { getFeatureDisabledReason, getFeatureSupportLevel } = (0, newFeatureSupportLevels_1.useNewFeatureSupportLevel)();
    const fieldId = (0, ui_1.getFieldId)('networkType', 'input');
    const showThirdPartyBanner = (0, networkType_1.isThirdPartyCNI)(values.networkType);
    react_1.default.useEffect(() => {
        if (getFeatureDisabledReason(networkType_1.NETWORK_TYPE_FEATURE_IDS[field.value], featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined) !== undefined) {
            setValue(networkType_1.NETWORK_TYPE_OVN);
        }
    }, [field.value, setValue, featureSupportLevelData, getFeatureDisabledReason]);
    const currentDisplayValue = (0, networkType_1.NETWORK_TYPE_LABELS)(t)[field.value];
    const dropdownItems = Object.entries((0, networkType_1.NETWORK_TYPE_LABELS)(t)).map(([value, label]) => {
        const disabledReason = getFeatureDisabledReason(networkType_1.NETWORK_TYPE_FEATURE_IDS[value], featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
        const featureId = networkType_1.NETWORK_TYPE_FEATURE_IDS[value];
        const supportLevel = featureId
            ? getFeatureSupportLevel(featureId, featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined)
            : undefined;
        const shouldDisable = supportLevel === 'unavailable' ||
            supportLevel === 'unsupported' ||
            disabledReason !== undefined;
        return (react_1.default.createElement(react_core_1.DropdownItem, { key: value, id: value, value: value, isAriaDisabled: shouldDisable },
            react_1.default.createElement(react_core_1.Tooltip, { hidden: !disabledReason, content: disabledReason, position: "top" },
                react_1.default.createElement("div", null,
                    label,
                    react_1.default.createElement("span", { onClick: (event) => event.stopPropagation() },
                        react_1.default.createElement(NewFeatureSupportLevelBadge_1.default, { featureId: featureId, supportLevel: supportLevel }))))));
    });
    const onSelect = (event, nextValue) => {
        if (nextValue) {
            setValue(nextValue);
        }
        setOpen(false);
    };
    const toggle = (toggleRef) => (react_1.default.createElement(react_core_1.MenuToggle, { ref: toggleRef, onClick: () => setOpen(!isOpen), isExpanded: isOpen, isDisabled: isDisabled, id: fieldId, className: "pf-v6-u-w-100", style: { minWidth: '100%' } }, currentDisplayValue));
    return (react_1.default.createElement(react_core_1.Stack, { hasGutter: true },
        react_1.default.createElement(react_core_1.StackItem, null,
            react_1.default.createElement(react_core_1.FormGroup, { fieldId: fieldId, label: "Network type", id: `form-control__${fieldId}` },
                react_1.default.createElement(react_core_1.Dropdown, { id: `${fieldId}-dropdown`, onOpenChange: (open) => setOpen(open), onSelect: onSelect, toggle: toggle, isOpen: isOpen }, dropdownItems))),
        showThirdPartyBanner && react_1.default.createElement(common_1.ThirdPartyCNIBanner, null)));
};
exports.NetworkTypeDropDown = NetworkTypeDropDown;
//# sourceMappingURL=NetworkTypeDropdown.js.map