"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useCalendarDateChange", {
    enumerable: true,
    get: function() {
        return useCalendarDateChange;
    }
});
const _react = require("react");
const _types = require("../store/types");
const _reducer = require("../store/reducer");
const useCalendarDateChange = ({ type, onChangeValue, onSelectIndexes, dispatch })=>{
    const handleOnChangeDay = (0, _react.useCallback)((newDate, coord)=>{
        const newDay = new Date(newDate.year, newDate.monthIndex, newDate.day);
        onChangeValue?.(newDay);
        onSelectIndexes(coord);
    }, [
        onChangeValue,
        onSelectIndexes
    ]);
    const handleOnChangeQuarter = (0, _react.useCallback)((newDate, coord, quarterName)=>{
        const newQuarterDate = new Date(newDate.year, newDate.monthIndex, newDate.day);
        const endQuarter = new Date(newQuarterDate);
        endQuarter.setMonth(newQuarterDate.getMonth() + 3);
        endQuarter.setDate(0);
        onChangeValue?.(newQuarterDate, {
            name: quarterName,
            fullValue: [
                newQuarterDate,
                endQuarter
            ]
        });
        onSelectIndexes(coord);
    }, [
        onChangeValue,
        onSelectIndexes,
        type
    ]);
    const handleOnChangeMonth = (0, _react.useCallback)((newDate, coord)=>{
        if (type === _types.CalendarState.Months) {
            const newMonthDate = new Date(newDate.year, newDate.monthIndex, newDate.day);
            onChangeValue?.(newMonthDate);
            onSelectIndexes(coord);
            return;
        }
        dispatch({
            type: _types.ActionType.UPDATE_MONTH,
            payload: {
                calendarState: _types.CalendarState.Days,
                monthIndex: newDate.monthIndex,
                year: newDate.year,
                size: _reducer.sizeMap.Days.double
            }
        });
    }, [
        onChangeValue,
        onSelectIndexes,
        type
    ]);
    const handleOnChangeYear = (0, _react.useCallback)((newDate, coord)=>{
        if (type === _types.CalendarState.Years) {
            const newYearDate = new Date(newDate.year, newDate.monthIndex, newDate.day);
            onChangeValue?.(newYearDate);
            onSelectIndexes(coord);
            return;
        }
        if (type === _types.CalendarState.Quarters) {
            dispatch({
                type: _types.ActionType.UPDATE_YEAR,
                payload: {
                    calendarState: _types.CalendarState.Quarters,
                    year: newDate.year,
                    size: _reducer.sizeMap.Quarters.double
                }
            });
            return;
        }
        dispatch({
            type: _types.ActionType.UPDATE_YEAR,
            payload: {
                calendarState: _types.CalendarState.Months,
                year: newDate.year,
                size: _reducer.sizeMap.Months.double
            }
        });
    }, [
        onChangeValue,
        onSelectIndexes,
        type
    ]);
    const handleUpdateCalendarState = (0, _react.useCallback)((newCalendarState, newSize)=>{
        dispatch({
            type: _types.ActionType.UPDATE_CALENDAR_STATE,
            payload: {
                calendarState: newCalendarState,
                size: newSize
            }
        });
    }, []);
    return {
        handleOnChangeDay,
        handleOnChangeMonth,
        handleOnChangeQuarter,
        handleOnChangeYear,
        handleUpdateCalendarState
    };
};
