import _extends from "@babel/runtime/helpers/esm/extends";
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";

/**
 * TimePicker.jsx
 *
 * @Author: apple
 * @Date: 2018/3/1 01:02
 */
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import Tooltip from '../tooltip';
import WatchLater from '../icons/internals/WatchLater';
import Input from '../input';
import TimeOptions from './TimePanel';
/**
 * TimePicker
 * */

var TimePicker =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(TimePicker, _React$PureComponent);

  _createClass(TimePicker, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (props.defaultValue !== state.prevDefaultValue) {
        return {
          value: props.defaultValue,
          prevDefaultValue: props.defaultValue
        };
      }

      return null;
    }
  }]);

  function TimePicker(props, context) {
    var _this;

    _classCallCheck(this, TimePicker);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(TimePicker).call(this, props, context));

    _defineProperty(_assertThisInitialized(_this), "handleInput", function (e) {
      var value = e.target.value;
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          format = _this$props.format;
      var valueMoment = moment(value, format);

      if (valueMoment.isValid()) {
        _this.setState({
          value: valueMoment
        });

        onChange && onChange(value);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleDialog", function (isOpen) {
      _this.setState({
        isOpen: isOpen
      });
    });

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

      _this.setState({
        value: value
      });

      onChange && onChange(value);
    });

    var defaultValue = props.defaultValue,
        defaultIsOpen = props.defaultIsOpen,
        _format = props.format;
    _this.state = {
      value: defaultValue ? moment(defaultValue, _format) : undefined,
      prevDefaultValue: defaultValue,
      isOpen: defaultIsOpen
    };
    return _this;
  }

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

      var _this$props2 = this.props,
          clearable = _this$props2.clearable,
          disabled = _this$props2.disabled,
          inputClass = _this$props2.inputClass,
          prefix = _this$props2.prefix,
          size = _this$props2.size,
          prefixCls = _this$props2.prefixCls,
          readOnly = _this$props2.readOnly,
          disabledHours = _this$props2.disabledHours,
          disabledMinutes = _this$props2.disabledMinutes,
          disabledSeconds = _this$props2.disabledSeconds,
          format = _this$props2.format,
          suffix = _this$props2.suffix,
          inputProps = _this$props2.inputProps,
          scrollContainer = _this$props2.scrollContainer;
      var _this$state = this.state,
          value = _this$state.value,
          isOpen = _this$state.isOpen;
      return React.createElement(Tooltip, {
        trigger: "click",
        className: "".concat(prefixCls, "-tooltip"),
        position: "bottomLeft",
        onOpen: function onOpen() {
          return _this2.handleDialog(true);
        },
        onClose: function onClose() {
          return _this2.handleDialog(false);
        },
        scrollContainer: scrollContainer,
        content: React.createElement(Input, _extends({
          defaultValue: value ? value.format(format) : '',
          disabled: disabled,
          readOnly: true,
          className: inputClass,
          prefix: prefix,
          size: size,
          clearable: clearable,
          suffix: suffix,
          onBlur: this.handleInput
        }, inputProps))
      }, React.createElement(TimeOptions, {
        value: value,
        isOpen: isOpen,
        onValueChange: this.onChange,
        disabledHours: disabledHours,
        disabledMinutes: disabledMinutes,
        disabledSeconds: disabledSeconds
      }));
    }
  }]);

  return TimePicker;
}(React.PureComponent);

_defineProperty(TimePicker, "propTypes", {
  /**
   * 更新时间回调函数
   * @params time: 所选时间
   * @return
   * */
  onChange: PropTypes.func,

  /**
   * 类名前缀
   * */
  prefixCls: PropTypes.string,

  /**
   * 禁用小时
   * @return true为禁用 false为不禁用
   * */
  disabledHours: PropTypes.func,

  /**
   * 禁用分钟
   * @params selectHour:当前所选小时
   * @return true为禁用 false为不禁用
   * */
  disabledMinutes: PropTypes.func,

  /**
   * 禁用秒
   * @params selectHour:当前所选小时, selectMinute:当前所选分钟
   * @return true为禁用 false为不禁用
   * */
  disabledSeconds: PropTypes.func,

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

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

_defineProperty(TimePicker, "defaultProps", {
  prefixCls: 'ne-time-picker',
  defaultIsOpen: false,
  format: 'HH:mm:ss',
  suffix: React.createElement(WatchLater, null)
});

export default TimePicker;