"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 common_1 = require("../../../common");
const newFeatureSupportLevels_1 = require("../../../common/components/newFeatureSupportLevels");
const useSupportLevelsAPI_1 = tslib_1.__importDefault(require("../../hooks/useSupportLevelsAPI"));
const constants_1 = require("./platformIntegration/constants");
const INPUT_NAME = 'cpuArchitecture';
const fieldId = (0, common_1.getFieldId)(INPUT_NAME, 'input');
const isCurrentDefaultCpuArchitecture = (cpuArchitectures, cpuArchitecture, day1CpuArchitecture) => {
    const cpuArchFound = cpuArchitectures.find((cpuArch) => cpuArch === cpuArchitecture);
    return (cpuArchFound === undefined &&
        cpuArchitecture !== (0, common_1.getDefaultCpuArchitecture)() &&
        day1CpuArchitecture !== common_1.CpuArchitecture.ARM);
};
const CpuArchitectureDropdown = ({ openshiftVersion, day1CpuArchitecture, cpuArchitectures, onChange, platformType, }) => {
    const newFeatureSupportLevelContext = (0, newFeatureSupportLevels_1.useNewFeatureSupportLevel)();
    const [field, { value: selectedCpuArchitecture }, { setValue }] = (0, formik_1.useField)(INPUT_NAME);
    const [isOpen, setOpen] = react_1.default.useState(false);
    const prevVersionRef = react_1.default.useRef(openshiftVersion);
    const [currentCpuArch, setCurrentCpuArch] = react_1.default.useState(day1CpuArchitecture ? common_1.architectureData[day1CpuArchitecture].label : common_1.CpuArchitecture.x86);
    const supportLevelDataForAllCpuArchs = (0, useSupportLevelsAPI_1.default)('featureForAllCpus', openshiftVersion, undefined, platformType, cpuArchitectures);
    const enabledItems = react_1.default.useMemo(() => {
        if (cpuArchitectures !== undefined) {
            return cpuArchitectures.map((cpuArch) => {
                let isCpuSupported = true;
                let disabledReason = `This cluster is using the  ${platformType ? constants_1.ExternalPlatformLabels[platformType] : ''} platform which doesn't allow this CPU architecture.`;
                if (day1CpuArchitecture === common_1.CpuArchitecture.ARM) {
                    isCpuSupported = cpuArch === common_1.CpuArchitecture.ARM;
                    disabledReason =
                        'Only hosts that have Arm64 CPU architecture can be added to this cluster.';
                }
                else if (supportLevelDataForAllCpuArchs) {
                    const featureSupportLevelData = supportLevelDataForAllCpuArchs[cpuArch];
                    isCpuSupported = newFeatureSupportLevelContext.isFeatureSupported(constants_1.ExternalPlaformIds[platformType ? platformType : 'baremetal'], featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
                }
                return (react_1.default.createElement(react_core_1.DropdownItem, { key: cpuArch, id: cpuArch, description: cpuArch ? common_1.architectureData[cpuArch].description : '', isAriaDisabled: !isCpuSupported },
                    react_1.default.createElement(react_core_1.Split, null,
                        react_1.default.createElement(react_core_1.SplitItem, null,
                            react_1.default.createElement(react_core_1.Tooltip, { hidden: isCpuSupported, content: disabledReason, position: "top" },
                                react_1.default.createElement("div", null, cpuArch ? common_1.architectureData[cpuArch].label : ''))),
                        react_1.default.createElement(react_core_1.SplitItem, { isFilled: true }))));
            });
        }
    }, [
        cpuArchitectures,
        day1CpuArchitecture,
        supportLevelDataForAllCpuArchs,
        platformType,
        newFeatureSupportLevelContext,
    ]);
    const onSelect = react_1.default.useCallback((event) => {
        const selectedCpuArch = event === null || event === void 0 ? void 0 : event.currentTarget.id;
        setValue(selectedCpuArch);
        setOpen(false);
        setCurrentCpuArch(common_1.architectureData[selectedCpuArch].label);
        onChange && onChange(selectedCpuArch);
    }, [setOpen, setValue, onChange]);
    react_1.default.useEffect(() => {
        const isCurrentDefaultCpuArchitectureSelected = isCurrentDefaultCpuArchitecture(cpuArchitectures, selectedCpuArchitecture, day1CpuArchitecture);
        if (isCurrentDefaultCpuArchitectureSelected) {
            setValue((0, common_1.getDefaultCpuArchitecture)());
            setCurrentCpuArch(common_1.architectureData[(0, common_1.getDefaultCpuArchitecture)()].label);
            setOpen(false);
        }
        if (prevVersionRef.current !== openshiftVersion) {
            prevVersionRef.current = openshiftVersion;
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [cpuArchitectures, openshiftVersion, selectedCpuArchitecture]);
    const toggle = (toggleRef) => (react_1.default.createElement(react_core_1.MenuToggle, { id: fieldId, ref: toggleRef, onClick: () => setOpen(!isOpen), isExpanded: isOpen, className: "pf-v6-u-w-100", style: { minWidth: '420px' }, "data-testid": "cpu-architecture-dropdown-toggle" }, currentCpuArch));
    return (react_1.default.createElement(react_core_1.FormGroup, { id: `form-control__${fieldId}`, fieldId: fieldId, label: 'CPU architecture' },
        react_1.default.createElement(react_core_1.Dropdown, Object.assign({}, field, { id: `${fieldId}-dropdown`, toggle: toggle, isOpen: isOpen, onSelect: onSelect, onOpenChange: () => setOpen(!isOpen) }), enabledItems)));
};
exports.default = CpuArchitectureDropdown;
//# sourceMappingURL=CpuArchitectureDropdown.js.map