'use strict';

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

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var types = require('./types.js');
var calendarGridHelper = require('../utils/calendarGridHelper.js');

var sizeMap = {
  Days: {
    single: [5, 6],
    "double": [11, 6]
  },
  Months: {
    single: [3, 2],
    "double": [7, 2]
  },
  Years: {
    single: [3, 2],
    "double": [7, 2]
  },
  Quarters: {
    single: [1, 1],
    "double": [3, 1]
  }
};
var getInitialState = function getInitialState(value, min, renderFromDate, calendarState, isDouble) {
  var initDate = value || renderFromDate || min || new Date();
  var date = calendarGridHelper.getDateFromValue(initDate);
  var resSize = isDouble ? sizeMap[calendarState]["double"] : sizeMap[calendarState].single;
  return {
    date: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, date), {}, {
      day: value !== undefined ? date.day : 0
    }),
    startYear: calendarGridHelper.getStartYear(date.year),
    calendarState: calendarState,
    size: resSize
  };
};
var reducer = function reducer(state, action) {
  switch (action.type) {
    case types.ActionType.PREVIOUS_MONTH:
      {
        var _action$payload = action.payload,
          year = _action$payload.year,
          monthIndex = _action$payload.monthIndex;
        var _getPrevDate = calendarGridHelper.getPrevDate(year, monthIndex),
          _getPrevDate2 = _rollupPluginBabelHelpers.slicedToArray(_getPrevDate, 2),
          prevYear = _getPrevDate2[0],
          prevMonthIndex = _getPrevDate2[1];
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          date: {
            day: state.date.day,
            monthIndex: prevMonthIndex,
            year: prevYear
          }
        });
      }
    case types.ActionType.NEXT_MONTH:
      {
        var _action$payload2 = action.payload,
          _monthIndex = _action$payload2.monthIndex,
          _year = _action$payload2.year;
        var _getNextDate = calendarGridHelper.getNextDate(_year, _monthIndex),
          _getNextDate2 = _rollupPluginBabelHelpers.slicedToArray(_getNextDate, 2),
          nextYear = _getNextDate2[0],
          nextMonthIndex = _getNextDate2[1];
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          date: {
            day: state.date.day,
            monthIndex: nextMonthIndex,
            year: nextYear
          }
        });
      }
    case types.ActionType.PREVIOUS_YEAR:
      {
        var step = action.payload.step;
        var startYear = state.date.year - step <= 0 ? 0 : state.date.year - step;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          startYear: startYear,
          date: {
            day: state.date.day,
            monthIndex: state.date.monthIndex,
            year: startYear
          }
        });
      }
    case types.ActionType.NEXT_YEAR:
      {
        var _step = action.payload.step;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          startYear: state.startYear + _step,
          date: {
            day: state.date.day,
            monthIndex: state.date.monthIndex,
            year: state.date.year + _step
          }
        });
      }
    case types.ActionType.PREVIOUS_START_YEAR:
      {
        var yearsCount = action.payload.yearsCount;
        var _startYear = state.startYear - yearsCount < 0 ? 0 : state.startYear - yearsCount;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          startYear: _startYear
        });
      }
    case types.ActionType.NEXT_START_YEAR:
      {
        var _yearsCount = action.payload.yearsCount;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          startYear: state.startYear + _yearsCount
        });
      }
    case types.ActionType.UPDATE_DATE:
      {
        var value = action.payload.value;
        var date = calendarGridHelper.getDateFromValue(value);
        var _startYear2 = calendarGridHelper.getStartYear(value.getFullYear());
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          date: date,
          startYear: _startYear2
        });
      }
    case types.ActionType.UPDATE_MONTH:
      {
        var _action$payload3 = action.payload,
          calendarState = _action$payload3.calendarState,
          _monthIndex2 = _action$payload3.monthIndex,
          _year2 = _action$payload3.year,
          size = _action$payload3.size;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          size: size,
          calendarState: calendarState,
          date: {
            day: state.date.day,
            monthIndex: _monthIndex2,
            year: _year2
          }
        });
      }
    case types.ActionType.UPDATE_YEAR:
      {
        var _action$payload4 = action.payload,
          _calendarState = _action$payload4.calendarState,
          _year3 = _action$payload4.year,
          _size = _action$payload4.size;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          size: _size,
          calendarState: _calendarState,
          date: {
            day: state.date.day,
            monthIndex: state.date.monthIndex,
            year: _year3
          }
        });
      }
    case types.ActionType.UPDATE_CALENDAR_STATE:
      {
        var _action$payload5 = action.payload,
          _calendarState2 = _action$payload5.calendarState,
          _size2 = _action$payload5.size;
        return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
          calendarState: _calendarState2,
          size: _size2 || state.size
        });
      }
    default:
      return state;
  }
};

exports.getInitialState = getInitialState;
exports.reducer = reducer;
exports.sizeMap = sizeMap;
