import { useCallback } from 'react';
import { CalendarState, ActionType } from '../store/types.js';
import { sizeMap } from '../store/reducer.js';

var useCalendarDateChange = function useCalendarDateChange(_ref) {
  var type = _ref.type,
    onChangeValue = _ref.onChangeValue,
    onSelectIndexes = _ref.onSelectIndexes,
    dispatch = _ref.dispatch;
  var handleOnChangeDay = useCallback(function (newDate, coord) {
    var newDay = new Date(newDate.year, newDate.monthIndex, newDate.day);
    onChangeValue === null || onChangeValue === void 0 || onChangeValue(newDay);
    onSelectIndexes(coord);
  }, [onChangeValue, onSelectIndexes]);
  var handleOnChangeQuarter = 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 || onChangeValue(newQuarterDate, {
      name: quarterName,
      fullValue: [newQuarterDate, endQuarter]
    });
    onSelectIndexes(coord);
  }, [onChangeValue, onSelectIndexes, type]);
  var handleOnChangeMonth = useCallback(function (newDate, coord) {
    if (type === CalendarState.Months) {
      var newMonthDate = new Date(newDate.year, newDate.monthIndex, newDate.day);
      onChangeValue === null || onChangeValue === void 0 || onChangeValue(newMonthDate);
      onSelectIndexes(coord);
      return;
    }
    dispatch({
      type: ActionType.UPDATE_MONTH,
      payload: {
        calendarState: CalendarState.Days,
        monthIndex: newDate.monthIndex,
        year: newDate.year,
        size: sizeMap.Days["double"]
      }
    });
  }, [onChangeValue, onSelectIndexes, type]);
  var handleOnChangeYear = useCallback(function (newDate, coord) {
    if (type === CalendarState.Years) {
      var newYearDate = new Date(newDate.year, newDate.monthIndex, newDate.day);
      onChangeValue === null || onChangeValue === void 0 || onChangeValue(newYearDate);
      onSelectIndexes(coord);
      return;
    }
    if (type === CalendarState.Quarters) {
      dispatch({
        type: ActionType.UPDATE_YEAR,
        payload: {
          calendarState: CalendarState.Quarters,
          year: newDate.year,
          size: sizeMap.Quarters["double"]
        }
      });
      return;
    }
    dispatch({
      type: ActionType.UPDATE_YEAR,
      payload: {
        calendarState: CalendarState.Months,
        year: newDate.year,
        size: sizeMap.Months["double"]
      }
    });
  }, [onChangeValue, onSelectIndexes, type]);
  var handleUpdateCalendarState = useCallback(function (newCalendarState, newSize) {
    dispatch({
      type: ActionType.UPDATE_CALENDAR_STATE,
      payload: {
        calendarState: newCalendarState,
        size: newSize
      }
    });
  }, []);
  return {
    handleOnChangeDay: handleOnChangeDay,
    handleOnChangeMonth: handleOnChangeMonth,
    handleOnChangeQuarter: handleOnChangeQuarter,
    handleOnChangeYear: handleOnChangeYear,
    handleUpdateCalendarState: handleUpdateCalendarState
  };
};

export { useCalendarDateChange };
