var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { isWeekend } from "date-fns";
import React, { useCallback, useState } from "react";
import { DayPicker, dateMatchModifiers, } from "react-day-picker";
import { Show } from "../../../primitives/responsive/index.js";
import { omit } from "../../../utils-external/index.js";
import { cl } from "../../../utils/helpers/index.js";
import { useDateLocale } from "../../../utils/i18n/i18n.hooks.js";
import { useDateInputContext } from "../../Date.Input.js";
import { getLocaleFromString } from "../../Date.locale.js";
import { isDateRange } from "../../Date.typeutils.js";
import { clampDisplayMonth, isDateOutsideRange } from "../../date-utils/index.js";
import { pickRangeSelection } from "../../date-utils/pick-range-selection.js";
import { DatePickerDayButton } from "./DatePicker.DayButton.js";
import { DatePickerMonths } from "./DatePicker.Months.js";
import { DatePickerWeekNumber } from "./DatePicker.WeekNumber.js";
/**
 * To support backwards compatibility with the old datepicker,
 * we need to provide a partial implementation of the classnames
 */
const LegacyClassNames = {
    root: "rdp",
    button_next: "button",
    day: "rdp-cell",
    day_button: "rdp-day rdp-button",
    /* We set this directly on DayButton */
    disabled: "",
    hidden: "rdp-day_hidden",
    outside: "rdp-day_outside",
    selected: "rdp-day_selected",
    weekday: "rdp-head_cell",
    weekdays: "rdp-head_row",
    week: "rdp-row",
    weeks: "rdp-tbody",
    month_grid: "rdp-table",
    week_number: "rdp-weeknumber",
};
const ReactDayPicker = (_a) => {
    var { className, dropdownCaption, disabled = [], disableWeekends = false, showWeekNumber = false, selected, fixedWeeks = false, onWeekNumberClick, fromDate, toDate, month, mode: _mode, handleSelect, locale: _locale, popupLabelId } = _a, rest = __rest(_a, ["className", "dropdownCaption", "disabled", "disableWeekends", "showWeekNumber", "selected", "fixedWeeks", "onWeekNumberClick", "fromDate", "toDate", "month", "mode", "handleSelect", "locale", "popupLabelId"]);
    const langProviderLocale = useDateLocale();
    const locale = _locale ? getLocaleFromString(_locale) : langProviderLocale;
    const mode = _mode !== null && _mode !== void 0 ? _mode : "single";
    const [dayHovering, setDayHovering] = useState();
    const context = useDateInputContext(false);
    return (React.createElement(DayPicker, Object.assign({ captionLayout: dropdownCaption ? "dropdown" : "label", hideNavigation: true, locale: locale, mode: mode, onSelect: (newSelection, newDate) => {
            if (mode !== "range" || !isDateRange(selected)) {
                handleSelect(newSelection);
                return;
            }
            const range = pickRangeSelection({
                caller: context === null || context === void 0 ? void 0 : context.caller,
                currentSelection: selected,
                newDate,
                newSelection,
            });
            handleSelect(range);
        }, selected: selected, classNames: LegacyClassNames, components: {
            MonthCaption: () => React.createElement(React.Fragment, null),
            DayButton: useCallback((props) => React.createElement(DatePickerDayButton, Object.assign({}, props, { locale: locale })), [locale]),
            Month: useCallback((props) => (React.createElement(DatePickerMonths, Object.assign({}, props, { locale: locale, onWeekNumberClick: mode === "multiple" ? onWeekNumberClick : undefined, popupLabelId: popupLabelId }))), [locale, mode, onWeekNumberClick, popupLabelId]),
            Day: useCallback((props) => (React.createElement("td", Object.assign({}, omit(props, ["day", "modifiers"]), { className: "rdp-cell", role: undefined }))), []),
            WeekNumber: useCallback((props) => (React.createElement(DatePickerWeekNumber, Object.assign({}, props, { showOnDesktop: true, onWeekNumberClick: mode === "multiple" ? onWeekNumberClick : undefined }))), [mode, onWeekNumberClick]),
            /* On smaller screens we hide it to accomedate our custom week-selector */
            WeekNumberHeader: useCallback((props) => (React.createElement(Show, { above: "sm", asChild: true },
                React.createElement("th", Object.assign({}, props)))), []),
            Weekdays: useCallback((props) => (React.createElement("thead", { className: "rdp-head", "aria-hidden": true },
                React.createElement("tr", Object.assign({}, props)))), []),
        }, className: cl("aksel-date", className), disabled: (day) => (disableWeekends && isWeekend(day)) ||
            dateMatchModifiers(day, disabled) ||
            isDateOutsideRange({ day, fromDate, toDate }), weekStartsOn: 1, modifiers: {
            weekend: (day) => disableWeekends && isWeekend(day),
            hoverRange: (day) => {
                if (!isDateRange(selected) ||
                    !selected.from ||
                    (selected.from && selected.to) ||
                    !dayHovering) {
                    return false;
                }
                const dayTime = day.getTime();
                const fromTime = selected.from.getTime();
                const hoverTime = dayHovering.getTime();
                // Hovering after the start date
                if (hoverTime > fromTime) {
                    return dayTime > fromTime && dayTime < hoverTime;
                }
                // Hovering before the start date
                return dayTime < fromTime && dayTime > hoverTime;
            },
        }, onDayMouseEnter: setDayHovering, onDayMouseLeave: () => setDayHovering(undefined), 
        // eslint-disable-next-line jsx-a11y/no-autofocus
        autoFocus: false, showWeekNumber: showWeekNumber, fixedWeeks: fixedWeeks, showOutsideDays: true, startMonth: fromDate, endMonth: toDate, month: clampDisplayMonth({ month, start: fromDate, end: toDate }), 
        /* We handle this logic manually in `onSelect` */
        resetOnSelect: true }, omit(rest, ["onSelect", "role", "id", "defaultSelected"]))));
};
export { ReactDayPicker };
//# sourceMappingURL=DatePicker.RDP.js.map