import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React from 'react';
import moment from 'moment';
import PropTypes from 'prop-types';
import Input from '../input';
import Calendar from './Calendar';
import CalendarToday from '../icons/internals/CalendarToday';
import Tooltip from '../tooltip';
import Button from '../button';

var DatePicker =
/*#__PURE__*/
function (_React$Component) {
  _inherits(DatePicker, _React$Component);

  _createClass(DatePicker, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var defaultValue = props.defaultValue;

      if (defaultValue !== state.prevDefaultValue) {
        return {
          value: defaultValue,
          prevDefaultValue: defaultValue
        };
      }

      return null;
    }
  }]);

  function DatePicker(props) {
    var _this;

    _classCallCheck(this, DatePicker);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(DatePicker).call(this, props));
    /**
     * 初始化默认值，对空默认值进行处理
     * */

    _defineProperty(_assertThisInitialized(_this), "handleValueChange", function (value) {
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          onFormatDate = _this$props.onFormatDate;
      var date = onFormatDate(value);

      _this.setState({
        value: date
      });

      if (onChange) {
        onChange(date);
      }

      if (_this.inputRef) _this.inputRef.focus();
    });

    _defineProperty(_assertThisInitialized(_this), "handlePanelTypeChange", function (type) {
      _this.setState({
        panelType: type
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleClear", function () {
      var onChange = _this.props.onChange;

      _this.setState({
        value: '',
        panelType: 'date'
      });

      if (onChange) {
        onChange(null);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getInputRef", function (el) {
      _this.inputRef = el;
    });

    var defaultValue = props.defaultValue,
        selectType = props.selectType;
    _this.state = {
      value: defaultValue,
      prevDefaultValue: defaultValue,
      panelType: selectType
    };
    return _this;
  }

  _createClass(DatePicker, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          selectType = _this$props2.selectType,
          readOnly = _this$props2.readOnly,
          format = _this$props2.format,
          placeholder = _this$props2.placeholder,
          disabled = _this$props2.disabled,
          showTime = _this$props2.showTime,
          suffix = _this$props2.suffix,
          inputProps = _this$props2.inputProps,
          size = _this$props2.size,
          status = _this$props2.status,
          clearable = _this$props2.clearable,
          onChange = _this$props2.onChange,
          clearIcon = _this$props2.clearIcon,
          errorIcon = _this$props2.errorIcon,
          loadingIcon = _this$props2.loadingIcon,
          showStatusIcon = _this$props2.showStatusIcon,
          successIcon = _this$props2.successIcon,
          style = _this$props2.style,
          scrollContainer = _this$props2.scrollContainer,
          other = _objectWithoutProperties(_this$props2, ["prefixCls", "selectType", "readOnly", "format", "placeholder", "disabled", "showTime", "suffix", "inputProps", "size", "status", "clearable", "onChange", "clearIcon", "errorIcon", "loadingIcon", "showStatusIcon", "successIcon", "style", "scrollContainer"]);

      var _this$state = this.state,
          value = _this$state.value,
          panelType = _this$state.panelType;
      return React.createElement(Tooltip, {
        position: "bottom",
        trigger: "click",
        className: prefixCls,
        disabled: disabled,
        autoAdjustPosition: true,
        scrollContainer: scrollContainer,
        content: React.createElement(Input, _extends({
          style: style,
          inputRef: this.getInputRef,
          size: size,
          suffix: suffix,
          placeholder: placeholder,
          disabled: disabled,
          clearable: clearable,
          onClear: this.handleClear,
          clearIcon: clearIcon,
          errorIcon: errorIcon,
          loadingIcon: loadingIcon,
          successIcon: successIcon,
          status: status,
          showStatusIcon: showStatusIcon,
          value: value ? moment(value).format(format) : '',
          readOnly: true
        }, inputProps))
      }, React.createElement(Calendar, _extends({
        panelType: panelType,
        onPanelTypeChange: this.handlePanelTypeChange,
        value: value ? value._isAMomentObject ? value : moment(value) : moment(),
        onValueChange: this.handleValueChange,
        onTimeChange: this.handleValueChange,
        selectType: selectType,
        showTime: showTime
      }, other)), value && showTime && (panelType === 'date' || panelType === 'time') ? React.createElement("div", {
        className: "".concat(prefixCls, "-footer")
      }, panelType === 'date' ? React.createElement(Button, {
        variant: "text",
        onClick: function onClick() {
          return _this2.handlePanelTypeChange('time');
        }
      }, "\u65F6\u95F4") : panelType === 'time' ? React.createElement(Button, {
        variant: "text",
        onClick: function onClick() {
          return _this2.handlePanelTypeChange('date');
        }
      }, "\u65E5\u671F") : null) : null);
    }
  }]);

  return DatePicker;
}(React.Component);

_defineProperty(DatePicker, "propTypes", {
  /**
   * 类前缀
   * */
  prefixCls: PropTypes.string,

  /**
   * 分隔符
   * */
  separator: PropTypes.node,

  /**
   * 选择类型
   * */
  selectType: PropTypes.oneOf(['date', 'month', 'year']),

  /**
   * 是否禁用
   */
  disabled: PropTypes.bool,

  /**
   * 日期显示格式
   */
  format: PropTypes.string,

  /**
   * 是否可以选择时刻
   */
  showTime: PropTypes.oneOfType([PropTypes.string, PropTypes.string]),

  /**
   * 后缀
   */
  suffix: PropTypes.node,

  /**
   * 占位文本
   */
  placeholder: PropTypes.node,

  /**
   * input元素的值，受控模式下使用
   */
  defaultValue: PropTypes.object,

  /**
   * 不可选择的时间范围
   */
  disabledTime: PropTypes.func,

  /**
   *  scrollContainer Tooltip兼容自定义可滚动容器
   */
  scrollContainer: PropTypes.node,

  /**
   * 时间格式化之后的回调函数，可以将格式化后的时间处理成需要的特殊格式
   * */
  onFormatDate: PropTypes.func
});

_defineProperty(DatePicker, "defaultProps", {
  disabled: false,
  format: 'YYYY-MM-DD',
  clearable: false,
  separator: '-',
  selectType: 'date',
  showTime: false,
  prefixCls: 'ne-date-picker',
  placeholder: '',
  suffix: React.createElement(CalendarToday, null),
  scrollContainer: '',
  onFormatDate: function onFormatDate(date) {
    return date;
  }
});

export default DatePicker;