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