"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CalendarDays", {
    enumerable: true,
    get: function() {
        return CalendarDays;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _hooks = require("../../hooks");
const _utils = require("../../utils");
const _DateStructureItem = require("../DateStructureItem/DateStructureItem");
const _Calendartokens = require("../../Calendar.tokens");
const _CalendarDaysstyles = require("./CalendarDays.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const CalendarDays = ({ date: currentDate, value, eventList, disabledList, min, max, includeEdgeDates, hoveredDay, selectIndexes, isDouble, isSecond, outerRefs, onChangeDay, onHoverDay, onTriggerEventTooltip, onSetSelected, onKeyDown, locale = 'ru' })=>{
    const [days, selected] = (0, _hooks.useDays)({
        date: currentDate,
        value,
        eventList,
        disabledList,
        min,
        max,
        includeEdgeDates
    });
    const selectedRef = (0, _react.useRef)(selected);
    const onSetSelectedRef = (0, _react.useRef)(onSetSelected);
    const noopRef = (0, _react.useRef)(null);
    const offset = isSecond ? _utils.ROW_STEP : 0;
    const dayItemTokens = {
        [`${_Calendartokens.innerTokens.dateStructureFontFamily}`]: `var(${_Calendartokens.tokens.calendarDayFontFamily})`,
        [`${_Calendartokens.innerTokens.dateStructureFontSize}`]: `var(${_Calendartokens.tokens.calendarDayFontSize})`,
        [`${_Calendartokens.innerTokens.dateStructureFontWeight}`]: `var(${_Calendartokens.tokens.calendarDayFontWeight})`,
        [`${_Calendartokens.innerTokens.dateStructureFontLineHeight}`]: `var(${_Calendartokens.tokens.calendarDayFontLineHeight})`,
        [`${_Calendartokens.innerTokens.dateStructureFontLetterSpacing}`]: `var(${_Calendartokens.tokens.calendarDayFontLetterSpacing})`,
        [`${_Calendartokens.innerTokens.dateStructureFontStyle}`]: `var(${_Calendartokens.tokens.calendarDayFontStyle})`,
        [`${_Calendartokens.innerTokens.dateStructureSelectedFontWeight}`]: `var(${_Calendartokens.tokens.calendarDaySelectedFontWeight})`,
        [`${_Calendartokens.innerTokens.dateStructureBorderRadius}`]: `var(${_Calendartokens.tokens.calendarDayItemBorderRadius})`
    };
    const getSelectedDate = (0, _react.useCallback)((event)=>{
        const { day, monthIndex, year } = event.currentTarget.dataset;
        const selectedDate = {
            day: Number(day),
            monthIndex: Number(monthIndex),
            year: Number(year)
        };
        if (!(0, _utils.canSelectDate)(selectedDate, value, disabledList)) {
            return;
        }
        return selectedDate;
    }, [
        disabledList,
        value
    ]);
    const handleOnChangeDay = (0, _react.useCallback)((i, j)=>(event)=>{
            const selectedDate = getSelectedDate(event);
            if (!selectedDate) {
                return;
            }
            onChangeDay(selectedDate, [
                i + offset,
                j
            ]);
            if ((0, _utils.isSelectProcess)(value)) {
                onHoverDay?.(undefined);
            }
        }, [
        getSelectedDate,
        onChangeDay,
        offset,
        value,
        onHoverDay
    ]);
    const handleOnHoverDay = (0, _react.useCallback)((event)=>{
        const selectedDate = getSelectedDate(event);
        const isSelectedDone = Array.isArray(value) && value[0] && value[1];
        if (!selectedDate || !Array.isArray(value) || isSelectedDone) {
            return;
        }
        onHoverDay?.(selectedDate);
    }, [
        getSelectedDate,
        onHoverDay,
        value
    ]);
    const handleMouseOutGrid = ()=>{
        onHoverDay?.(undefined);
        onTriggerEventTooltip?.(noopRef, []);
    };
    const getRefs = (element, isDayInCurrentMonth, i, j)=>{
        if (isDayInCurrentMonth) {
            if (!outerRefs.current[i + offset]) {
                outerRefs.current[i + offset] = [];
            }
            outerRefs.current[i + offset][j] = element;
        }
    };
    (0, _react.useEffect)(()=>{
        if (selectedRef.current) {
            onSetSelectedRef.current?.(selectedRef.current);
        }
    }, []);
    return /*#__PURE__*/ _react.default.createElement(_CalendarDaysstyles.StyledCalendarDays, {
        role: "grid",
        "aria-labelledby": "id-grid-label",
        onKeyDown: onKeyDown,
        onMouseLeave: handleMouseOutGrid
    }, /*#__PURE__*/ _react.default.createElement(_CalendarDaysstyles.StyledCalendarDaysHint, {
        id: "withShift"
    }, _utils.I18N.navigationByShift[locale]), /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, _utils.SHORT_DAY_NAMES[locale].map((name)=>/*#__PURE__*/ _react.default.createElement(_DateStructureItem.DateStructureItem, {
            role: "columnheader",
            "aria-label": _utils.FULL_DAY_NAMES[locale][name],
            key: name,
            dayOfWeek: true,
            day: name,
            style: dayItemTokens
        }, name))), days.map((day, i)=>/*#__PURE__*/ _react.default.createElement(_react.Fragment, {
            key: i
        }, day.map(({ date, events, disabled, isSelected, isCurrent, isDayInCurrentMonth, inRange, isOutOfMinMaxRange = false, disabledArrowKey, disabledDates }, j)=>/*#__PURE__*/ _react.default.createElement(_DateStructureItem.DateStructureItem, {
                ref: (element)=>getRefs(element, Boolean(isDayInCurrentMonth), i, j),
                style: dayItemTokens,
                eventList: events,
                disabled: disabled,
                day: date.day,
                year: date.year,
                monthIndex: date.monthIndex,
                isFocused: i + offset === selectIndexes?.[0] && j === selectIndexes?.[1] && !isOutOfMinMaxRange,
                isSelected: isSelected,
                isCurrent: isCurrent,
                isDayInCurrentMonth: isDayInCurrentMonth,
                isDouble: isDouble,
                isHovered: (0, _utils.isSameDay)(date, hoveredDay),
                inRange: (0, _utils.getInRange)(value, date, hoveredDay, inRange),
                sideInRange: (0, _utils.getSideInRange)(value, date, hoveredDay, isSelected),
                onClick: disabled ? undefined : handleOnChangeDay(i, j),
                onMouseOver: disabled ? undefined : handleOnHoverDay,
                onTriggerEventTooltip: onTriggerEventTooltip,
                key: `StyledDay-${i}-${j}`,
                role: "gridcell",
                disabledArrowKey: disabledArrowKey,
                disabledMonths: disabledDates
            }, date.day)))));
};
