"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenShiftVersionDropdown = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const formik_1 = require("formik");
const formik_2 = require("./formik");
const ExternalLink_1 = tslib_1.__importDefault(require("./ExternalLink"));
const config_1 = require("../../config");
const utils_1 = require("./utils");
const getParsedVersions = (items) => {
    const versionsY = Array.from(new Set(items.map((val) => { var _a; return (_a = val.value.match(/^\d+\.(\d+)/)) === null || _a === void 0 ? void 0 : _a[1]; })));
    const parsedVersions = versionsY.map((y) => ({
        y: y,
        versions: items.filter((val) => { var _a; return ((_a = val.value.match(/^\d+\.(\d+)/)) === null || _a === void 0 ? void 0 : _a[1]) === y; }),
    }));
    return parsedVersions.reverse();
};
const OpenShiftVersionDropdown = ({ name, versions, getHelperText, showReleasesLink, showOpenshiftVersionModal, customVersion, }) => {
    const [field, , { setValue }] = (0, formik_1.useField)(name);
    const [isOpen, setOpen] = react_1.default.useState(false);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const fieldId = (0, formik_2.getFieldId)(name, 'input');
    const current = (customVersion ? [...versions, customVersion] : versions).find((item) => item.value === field.value);
    react_1.default.useEffect(() => {
        if (!field.value) {
            const defaultVersion = versions.find((item) => item.default) || versions[0];
            if (defaultVersion) {
                setValue(defaultVersion.value);
            }
        }
    }, [field.value, versions, setValue]);
    const parsedVersions = getParsedVersions(versions);
    let lastY = '';
    const dropdownItems = parsedVersions.map(({ y, versions }) => {
        const items = versions.map((version) => (react_1.default.createElement(react_core_1.DropdownItem, { key: version.value, id: version.value, value: version.value }, (0, utils_1.getVersionLabel)(version, t))));
        if (lastY !== null && y !== lastY) {
            items.push(react_1.default.createElement(react_core_1.Divider, { key: `separator-${y || ''}` }));
        }
        lastY = y;
        return items;
    });
    const dropdownGroup = [
        dropdownItems.length && (react_1.default.createElement(react_core_1.DropdownGroup, { label: "Latest releases", key: "latest-releases" }, dropdownItems)),
        customVersion && (react_1.default.createElement(react_core_1.DropdownGroup, { label: "Custom releases", key: "custom-releases" },
            react_1.default.createElement(react_core_1.DropdownItem, { key: customVersion.value, id: customVersion.value, value: customVersion.value }, (0, utils_1.getVersionLabel)(customVersion, t)))),
        react_1.default.createElement(react_core_1.DropdownGroup, { key: "all-available-versions" },
            react_1.default.createElement(react_core_1.DropdownItem, { key: "all-versions", id: "all-versions", value: "all-versions" },
                react_1.default.createElement("div", { className: "pf-v6-u-text-color-link" }, t('ai:Show all available versions')))),
    ].filter(Boolean);
    const dropdownToggle = (toggleRef) => (react_1.default.createElement(react_core_1.MenuToggle, { id: fieldId, className: "pf-v6-u-w-100", style: { minWidth: '420px' }, ref: toggleRef, isFullWidth: true, onClick: () => setOpen(!isOpen), isExpanded: isOpen, "data-testid": "openshift-version-dropdown-toggle" }, current ? (0, utils_1.getVersionLabel)(current, t) : t('ai:OpenShift version')));
    const helperText = getHelperText && getHelperText(field.value);
    const onSelect = (_, val) => {
        if (val === 'all-versions') {
            showOpenshiftVersionModal();
        }
        else if (val) {
            setValue(val);
        }
        setOpen(false);
    };
    return (react_1.default.createElement(react_core_1.FormGroup, { id: `form-control__${fieldId}`, fieldId: fieldId, label: t('ai:OpenShift version'), isRequired: true },
        react_1.default.createElement(react_core_1.Dropdown, Object.assign({}, field, { id: `${fieldId}-dropdown`, shouldFocusToggleOnSelect: true, isOpen: isOpen, onSelect: onSelect, onOpenChange: () => setOpen(!isOpen), toggle: dropdownToggle }), dropdownGroup),
        helperText && (react_1.default.createElement(react_core_1.FormHelperText, null,
            react_1.default.createElement(react_core_1.HelperText, null,
                react_1.default.createElement(react_core_1.HelperTextItem, { variant: "default", "data-testid": "openshift-version-dropdown-helper-text" }, helperText)))),
        showReleasesLink && (react_1.default.createElement("div", { className: "pf-v6-u-mt-sm" },
            react_1.default.createElement(ExternalLink_1.default, { href: `${window.location.origin}/${config_1.OCP_RELEASES_PAGE}` },
                react_1.default.createElement("span", { "data-ouia-id": "openshift-releases-link" }, t('ai:Learn more about OpenShift releases')))))));
};
exports.OpenShiftVersionDropdown = OpenShiftVersionDropdown;
//# sourceMappingURL=OpenShiftVersionDropdown.js.map