'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var types = require('../store/types.js');
var reducer = require('../store/reducer.js');

var useCalendarDateChange = function useCalendarDateChange(_ref) {
  var type = _ref.type,
    onChangeValue = _ref.onChangeValue,
    onSelectIndexes = _ref.onSelectIndexes,
    dispatch = _ref.dispatch;
  var handleOnChangeDay = React.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 = 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 || onChangeValue(newQuarterDate, {
      name: quarterName,
      fullValue: [newQuarterDate, endQuarter]
    });
    onSelectIndexes(coord);
  }, [onChangeValue, onSelectIndexes, type]);
  var handleOnChangeMonth = 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 || 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 = 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 || 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 = 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
  };
};

exports.useCalendarDateChange = useCalendarDateChange;
