"use strict";

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

var _extends2 = require("babel-runtime/helpers/extends");

var _extends3 = _interopRequireDefault(_extends2);

var _defineProperty2 = require("babel-runtime/helpers/defineProperty");

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require("babel-runtime/helpers/createClass");

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require("babel-runtime/helpers/inherits");

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

var _moment = require("moment");

var _moment2 = _interopRequireDefault(_moment);

var _slider = require("../slider");

var _slider2 = _interopRequireDefault(_slider);

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

var DEFAULT_FORMAT_DATE = "YYYY-MM-DD";
var DEFAULT_FORMAT_MONTH = "YYYY-MM";
var DEFAULT_VIEWTYPE = "day";
var DEFAULT_OPTS = {
    min: 0,
    step: 1
};
var returnDiff = function returnDiff(start, end, viewType) {
    return (0, _moment2["default"])(end).diff((0, _moment2["default"])(start), viewType === "day" ? "days" : "months");
};
var returnAdd = function returnAdd(start, addNum, viewType) {
    return (0, _moment2["default"])(start).add(addNum, viewType === "day" ? "days" : "months");
};
var compareDate = function compareDate(start, end) {
    return (0, _moment2["default"])(end).isAfter((0, _moment2["default"])(start));
};
var formatValue = function formatValue(value, format) {
    return (0, _moment2["default"])(value).format(format);
};
var calculateDiff = function calculateDiff(props) {
    var min = props.min,
        max = props.max,
        defaultValue = props.defaultValue,
        viewType = props.viewType,
        format = props.format;

    var defaultState = {
        format: format || (viewType === "day" ? DEFAULT_FORMAT_DATE : DEFAULT_FORMAT_MONTH),
        diff: returnDiff(min, max, viewType),
        defaultValue: defaultValue && compareDate ? returnDiff(min, defaultValue, viewType) : 0
    };
    return defaultState;
};

var SliderDate = function (_Component) {
    (0, _inherits3["default"])(SliderDate, _Component);

    function SliderDate(props) {
        (0, _classCallCheck3["default"])(this, SliderDate);

        var _this = (0, _possibleConstructorReturn3["default"])(this, (SliderDate.__proto__ || Object.getPrototypeOf(SliderDate)).call(this, props));

        _this.handleProps = function () {
            var _marks;

            var _this$state = _this.state,
                diff = _this$state.diff,
                defaultValue = _this$state.defaultValue,
                format = _this$state.format;
            var _this$props = _this.props,
                min = _this$props.min,
                max = _this$props.max,
                _this$props$defaultVa = _this$props.defaultValue,
                propDefault = _this$props$defaultVa === undefined ? min : _this$props$defaultVa;

            var marks = (_marks = {
                0: formatValue(min, format)
            }, (0, _defineProperty3["default"])(_marks, defaultValue, formatValue(propDefault, format)), (0, _defineProperty3["default"])(_marks, diff, formatValue(max, format)), _marks);
            var sliderProps = (0, _extends3["default"])((0, _extends3["default"])({}, DEFAULT_OPTS), { max: diff, defaultValue: defaultValue,
                marks: marks });
            return sliderProps;
        };
        _this.handleAfterChange = function (value) {
            var onAfterChange = _this.props.onAfterChange;

            onAfterChange(_this.formatter(value));
        };
        _this.handleChange = function (value) {
            var onChange = _this.props.onChange;

            onChange(_this.formatter(value));
        };
        _this.formatter = function (value) {
            var _this$props2 = _this.props,
                min = _this$props2.min,
                viewType = _this$props2.viewType;
            var format = _this.state.format;

            var nVal = returnAdd(min, value, viewType);
            return formatValue(nVal, format);
        };
        _this.state = (0, _extends3["default"])({}, calculateDiff(props));
        return _this;
    }

    (0, _createClass3["default"])(SliderDate, [{
        key: "render",
        value: function render() {
            return _react2["default"].createElement(_slider2["default"], (0, _extends3["default"])({}, this.props, this.handleProps(), { tipFormatter: this.formatter, onAfterChange: this.handleAfterChange, onChange: this.handleChange }));
        }
    }]);
    return SliderDate;
}(_react.Component);

exports["default"] = SliderDate;

SliderDate.propTypes = {
    viewType: _propTypes2["default"].string,
    // format: PropTypes.string,
    onChange: _propTypes2["default"].func,
    onAfterChange: _propTypes2["default"].func
};
SliderDate.defaultProps = {
    viewType: DEFAULT_VIEWTYPE,
    onChange: function onChange() {},
    onAfterChange: function onAfterChange() {}
};
module.exports = exports['default'];