import _extends from "babel-runtime/helpers/extends";
import _defineProperty from "babel-runtime/helpers/defineProperty";
import _classCallCheck from "babel-runtime/helpers/classCallCheck";
import _createClass from "babel-runtime/helpers/createClass";
import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn";
import _inherits from "babel-runtime/helpers/inherits";
import React, { Component } from "react";
import PropTypes from "prop-types";
import moment from "moment";
import Slider from '../slider';
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 moment(end).diff(moment(start), viewType === "day" ? "days" : "months");
};
var returnAdd = function returnAdd(start, addNum, viewType) {
    return moment(start).add(addNum, viewType === "day" ? "days" : "months");
};
var compareDate = function compareDate(start, end) {
    return moment(end).isAfter(moment(start));
};
var formatValue = function formatValue(value, format) {
    return moment(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) {
    _inherits(SliderDate, _Component);

    function SliderDate(props) {
        _classCallCheck(this, SliderDate);

        var _this = _possibleConstructorReturn(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)
            }, _defineProperty(_marks, defaultValue, formatValue(propDefault, format)), _defineProperty(_marks, diff, formatValue(max, format)), _marks);
            var sliderProps = _extends(_extends({}, 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 = _extends({}, calculateDiff(props));
        return _this;
    }

    _createClass(SliderDate, [{
        key: "render",
        value: function render() {
            return React.createElement(Slider, _extends({}, this.props, this.handleProps(), { tipFormatter: this.formatter, onAfterChange: this.handleAfterChange, onChange: this.handleChange }));
        }
    }]);

    return SliderDate;
}(Component);

export default SliderDate;

SliderDate.propTypes = {
    viewType: PropTypes.string,
    // format: PropTypes.string,
    onChange: PropTypes.func,
    onAfterChange: PropTypes.func
};
SliderDate.defaultProps = {
    viewType: DEFAULT_VIEWTYPE,
    onChange: function onChange() {},
    onAfterChange: function onAfterChange() {}
};