import { jsx as _jsx } from "react/jsx-runtime";
import { useIntl } from "react-intl";
import { invariant } from "ts-invariant";
import { UiListbox } from "../@ui/UiListbox/UiListbox.js";
import { Dropdown } from "../Dropdown/Dropdown.js";
import { DropdownButton } from "../Dropdown/DropdownButton.js";
import { SingleSelectListItem } from "../List/ListItem.js";
import { DEFAULT_DROPDOWN_ALIGN_POINTS, DEFAULT_DROPDOWN_WIDTH, DEFAULT_DROPDOWN_ZINDEX, RECURRENCE_TYPES, } from "./constants.js";
import { messages } from "./locales.js";
import { getIntlDayName, getWeekNumber, isLastOccurrenceOfWeekdayInMonth } from "./utils/utils.js";
const getLocalizationKey = (id) => {
    switch (id) {
        case RECURRENCE_TYPES.HOURLY:
            return {
                title: messages["recurrence_hourly"],
            };
        case RECURRENCE_TYPES.DAILY:
            return {
                title: messages["recurrence_daily"],
            };
        case RECURRENCE_TYPES.WEEKLY:
            return {
                title: messages["recurrence_weekly"],
            };
        case RECURRENCE_TYPES.MONTHLY:
            return {
                title: messages["recurrence_monthly"],
            };
        case RECURRENCE_TYPES.CRON:
            return {
                title: messages["recurrence_cron"],
            };
        case RECURRENCE_TYPES.INHERIT:
            return {
                title: messages["recurrence_inherit"],
                info: messages["recurrence_inherit_info"],
            };
        default:
            throw new Error("Invariant: Unexpected localization key.");
    }
};
const getRepeatItems = (intl, startDate, allowHourlyRecurrence, showInheritValue) => {
    const isLastOccurrenceOfWeekDay = isLastOccurrenceOfWeekdayInMonth(startDate);
    const recurrenceTypes = [
        ...(showInheritValue ? [RECURRENCE_TYPES.INHERIT] : []),
        ...(allowHourlyRecurrence ? [RECURRENCE_TYPES.HOURLY] : []),
        RECURRENCE_TYPES.DAILY,
        RECURRENCE_TYPES.WEEKLY,
        RECURRENCE_TYPES.MONTHLY,
        RECURRENCE_TYPES.CRON,
    ];
    return recurrenceTypes.map((id) => {
        const { title, info } = getLocalizationKey(id);
        if (id === RECURRENCE_TYPES.MONTHLY && !startDate) {
            return {
                id,
                title: intl.formatMessage(messages["recurrence_monthly_first"]),
            };
        }
        if (id === RECURRENCE_TYPES.WEEKLY && !startDate) {
            return {
                id,
                title: intl.formatMessage(messages["recurrence_weekly_first"]),
            };
        }
        if (id === RECURRENCE_TYPES.MONTHLY && isLastOccurrenceOfWeekDay) {
            return {
                id,
                title: intl.formatMessage(messages["recurrence_monthly_last"], {
                    day: getIntlDayName(intl, startDate),
                }),
            };
        }
        return {
            id,
            title: intl.formatMessage(title, {
                day: getIntlDayName(intl, startDate),
                week: getWeekNumber(startDate),
            }),
            info: info ? intl.formatMessage(info) : undefined,
        };
    });
};
export function RepeatTypeSelect({ id, onChange, repeatType, startDate = null, allowHourlyRecurrence, showInheritValue, onRepeatDropdownOpen, closeOnParentScroll, }) {
    const intl = useIntl();
    const repeatItems = getRepeatItems(intl, startDate, allowHourlyRecurrence, showInheritValue);
    const repeatTypeItem = repeatItems.find((item) => item.id === repeatType);
    invariant(repeatTypeItem, "Inconsistent state in RepeatTypeSelect");
    return (_jsx(Dropdown, { closeOnParentScroll: closeOnParentScroll, alignPoints: DEFAULT_DROPDOWN_ALIGN_POINTS, className: "gd-recurrence-form-type s-recurrence-form-type", autofocusOnOpen: true, renderButton: ({ toggleDropdown, isOpen, dropdownId, buttonRef }) => (_jsx(DropdownButton, { id: id, value: repeatTypeItem.title, onClick: () => {
                if (!isOpen) {
                    onRepeatDropdownOpen?.();
                }
                toggleDropdown();
            }, dropdownId: dropdownId, isOpen: isOpen, buttonRef: buttonRef, accessibilityConfig: {
                popupType: "listbox",
            } })), renderBody: ({ closeDropdown, ariaAttributes }) => {
            const listboxItems = repeatItems.map((item) => ({
                type: "interactive",
                id: item.id,
                stringTitle: item.title,
                data: item,
            }));
            const handleKeyDown = (e) => {
                if (e.key !== "Tab") {
                    return;
                }
                closeDropdown();
            };
            return (_jsx(UiListbox, { shouldKeyboardActionStopPropagation: true, shouldKeyboardActionPreventDefault: true, dataTestId: "s-recurrence-form-type-list", items: listboxItems, maxWidth: DEFAULT_DROPDOWN_WIDTH, selectedItemId: repeatType, onSelect: (item) => {
                    onChange(item.id);
                }, onUnhandledKeyDown: handleKeyDown, onClose: closeDropdown, ariaAttributes: ariaAttributes, InteractiveItemComponent: ({ item, isSelected, onSelect, isFocused }) => {
                    return (_jsx(SingleSelectListItem, { title: item.stringTitle, info: item.data.info, isSelected: isSelected, isFocused: isFocused, onClick: onSelect, className: "gd-recurrence-form-list-item" }));
                } }));
        }, overlayPositionType: "sameAsTarget", overlayZIndex: DEFAULT_DROPDOWN_ZINDEX }));
}
