import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2025 GoodData Corporation
import { useCallback, useState } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { IntlWrapper } from "@gooddata/sdk-ui";
import { DEFAULT_DATE_FORMAT, DEFAULT_LOCALE, DEFAULT_TIME_FORMAT, DEFAULT_WEEK_START, RECURRENCE_TYPES, } from "./constants.js";
import { DateTime } from "./DateTime.js";
import { messages } from "./locales.js";
import { Recurrence } from "./Recurrence.js";
import { constructCronExpression, isCronExpressionValid, transformCronExpressionToRecurrenceType, } from "./utils/utils.js";
function RecurrenceFormCore({ startDate = null, cronExpression = "", cronDescription = "", placeholder, onChange, locale = DEFAULT_LOCALE, dateFormat = DEFAULT_DATE_FORMAT, timeFormat = DEFAULT_TIME_FORMAT, weekStart = DEFAULT_WEEK_START, timezone, startLabel, repeatLabel, className, allowHourlyRecurrence = true, showRepeatTypeDescription, showTimezoneInOccurrence, showInheritValue, isWhiteLabeled, onRecurrenceDropdownOpen, closeDropdownsOnParentScroll, onKeyDownSubmit, customRecurrenceTypeMappingFn, }) {
    const intl = useIntl();
    const mapRecurrenceType = customRecurrenceTypeMappingFn ?? transformCronExpressionToRecurrenceType;
    const [dateValue, setDateValue] = useState(startDate);
    const [cronValue, setCronValue] = useState(cronExpression);
    const [recurrenceType, setRecurrenceType] = useState(mapRecurrenceType(dateValue, cronExpression, allowHourlyRecurrence ?? false, showInheritValue ?? false, weekStart));
    const [inheritRecurrenceType] = useState(mapRecurrenceType(dateValue, placeholder, allowHourlyRecurrence, false, weekStart));
    const onDateChange = useCallback((date, valid) => {
        setDateValue(date);
        const newExpression = constructCronExpression(date, recurrenceType, cronExpression ?? "", weekStart ?? "Sunday") ??
            "";
        onChange(newExpression, date, valid && isCronExpressionValid(newExpression, allowHourlyRecurrence));
    }, [cronExpression, onChange, recurrenceType, weekStart, allowHourlyRecurrence]);
    const onRepeatTypeChange = useCallback((type) => {
        setRecurrenceType(type);
        const newExpression = constructCronExpression(dateValue, type, cronValue ?? "", weekStart ?? "Sunday") ?? "";
        onChange(newExpression, dateValue, type === RECURRENCE_TYPES.INHERIT
            ? true
            : isCronExpressionValid(newExpression, allowHourlyRecurrence));
    }, [cronValue, dateValue, onChange, weekStart, allowHourlyRecurrence]);
    const onCronValueChange = useCallback((cron, isValid) => {
        setCronValue(cron);
        onChange(cron, dateValue, isValid);
    }, [dateValue, onChange]);
    return (_jsxs("div", { className: cx("gd-recurrence-form s-recurrence-form", className), children: [Boolean(startDate) && (_jsx(DateTime, { label: startLabel ?? intl.formatMessage({ id: messages["starts"].id }), date: dateValue, onDateChange: onDateChange, locale: locale, dateFormat: dateFormat, timezone: timezone, weekStart: weekStart, timeFormat: timeFormat, closeOnParentScroll: closeDropdownsOnParentScroll, onKeyDownSubmit: onKeyDownSubmit })), _jsx(Recurrence, { label: repeatLabel ?? intl.formatMessage({ id: messages["repeats"].id }), showRepeatTypeDescription: showRepeatTypeDescription, recurrenceType: recurrenceType, inheritRecurrenceType: inheritRecurrenceType, startDate: dateValue, cronValue: cronValue, cronPlaceholder: placeholder, cronDescription: cronDescription, onRepeatTypeChange: onRepeatTypeChange, onCronValueChange: onCronValueChange, allowHourlyRecurrence: allowHourlyRecurrence, weekStart: weekStart, timezone: timezone, showInheritValue: showInheritValue, showTimezoneInOccurrence: showTimezoneInOccurrence, onRecurrenceDropdownOpen: onRecurrenceDropdownOpen, isWhiteLabeled: isWhiteLabeled, closeOnParentScroll: closeDropdownsOnParentScroll })
        ] }));
}
/**
 * @internal
 */
export function RecurrenceForm(props) {
    return (_jsx(IntlWrapper, { locale: props.locale, children: _jsx(RecurrenceFormCore, { ...props }) }));
}
