import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2026 GoodData Corporation
import cx from "classnames";
import { useIdPrefixed } from "../utils/useId.js";
import { RECURRENCE_TYPES } from "./constants.js";
import { CronExpression } from "./CronExpression.js";
import { CronExpressionSuggestion } from "./CronExpressionSuggestion.js";
import { RepeatTypeDescription } from "./RepeatTypeDescription.js";
import { RepeatTypeSelect } from "./RepeatTypeSelect.js";
import { useCronValidation } from "./useCronValidation.js";
/**
 * @internal
 *
 * Recurrence component is used to select recurrence type and set cron expression.
 * @param props - IRecurrenceProps
 */
export function Recurrence({ label, recurrenceType, inheritRecurrenceType, startDate, cronValue, cronPlaceholder, cronDescription, timezone, onRepeatTypeChange, onCronValueChange, allowHourlyRecurrence, showTimezoneInOccurrence, showRepeatTypeDescription, showInheritValue, isWhiteLabeled, weekStart = "Sunday", onRecurrenceDropdownOpen, closeOnParentScroll, }) {
    const { cronError, handleChange, handleOnBlur } = useCronValidation({
        allowHourlyRecurrence: allowHourlyRecurrence ?? false,
        onCronValueChange,
    });
    const labelId = useIdPrefixed("label");
    const errorId = useIdPrefixed("error");
    const recurrenceFormClasses = cx("gd-recurrence-form-repeat", "gd-input-component", {
        "gd-recurrence-form-repeat-cron": recurrenceType === RECURRENCE_TYPES.CRON,
    });
    const isInherit = recurrenceType === RECURRENCE_TYPES.INHERIT;
    const isCron = recurrenceType === RECURRENCE_TYPES.CRON ||
        (isInherit && inheritRecurrenceType === RECURRENCE_TYPES.CRON);
    const isSpecified = recurrenceType !== RECURRENCE_TYPES.CRON ||
        (isInherit && inheritRecurrenceType !== RECURRENCE_TYPES.CRON);
    return (_jsxs(_Fragment, { children: [
            _jsxs("div", { className: recurrenceFormClasses, children: [label ? (_jsx("label", { htmlFor: labelId, className: "gd-label", children: label })) : null, _jsxs("div", { className: "gd-recurrence-form-repeat-inner", children: [
                            _jsx(RepeatTypeSelect, { id: labelId, repeatType: recurrenceType, startDate: startDate, onChange: onRepeatTypeChange, allowHourlyRecurrence: allowHourlyRecurrence, showInheritValue: showInheritValue, onRepeatDropdownOpen: onRecurrenceDropdownOpen, closeOnParentScroll: closeOnParentScroll }), isSpecified && showRepeatTypeDescription ? (_jsx(RepeatTypeDescription, { repeatType: isInherit ? (inheritRecurrenceType ?? "") : recurrenceType, startDate: startDate, weekStart: weekStart, timezone: timezone ?? "", showTimezone: Boolean(showTimezoneInOccurrence && !isInherit) })) : null, isCron ? (_jsx(CronExpression, { expression: isInherit ? (cronPlaceholder ?? "") : (cronValue ?? ""), description: cronDescription, onChange: handleChange, allowHourlyRecurrence: allowHourlyRecurrence, timezone: timezone ?? "", showTimezone: showTimezoneInOccurrence, validationError: cronError ?? undefined, onBlur: handleOnBlur, accessibilityConfig: {
                                    ariaDescribedBy: cronError ? errorId : undefined,
                                    ariaLabelledBy: labelId,
                                }, disabled: isInherit })) : null] })
                ] }), isInherit ? null : (_jsx(CronExpressionSuggestion, { errorId: errorId, validationError: cronError, recurrenceType: recurrenceType, isWhiteLabeled: isWhiteLabeled }))] }));
}
