import { jsx as _jsx } from "react/jsx-runtime";
import { useMemo, useCallback, useContext } from 'react';
import { startOfMonth, setYear, setDate, setMonth, isSameMonth, parse, } from 'date-fns';
import PanelCell from '../../components/PanelCell';
import PanelContext from '../../context/PanelContext';
import getPanelCellsData from '../../utils/getPanelCellsData';
import { dateConfig } from '../../utils/dateUtils';
var COL_COUNT = 7;
var ROW_COUNT = 6;
var DatePickerBody = function (props) {
    var onHover = useContext(PanelContext).onHover;
    var selected = props.selected, _a = props.popText, popText = _a === void 0 ? '' : _a, _b = props.disableRangeOverView, disableRangeOverView = _b === void 0 ? false : _b, defaultPanelDate = props.defaultPanelDate, rangeDate = props.rangeDate, hoverRangeDate = props.hoverRangeDate, _c = props.row, row = _c === void 0 ? ROW_COUNT : _c, _d = props.col, col = _d === void 0 ? COL_COUNT : _d, showTimeOption = props.showTimeOption, onSelected = props.onSelected, disabledPanelDate = props.disabledPanelDate, showLunarDate = props.showLunarDate;
    var startDateOfMonth = useMemo(function () { return startOfMonth(defaultPanelDate); }, [defaultPanelDate]);
    var cells = useMemo(function () {
        return getPanelCellsData({
            offset: startDateOfMonth.getDay(),
            defaultPanelDate: startDateOfMonth,
            selected: selected,
            disabledPanelDate: disabledPanelDate,
            rangeDate: rangeDate,
            hoverRangeDate: hoverRangeDate,
            row: row,
            col: col,
            dateConfig: dateConfig.date,
            inView: isSameMonth,
            disableRangeOverView: disableRangeOverView,
            showLunarDate: showLunarDate,
        });
    }, [
        disableRangeOverView,
        selected,
        rangeDate,
        hoverRangeDate,
        row,
        col,
        startDateOfMonth,
        disabledPanelDate,
        showLunarDate,
    ]);
    var setSelectedDate = useCallback(function (val) {
        var _a = showTimeOption || {}, defaultTime = _a.defaultTime, format = _a.format;
        var defaultTimeFn = function () {
            return typeof defaultTime === 'function' ? defaultTime(val) : defaultTime;
        };
        if (!selected) {
            return onSelected(defaultTime ? parse(defaultTimeFn(), format, val) : val);
        }
        var selectedDate = selected;
        selectedDate = setYear(selectedDate, val.getFullYear());
        selectedDate = setMonth(selectedDate, val.getMonth());
        selectedDate = setDate(selectedDate, val.getDate());
        onSelected(selectedDate);
    }, [selected, showTimeOption, onSelected]);
    return (_jsx(PanelCell, { col: col, cells: cells, popText: popText, onSelected: setSelectedDate, onHover: onHover }, void 0));
};
export default DatePickerBody;
