'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.isLeapYear = isLeapYear;
exports.splitValue = splitValue;
exports.getPrevTime = getPrevTime;
exports.getNextTime = getNextTime;
exports.isBetween = isBetween;
exports.getDays = getDays;

var _dateUtil = require('../utils/dateUtil');

var _numberUtil = require('../utils/numberUtil');

function isLeapYear(Year) {
    if (Year % 4 === 0 && Year % 100 !== 0 || Year % 400 === 0) {
        return true;
    } else {
        return false;
    }
}

function splitValue(value) {
    var split = value.split('/');
    return {
        year: parseInt(split[0], 10),
        month: parseInt(split[1], 10) - 1,
        day: parseInt(split[2], 10)
    };
}

function getPrevTime(year, month) {
    if (month === 0) {
        return {
            month: 11,
            year: year - 1
        };
    } else {
        return {
            year: year,
            month: month - 1
        };
    }
}

function getNextTime(year, month) {
    if (month === 11) {
        return {
            month: 0,
            year: year + 1
        };
    } else {
        return {
            year: year,
            month: month + 1
        };
    }
}

function getTime(str) {
    if (typeof str === 'number') {
        return str;
    }
    return typeof str === 'string' ? new Date(str.replace(/-/g, '/')).getTime() : str.getTime();
}

function isBetween(value, disablePast, disableFuture, rangeBegin, rangeEnd) {
    var _getRange = getRange(disablePast, disableFuture, rangeBegin, rangeEnd),
        start = _getRange.start,
        end = _getRange.end;

    value = getTime(value);
    var isGte = start ? value >= getTime(start) : true;
    var isLte = end ? value <= getTime(end) : true;
    return isGte && isLte;
}

function getRange() {
    var disablePast = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    var disableFuture = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var rangeBegin = arguments[2];
    var rangeEnd = arguments[3];

    var startOfToday = new Date();
    startOfToday.setHours(0, 0, 0, 0);

    if (disablePast) {
        if (!rangeBegin) {
            rangeBegin = startOfToday;
        } else {
            rangeBegin = Math.max(startOfToday.getTime(), getTime(rangeBegin));
        }
    }

    if (disableFuture) {
        if (!rangeEnd) {
            rangeEnd = startOfToday;
        } else {
            rangeEnd = Math.min(startOfToday.getTime(), getTime(rangeEnd));
        }
    }
    return {
        start: rangeBegin,
        end: rangeEnd
    };
}

function getDays(_ref) {
    var year = _ref.year,
        month = _ref.month,
        value = _ref.value,
        rangeBegin = _ref.rangeBegin,
        rangeEnd = _ref.rangeEnd,
        _ref$returnSixRows = _ref.returnSixRows,
        returnSixRows = _ref$returnSixRows === undefined ? true : _ref$returnSixRows,
        disabledDate = _ref.disabledDate;

    var today = (0, _dateUtil.dateFormat)(new Date(), 'YYYY/MM/DD');
    if (value) {
        value = (0, _dateUtil.dateFormat)(value, 'YYYY/MM/DD');
    }
    var _splitValue = splitValue(value || today);

    // if year or month is not specified, get them from value
    if (typeof year !== 'number' || typeof month !== 'number' || month < 0) {
        year = _splitValue.year;
        month = _splitValue.month;
    }

    var firstDayOfMonth = new Date(year, month, 1).getDay();
    var lastDateOfMonth = new Date(year, month + 1, 0).getDate();
    var lastDayOfLastMonth = new Date(year, month, 0).getDate();

    var i = void 0;
    var line = 0;
    var temp = [];
    for (i = 1; i <= lastDateOfMonth; i++) {
        var dow = new Date(year, month, i).getDay();
        // 第一行
        if (dow === 0) {
            temp[line] = [];
        } else if (i === 1) {
            temp[line] = [];

            var k = lastDayOfLastMonth - firstDayOfMonth + 1;
            for (var j = 0; j < firstDayOfMonth; j++) {
                var rs = getPrevTime(year, month);
                temp[line].push({
                    year: rs.year,
                    month: rs.month,
                    month_str: (0, _numberUtil.zeroComplete)(rs.month + 1),
                    day: k,
                    isCurrent: false,
                    isLastMonth: true
                });
                k++;
            }
        }

        var _format = year + '/' + (0, _numberUtil.zeroComplete)(month + 1) + '/' + (0, _numberUtil.zeroComplete)(i);
        var options = {
            year: year,
            month: month,
            month_str: (0, _numberUtil.zeroComplete)(month + 1),
            day: i,
            isCurrent: (value && (0, _dateUtil.dateFormat)(new Date(value), 'YYYY/MM/DD')) === _format,
            isToday: (0, _dateUtil.dateFormat)(new Date(), 'YYYY/MM/DD') === _format,
            isCurMonth: true
        };
        temp[line].push(options);

        if (dow === 6) {
            line++;
        } else if (i === lastDateOfMonth) {
            var _k = 1;
            for (dow; dow < 6; dow++) {
                var _rs = getNextTime(year, month);
                temp[line].push({
                    year: _rs.year,
                    month: _rs.month,
                    month_str: (0, _numberUtil.zeroComplete)(_rs.month + 1),
                    day: _k,
                    isCurrent: false,
                    isNextMonth: true
                });
                _k++;
            }
        }
    }

    if (returnSixRows && temp.length === 5) {
        var _rs2 = getNextTime(year, month);
        var start = temp[4][6].isNextMonth ? temp[4][6].day : 0;
        temp[5] = [];
        for (var _i = 0; _i < 7; _i++) {
            var day = ++start;
            temp[5].push({
                year: _rs2.year,
                month: _rs2.month,
                month_str: (0, _numberUtil.zeroComplete)(_rs2.month + 1),
                day: day,
                isCurrent: false,
                isNextMonth: true
            });
        }
    }

    // 2026-02, there is only 4 lines
    if (returnSixRows && temp.length === 4) {
        var _rs3 = getNextTime(year, month);
        var _start = 0;
        temp[4] = [];
        temp[5] = [];
        for (var _i2 = 0; _i2 < 7; _i2++) {
            var _day = ++_start;
            temp[4].push({
                year: _rs3.year,
                month: _rs3.month,
                month_str: (0, _numberUtil.zeroComplete)(_rs3.month + 1),
                day: _day,
                isNextMonth: true
            });
            _day = ++_start;
            temp[5].push({
                year: _rs3.year,
                month: _rs3.month,
                month_str: (0, _numberUtil.zeroComplete)(_rs3.month + 1),
                day: _day,
                isNextMonth: true
            });
        }
    }

    return {
        year: year,
        month: month,
        month_str: (0, _numberUtil.zeroComplete)(month + 1),
        days: temp.map(function (line) {
            line.map(function (item, index) {
                item.date = item.day;
                item.weekDay = index;
                item.isWeekend = index === 0 || index === 6;
                item.formatedDate = item.year + '/' + item.month_str + '/' + (0, _numberUtil.zeroComplete)(item.date);
                item.timestamp = new Date(item.formatedDate).getTime();
                if (rangeBegin || rangeEnd) {
                    item.isInRange = isBetween(item.timestamp, false, false, rangeBegin, rangeEnd);
                }
                if (disabledDate && typeof disabledDate === 'function') {
                    item.isDisabled = disabledDate(item.timestamp);
                }

                return item;
            });
            return line;
        })
    };
}