'use strict';

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

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

exports.formatFn = formatFn;
exports.getDefaultDate = getDefaultDate;

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

function formatIt(date, form) {
    var pad = function pad(n) {
        return n < 10 ? '0' + n : n;
    };
    var dateStr = date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate());
    var timeStr = pad(date.getHours()) + ':' + pad(date.getMinutes());
    if (form === 'YYYY-MM-DD') {
        return dateStr;
    }
    if (form === 'HH:mm') {
        return timeStr;
    }
    return dateStr + ' ' + timeStr;
}
function formatFn(instance, value) {
    var formatsEnum = {
        date: 'YYYY-MM-DD',
        time: 'HH:mm',
        datetime: 'YYYY-MM-DD HH:mm'
    };
    var format = instance.props.format;

    var type = typeof format === 'undefined' ? 'undefined' : (0, _typeof3['default'])(format);
    if (type === 'string') {
        return formatIt(value, format);
    }
    if (type === 'function') {
        return format(value);
    }
    return formatIt(value, formatsEnum[instance.props.mode]);
}
function getDefaultDate(props) {
    var defaultDate = props.defaultDate,
        minDate = props.minDate,
        maxDate = props.maxDate;

    if (defaultDate) {
        return defaultDate;
    }
    var now = new Date();
    if (minDate && now < minDate) {
        return minDate;
    }
    if (maxDate && maxDate < now) {
        return minDate;
    }
    return now;
}