"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DayPicker = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const addMonths_1 = require("@astral/utils/date/addMonths");
const addYears_1 = require("@astral/utils/date/addYears");
const react_1 = require("react");
const ConfigProvider_1 = require("../../ConfigProvider");
const useLocaleDateTimeFormat_1 = require("../../useLocaleDateTimeFormat");
const constants_1 = require("../constants");
const DateCalendar_1 = require("../DateCalendar");
const useCalendarNavigate_1 = require("../hooks/useCalendarNavigate");
const MinMaxDateContext_1 = require("../MinMaxDateContext");
const PopoverHoveredContext_1 = require("../PopoverHoveredContext");
const DayPicker = ({ date: initialDate, selectedDate, onChange, isMondayFirst, rangeDate, onDayHover, hoveredDayDate, isDoubleNavigationButtonsEnabled, ...headProps }) => {
    const monthYearFormat = (0, useLocaleDateTimeFormat_1.useLocaleDateTimeFormat)({
        month: 'long',
        year: 'numeric',
    });
    const dayFormat = (0, useLocaleDateTimeFormat_1.useLocaleDateTimeFormat)({
        weekday: 'short',
        year: '2-digit',
        month: 'short',
        day: '2-digit',
        timeZone: 'UTC',
    });
    const { month: monthCaption, year: yearCaption } = (0, react_1.useContext)(ConfigProvider_1.ConfigContext).datePickerLanguageMap;
    const { baseDate, handlePrevClick, handleNextClick, handleDoubleNextClick, handleDoublePrevClick, } = (0, useCalendarNavigate_1.useCalendarNavigate)({
        date: initialDate,
        addCb: addMonths_1.addMonths,
        addDoubleCb: isDoubleNavigationButtonsEnabled ? addYears_1.addYears : undefined,
    });
    const { popoverHovered } = (0, react_1.useContext)(PopoverHoveredContext_1.PopoverHoveredContext);
    const { maxDate, minDate } = (0, react_1.useContext)(MinMaxDateContext_1.MinMaxDateContext);
    /*
    Убираем hover day из стейта, если курсор вышел за проделы popover
     */
    (0, react_1.useEffect)(() => {
        if (!popoverHovered) {
            onDayHover?.();
        }
    }, [onDayHover, popoverHovered]);
    const renderDayTooltipTitle = (0, react_1.useCallback)(({ date }) => {
        return dayFormat(date);
    }, [dayFormat]);
    const selectedRanges = (0, react_1.useMemo)(() => {
        if (!selectedDate || !rangeDate) {
            return null;
        }
        return [{ dateA: selectedDate, dateB: rangeDate }];
    }, [selectedDate, rangeDate]);
    return ((0, jsx_runtime_1.jsxs)(DateCalendar_1.DateCalendarWrapper, { children: [(0, jsx_runtime_1.jsx)(DateCalendar_1.DateCalendarHead, { ...headProps, arrowPostfixTitle: monthCaption.single, doubleArrowPostfixTitle: yearCaption.single, onPrevClick: handlePrevClick, onDoublePrevClick: handleDoublePrevClick, onNextClick: handleNextClick, onDoubleNextClick: handleDoubleNextClick, headBtnText: monthYearFormat(baseDate) }), (0, jsx_runtime_1.jsx)(DateCalendar_1.StaticDaysCalendarWrapper, { className: constants_1.datePickerClassnames.daysContainer, renderDayTooltipTitle: renderDayTooltipTitle, minDate: minDate, maxDate: maxDate, selectedDate: selectedDate, baseDate: baseDate, onDayHover: onDayHover, hoveredDate: hoveredDayDate, selectedRanges: selectedRanges, onChange: onChange, isMondayFirst: isMondayFirst, checkTooltipActive: () => false })] }));
};
exports.DayPicker = DayPicker;
