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