"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Calendar = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const box_1 = __importDefault(require("../box"));
const core_1 = require("../core");
const utils_1 = require("../utils");
const components_1 = require("./components");
const hooks_1 = require("./hooks");
const utils_2 = require("./utils");
/** Displays calendar. */
exports.Calendar = (0, core_1.vui)((props, ref) => {
    const _a = (0, core_1.omitThemingProps)(props), { boundaries, className, fixedNumberOfWeeks = false, disabled, isStartDate, mode, onSelectDates, selectedDates } = _a, rest = __rest(_a, ["boundaries", "className", "fixedNumberOfWeeks", "disabled", "isStartDate", "mode", "onSelectDates", "selectedDates"]);
    const styles = (0, core_1.useStyleConfig)('Calendar', props);
    const currentDateRef = (0, react_1.useRef)((0, utils_2.isFrom)(mode) || (0, utils_2.isExact)(mode) ? selectedDates === null || selectedDates === void 0 ? void 0 : selectedDates.startDate : selectedDates === null || selectedDates === void 0 ? void 0 : selectedDates.endDate);
    /**
     * Effect to reset viewDate when selectedDates changes (onReset)
     */
    (0, react_1.useEffect)(() => {
        currentDateRef.current = (0, utils_2.isFrom)(mode) || (0, utils_2.isExact)(mode) ? selectedDates === null || selectedDates === void 0 ? void 0 : selectedDates.startDate : selectedDates === null || selectedDates === void 0 ? void 0 : selectedDates.endDate;
        currentDateRef.current && setViewDate(currentDateRef.current);
    }, [selectedDates]);
    const calendarRef = (0, react_1.useRef)(null);
    const defaultViewMonth = (0, hooks_1.useDefaultViewMonth)({
        boundaries,
        selectedDate: currentDateRef.current,
        isStartDate,
    });
    const [timeUnitMode, setTimeUnitMode] = (0, react_1.useState)('day');
    const [viewDate, setViewDate] = (0, react_1.useState)(defaultViewMonth);
    /**
     * Handles clicking next/prev in header
     */
    const onChangeViewItem = (direction) => {
        var _a;
        const newViewMonth = (0, utils_2.processViewDate)(viewDate, direction, timeUnitMode, (_a = calendarRef === null || calendarRef === void 0 ? void 0 : calendarRef.current) === null || _a === void 0 ? void 0 : _a.items);
        newViewMonth && setViewDate(newViewMonth);
    };
    /**
     * Handles clicking on a day
     */
    const onSelectDay = (clickedDate) => () => {
        updateDateState(clickedDate);
        setViewDate(clickedDate);
    };
    /**
     * Handles clicking on a month
     */
    const onSelectMonth = (clickedDate) => () => {
        const processedNewDate = (0, utils_2.processSelectMonth)(clickedDate, mode, boundaries);
        updateDateState(processedNewDate);
        setViewDate(processedNewDate);
        setTimeUnitMode('day');
    };
    /**
     * Handles clicking on a year
     */
    const onSelectYear = (clickedDate) => () => {
        const processedNewDate = (0, utils_2.processSelectYear)(clickedDate, mode, boundaries);
        updateDateState(processedNewDate);
        setViewDate(processedNewDate);
        setTimeUnitMode('month');
    };
    /**
     * Send update date to parent component
     */
    const updateDateState = (newDate) => {
        const dateRange = Object.assign({}, selectedDates);
        if (dateRange) {
            if ((0, utils_2.isTo)(mode))
                dateRange.endDate = newDate;
            else
                dateRange.startDate = newDate;
        }
        if (newDate && !disabled)
            onSelectDates && onSelectDates(dateRange);
    };
    const calendar = (calendarRef.current = (0, hooks_1.useCalendar)({
        boundaries,
        mode,
        selectedDates,
        timeUnitMode,
        viewDate,
        fixedNumberOfWeeks,
    }));
    return ((0, jsx_runtime_1.jsxs)(box_1.default, Object.assign({ className: (0, utils_1.cs)('vui-calendar', className), column: true, cursor: "default", minW: 280, position: "relative", ref: ref, w: "fit-content" }, styles, rest, { children: [(0, jsx_runtime_1.jsx)(components_1.CalendarHeader, { nextDisabled: calendar.nextDisabled, onChangeViewItem: onChangeViewItem, onSetTimeUnitMode: (mode) => setTimeUnitMode(mode), prevDisabled: calendar.prevDisabled, timeUnitMode: timeUnitMode, viewMonth: viewDate }), (0, utils_2.isDay)(timeUnitMode) && (0, jsx_runtime_1.jsx)(components_1.DayPicker, { calendar: calendar, onSelectDay: onSelectDay }), (0, utils_2.isMonth)(timeUnitMode) && (0, jsx_runtime_1.jsx)(components_1.MonthPicker, { calendar: calendar, onSelectMonth: onSelectMonth }), (0, utils_2.isYear)(timeUnitMode) && (0, jsx_runtime_1.jsx)(components_1.YearPicker, { calendar: calendar, onSelectYear: onSelectYear })] })));
});
exports.Calendar.displayName = 'Calendar';
exports.default = exports.Calendar;
//# sourceMappingURL=calendar.js.map