'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.keyCodes = undefined;
exports.getDaysInMonth = getDaysInMonth;
exports.getMonthsForYear = getMonthsForYear;
exports.getScrollSpeed = getScrollSpeed;
exports.parseDate = parseDate;
exports.validDate = validDate;
exports.validParsedDate = validParsedDate;

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

require('moment-range');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Object.assign Polyfill
if (typeof Object.assign != 'function') {
    (function () {
        Object.assign = function (target) {
            'use strict';

            if (target === undefined || target === null) {
                throw new TypeError('Cannot convert undefined or null to object');
            }

            var output = Object(target);
            for (var index = 1; index < arguments.length; index++) {
                var source = arguments[index];
                if (source !== undefined && source !== null) {
                    for (var nextKey in source) {
                        if (Object.prototype.hasOwnProperty.call(source, nextKey)) {
                            output[nextKey] = source[nextKey];
                        }
                    }
                }
            }
            return output;
        };
    })();
}

var keyCodes = exports.keyCodes = {
    enter: 13,
    left: 37,
    up: 38,
    right: 39,
    down: 40,
    shift: 16,
    control: 17,
    command: 91
};

function getDaysInMonth(date) {
    return (0, _moment2.default)(date).range('month').toArray('days');
}

function getMonthsForYear(year, min, max) {
    var months = void 0;

    if (min && min.year() == year) {
        months = _moment2.default.range([min, max && max.year() == year ? max : (0, _moment2.default)(min).endOf('year')]);
    } else if (max && max.year() == year) {
        months = _moment2.default.range([min && min.year() == year ? min : (0, _moment2.default)(year, 'YYYY').startOf('year'), max]);
    } else if (year) {
        months = (0, _moment2.default)(year, 'YYYY').range('year');
    } else {
        months = _moment2.default.range('year');
    }

    return months.toArray('months');
}

function getScrollSpeed(settings) {
    settings = settings || {};

    var lastPos,
        newPos,
        timer,
        delta,
        delay = settings.delay || 50; // in "ms" (higher means lower fidelity )

    function clear() {
        lastPos = null;
        delta = 0;
    }

    clear();

    return function (scrollY) {
        newPos = scrollY;
        if (lastPos != null) {
            // && newPos < maxScroll
            delta = newPos - lastPos;
        }
        lastPos = newPos;
        clearTimeout(timer);
        timer = setTimeout(clear, delay);
        return delta;
    };
}

function parseDate(date) {
    if (date) {
        if (!date._isAMomentObject) date = (0, _moment2.default)(date);

        return {
            date: date,
            yyyymmdd: date.format('YYYYMMDD')
        };
    }
}

// Custom date prop validation
function validDate(props, propName, componentName) {
    if (props[propName] && !(0, _moment2.default)(props[propName]).isValid()) {
        return new Error('Invalid prop `' + propName + '` supplied to ' + componentName + '. Should be a format supported by Moment.js, see http://momentjs.com/docs/.');
    }
}

// Custom date prop validation
function validParsedDate(props, propName, componentName) {
    var prop = props[propName];

    if (prop.date && (0, _moment2.default)(prop.date).isValid() && prop.yyyymmdd && prop.yyyymmdd.length == 8) {
        // valid
    } else {
            return new Error('Invalid prop `' + propName + '` supplied to ' + componentName + '.');
        }
}