"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get getInitialState () {
        return getInitialState;
    },
    get reducer () {
        return reducer;
    },
    get sizeMap () {
        return sizeMap;
    }
});
const _utils = require("../utils");
const _types = require("./types");
const 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
        ]
    }
};
const getInitialState = (value, min, renderFromDate, calendarState, isDouble)=>{
    const initDate = value || renderFromDate || min || new Date();
    const date = (0, _utils.getDateFromValue)(initDate);
    const resSize = isDouble ? sizeMap[calendarState].double : sizeMap[calendarState].single;
    return {
        date: {
            ...date,
            day: value !== undefined ? date.day : 0
        },
        startYear: (0, _utils.getStartYear)(date.year),
        calendarState,
        size: resSize
    };
};
const reducer = (state, action)=>{
    switch(action.type){
        case _types.ActionType.PREVIOUS_MONTH:
            {
                const { year, monthIndex } = action.payload;
                const [prevYear, prevMonthIndex] = (0, _utils.getPrevDate)(year, monthIndex);
                return {
                    ...state,
                    date: {
                        day: state.date.day,
                        monthIndex: prevMonthIndex,
                        year: prevYear
                    }
                };
            }
        case _types.ActionType.NEXT_MONTH:
            {
                const { monthIndex, year } = action.payload;
                const [nextYear, nextMonthIndex] = (0, _utils.getNextDate)(year, monthIndex);
                return {
                    ...state,
                    date: {
                        day: state.date.day,
                        monthIndex: nextMonthIndex,
                        year: nextYear
                    }
                };
            }
        case _types.ActionType.PREVIOUS_YEAR:
            {
                const { step } = action.payload;
                const startYear = state.date.year - step <= 0 ? 0 : state.date.year - step;
                return {
                    ...state,
                    startYear,
                    date: {
                        day: state.date.day,
                        monthIndex: state.date.monthIndex,
                        year: startYear
                    }
                };
            }
        case _types.ActionType.NEXT_YEAR:
            {
                const { step } = action.payload;
                return {
                    ...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:
            {
                const { yearsCount } = action.payload;
                const startYear = state.startYear - yearsCount < 0 ? 0 : state.startYear - yearsCount;
                return {
                    ...state,
                    startYear
                };
            }
        case _types.ActionType.NEXT_START_YEAR:
            {
                const { yearsCount } = action.payload;
                return {
                    ...state,
                    startYear: state.startYear + yearsCount
                };
            }
        case _types.ActionType.UPDATE_DATE:
            {
                const { value } = action.payload;
                const date = (0, _utils.getDateFromValue)(value);
                const startYear = (0, _utils.getStartYear)(value.getFullYear());
                return {
                    ...state,
                    date,
                    startYear
                };
            }
        case _types.ActionType.UPDATE_MONTH:
            {
                const { calendarState, monthIndex, year, size } = action.payload;
                return {
                    ...state,
                    size,
                    calendarState,
                    date: {
                        day: state.date.day,
                        monthIndex,
                        year
                    }
                };
            }
        case _types.ActionType.UPDATE_YEAR:
            {
                const { calendarState, year, size } = action.payload;
                return {
                    ...state,
                    size,
                    calendarState,
                    date: {
                        day: state.date.day,
                        monthIndex: state.date.monthIndex,
                        year
                    }
                };
            }
        case _types.ActionType.UPDATE_CALENDAR_STATE:
            {
                const { calendarState, size } = action.payload;
                return {
                    ...state,
                    calendarState,
                    size: size || state.size
                };
            }
        default:
            return state;
    }
};
