"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactDayPicker = void 0;
const date_fns_1 = require("date-fns");
const react_1 = __importStar(require("react"));
const react_day_picker_1 = require("react-day-picker");
const responsive_1 = require("../../../primitives/responsive");
const utils_external_1 = require("../../../utils-external");
const helpers_1 = require("../../../utils/helpers");
const i18n_hooks_1 = require("../../../utils/i18n/i18n.hooks");
const Date_Input_1 = require("../../Date.Input");
const Date_locale_1 = require("../../Date.locale");
const Date_typeutils_1 = require("../../Date.typeutils");
const date_utils_1 = require("../../date-utils");
const pick_range_selection_1 = require("../../date-utils/pick-range-selection");
const DatePicker_DayButton_1 = require("./DatePicker.DayButton");
const DatePicker_Months_1 = require("./DatePicker.Months");
const DatePicker_WeekNumber_1 = require("./DatePicker.WeekNumber");
/**
 * 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 = (0, i18n_hooks_1.useDateLocale)();
    const locale = _locale ? (0, Date_locale_1.getLocaleFromString)(_locale) : langProviderLocale;
    const mode = _mode !== null && _mode !== void 0 ? _mode : "single";
    const [dayHovering, setDayHovering] = (0, react_1.useState)();
    const context = (0, Date_Input_1.useDateInputContext)(false);
    return (react_1.default.createElement(react_day_picker_1.DayPicker, Object.assign({ captionLayout: dropdownCaption ? "dropdown" : "label", hideNavigation: true, locale: locale, mode: mode, onSelect: (newSelection, newDate) => {
            if (mode !== "range" || !(0, Date_typeutils_1.isDateRange)(selected)) {
                handleSelect(newSelection);
                return;
            }
            const range = (0, pick_range_selection_1.pickRangeSelection)({
                caller: context === null || context === void 0 ? void 0 : context.caller,
                currentSelection: selected,
                newDate,
                newSelection,
            });
            handleSelect(range);
        }, selected: selected, classNames: LegacyClassNames, components: {
            MonthCaption: () => react_1.default.createElement(react_1.default.Fragment, null),
            DayButton: (0, react_1.useCallback)((props) => react_1.default.createElement(DatePicker_DayButton_1.DatePickerDayButton, Object.assign({}, props, { locale: locale })), [locale]),
            Month: (0, react_1.useCallback)((props) => (react_1.default.createElement(DatePicker_Months_1.DatePickerMonths, Object.assign({}, props, { locale: locale, onWeekNumberClick: mode === "multiple" ? onWeekNumberClick : undefined, popupLabelId: popupLabelId }))), [locale, mode, onWeekNumberClick, popupLabelId]),
            Day: (0, react_1.useCallback)((props) => (react_1.default.createElement("td", Object.assign({}, (0, utils_external_1.omit)(props, ["day", "modifiers"]), { className: "rdp-cell", role: undefined }))), []),
            WeekNumber: (0, react_1.useCallback)((props) => (react_1.default.createElement(DatePicker_WeekNumber_1.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: (0, react_1.useCallback)((props) => (react_1.default.createElement(responsive_1.Show, { above: "sm", asChild: true },
                react_1.default.createElement("th", Object.assign({}, props)))), []),
            Weekdays: (0, react_1.useCallback)((props) => (react_1.default.createElement("thead", { className: "rdp-head", "aria-hidden": true },
                react_1.default.createElement("tr", Object.assign({}, props)))), []),
        }, className: (0, helpers_1.cl)("aksel-date", className), disabled: (day) => (disableWeekends && (0, date_fns_1.isWeekend)(day)) ||
            (0, react_day_picker_1.dateMatchModifiers)(day, disabled) ||
            (0, date_utils_1.isDateOutsideRange)({ day, fromDate, toDate }), weekStartsOn: 1, modifiers: {
            weekend: (day) => disableWeekends && (0, date_fns_1.isWeekend)(day),
            hoverRange: (day) => {
                if (!(0, Date_typeutils_1.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: (0, date_utils_1.clampDisplayMonth)({ month, start: fromDate, end: toDate }), 
        /* We handle this logic manually in `onSelect` */
        resetOnSelect: true }, (0, utils_external_1.omit)(rest, ["onSelect", "role", "id", "defaultSelected"]))));
};
exports.ReactDayPicker = ReactDayPicker;
//# sourceMappingURL=DatePicker.RDP.js.map