function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { getDateFromValue, getNextDate, getPrevDate, getStartYear } from "../utils";
import { ActionType } from "./types";
export 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
        ]
    }
};
export var getInitialState = function getInitialState(value, min, renderFromDate, calendarState, isDouble) {
    var initDate = value || renderFromDate || min || new Date();
    var date = getDateFromValue(initDate);
    var resSize = isDouble ? sizeMap[calendarState].double : sizeMap[calendarState].single;
    return {
        date: _object_spread_props(_object_spread({}, date), {
            day: value !== undefined ? date.day : 0
        }),
        startYear: getStartYear(date.year),
        calendarState: calendarState,
        size: resSize
    };
};
export var reducer = function reducer(state, action) {
    switch(action.type){
        case ActionType.PREVIOUS_MONTH:
            {
                var _action_payload = action.payload, year = _action_payload.year, monthIndex = _action_payload.monthIndex;
                var _getPrevDate = _sliced_to_array(getPrevDate(year, monthIndex), 2), prevYear = _getPrevDate[0], prevMonthIndex = _getPrevDate[1];
                return _object_spread_props(_object_spread({}, state), {
                    date: {
                        day: state.date.day,
                        monthIndex: prevMonthIndex,
                        year: prevYear
                    }
                });
            }
        case ActionType.NEXT_MONTH:
            {
                var _action_payload1 = action.payload, monthIndex1 = _action_payload1.monthIndex, year1 = _action_payload1.year;
                var _getNextDate = _sliced_to_array(getNextDate(year1, monthIndex1), 2), nextYear = _getNextDate[0], nextMonthIndex = _getNextDate[1];
                return _object_spread_props(_object_spread({}, state), {
                    date: {
                        day: state.date.day,
                        monthIndex: nextMonthIndex,
                        year: nextYear
                    }
                });
            }
        case ActionType.PREVIOUS_YEAR:
            {
                var step = action.payload.step;
                var startYear = state.date.year - step <= 0 ? 0 : state.date.year - step;
                return _object_spread_props(_object_spread({}, state), {
                    startYear: startYear,
                    date: {
                        day: state.date.day,
                        monthIndex: state.date.monthIndex,
                        year: startYear
                    }
                });
            }
        case ActionType.NEXT_YEAR:
            {
                var step1 = action.payload.step;
                return _object_spread_props(_object_spread({}, state), {
                    startYear: state.startYear + step1,
                    date: {
                        day: state.date.day,
                        monthIndex: state.date.monthIndex,
                        year: state.date.year + step1
                    }
                });
            }
        case ActionType.PREVIOUS_START_YEAR:
            {
                var yearsCount = action.payload.yearsCount;
                var startYear1 = state.startYear - yearsCount < 0 ? 0 : state.startYear - yearsCount;
                return _object_spread_props(_object_spread({}, state), {
                    startYear: startYear1
                });
            }
        case ActionType.NEXT_START_YEAR:
            {
                var yearsCount1 = action.payload.yearsCount;
                return _object_spread_props(_object_spread({}, state), {
                    startYear: state.startYear + yearsCount1
                });
            }
        case ActionType.UPDATE_DATE:
            {
                var value = action.payload.value;
                var date = getDateFromValue(value);
                var startYear2 = getStartYear(value.getFullYear());
                return _object_spread_props(_object_spread({}, state), {
                    date: date,
                    startYear: startYear2
                });
            }
        case ActionType.UPDATE_MONTH:
            {
                var _action_payload2 = action.payload, calendarState = _action_payload2.calendarState, monthIndex2 = _action_payload2.monthIndex, year2 = _action_payload2.year, size = _action_payload2.size;
                return _object_spread_props(_object_spread({}, state), {
                    size: size,
                    calendarState: calendarState,
                    date: {
                        day: state.date.day,
                        monthIndex: monthIndex2,
                        year: year2
                    }
                });
            }
        case ActionType.UPDATE_YEAR:
            {
                var _action_payload3 = action.payload, calendarState1 = _action_payload3.calendarState, year3 = _action_payload3.year, size1 = _action_payload3.size;
                return _object_spread_props(_object_spread({}, state), {
                    size: size1,
                    calendarState: calendarState1,
                    date: {
                        day: state.date.day,
                        monthIndex: state.date.monthIndex,
                        year: year3
                    }
                });
            }
        case ActionType.UPDATE_CALENDAR_STATE:
            {
                var _action_payload4 = action.payload, calendarState2 = _action_payload4.calendarState, size2 = _action_payload4.size;
                return _object_spread_props(_object_spread({}, state), {
                    calendarState: calendarState2,
                    size: size2 || state.size
                });
            }
        default:
            return state;
    }
};
