"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenShiftSelectWithSearch = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const formik_1 = require("formik");
const hooks_1 = require("../../hooks");
const formik_2 = require("./formik");
const utils_1 = require("./utils");
const OpenShiftSelectWithSearch = ({ versions, getHelperText, }) => {
    var _a;
    const { t } = (0, hooks_1.useTranslation)();
    const [{ value }] = (0, formik_1.useField)('customOpenshiftSelect');
    const [filterValue, setFilterValue] = react_1.default.useState('');
    const selectOptions = react_1.default.useMemo(() => {
        const options = versions
            .map((version) => ({
            children: (0, utils_1.getVersionLabel)(version, t),
            value: version.value,
        }))
            .filter((v) => filterValue ? String(v.children).toLowerCase().includes(filterValue.toLowerCase()) : true);
        if (!options.length) {
            return [
                {
                    isDisabled: true,
                    children: t('ai:No results found for {{filter}}', { filter: filterValue }),
                    value: 'no_results',
                },
            ];
        }
        return options.map((option, index) => {
            const match = option.value.match(/\d+\.(\d+)\.\d+/);
            const y = match ? match[1] : null;
            const previousY = index > 0 ? (options[index - 1].value.match(/\d+\.(\d+)\.\d+/) || [])[1] : null;
            return Object.assign(Object.assign({}, option), { showDivider: previousY !== null && y !== previousY });
        });
    }, [filterValue, versions, t]);
    return (react_1.default.createElement(formik_2.SelectFieldWithSearch, { selectOptions: selectOptions, filterValue: filterValue, setFilterValue: setFilterValue, name: 'customOpenshiftSelect', helperText: (_a = getHelperText === null || getHelperText === void 0 ? void 0 : getHelperText(value, true)) !== null && _a !== void 0 ? _a : t('ai:Select an OpenShift version from the list or use the type ahead to narrow down the list.') }));
};
exports.OpenShiftSelectWithSearch = OpenShiftSelectWithSearch;
//# sourceMappingURL=OpenShiftSelectWithSearch.js.map