import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Component } from 'react';
import { isMoment } from 'moment';
import classNames from 'classnames';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from './locale/en_US';
import RcTimePicker from '../rc-components/time-picker';
import ConfigContext from '../config-provider/ConfigContext';
export function generateShowHourMinuteSecond(format) {
  // Ref: http://momentjs.com/docs/#/parsing/string-format/
  return {
    showHour: format.indexOf('H') > -1 || format.indexOf('h') > -1 || format.indexOf('k') > -1,
    showMinute: format.indexOf('m') > -1,
    showSecond: format.indexOf('s') > -1
  };
}

var TimePicker = /*#__PURE__*/function (_Component) {
  _inherits(TimePicker, _Component);

  var _super = _createSuper(TimePicker);

  function TimePicker(props) {
    var _this;

    _classCallCheck(this, TimePicker);

    _this = _super.call(this, props);

    _this.handleChange = function (value) {
      if (!('value' in _this.props)) {
        _this.setState({
          value: value
        });
      }

      var _this$props = _this.props,
          onChange = _this$props.onChange,
          _this$props$format = _this$props.format,
          format = _this$props$format === void 0 ? 'HH:mm:ss' : _this$props$format;

      if (onChange) {
        onChange(value, value && value.format(format) || '');
      }
    };

    _this.handleOpenClose = function (_ref) {
      var open = _ref.open;
      var onOpenChange = _this.props.onOpenChange;

      if (onOpenChange) {
        onOpenChange(open);
      }
    };

    _this.saveTimePicker = function (timePickerRef) {
      _this.timePickerRef = timePickerRef;
    };

    _this.renderTimePicker = function (locale) {
      var props = _objectSpread({}, _this.props);

      var value = _this.state.value;
      var getPrefixCls = _this.context.getPrefixCls;
      delete props.defaultValue;
      var prefixCls = getPrefixCls('time-picker', props.prefixCls);

      var format = _this.getDefaultFormat();

      var className = classNames(props.className, _defineProperty({}, "".concat(prefixCls, "-").concat(props.size), !!props.size));

      var addon = function addon(panel) {
        return props.addon ? /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-panel-addon")
        }, props.addon(panel)) : null;
      };

      return /*#__PURE__*/React.createElement(RcTimePicker, _extends({}, generateShowHourMinuteSecond(format), props, {
        prefixCls: prefixCls,
        ref: _this.saveTimePicker,
        format: format,
        className: className,
        value: value,
        placeholder: props.placeholder === undefined ? locale.placeholder : props.placeholder,
        onChange: _this.handleChange,
        onOpen: _this.handleOpenClose,
        onClose: _this.handleOpenClose,
        addon: addon
      }));
    };

    var value = props.value || props.defaultValue;

    if (value && !isMoment(value)) {
      throw new Error('The value/defaultValue of TimePicker must be a moment object');
    }

    _this.state = {
      value: value
    };
    return _this;
  }

  _createClass(TimePicker, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this.setState({
          value: nextProps.value
        });
      }
    }
  }, {
    key: "focus",
    value: function focus() {
      this.timePickerRef.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.timePickerRef.blur();
    }
  }, {
    key: "getDefaultFormat",
    value: function getDefaultFormat() {
      var _this$props2 = this.props,
          format = _this$props2.format,
          use12Hours = _this$props2.use12Hours;

      if (format) {
        return format;
      }

      if (use12Hours) {
        return 'h:mm:ss a';
      }

      return 'HH:mm:ss';
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(LocaleReceiver, {
        componentName: "TimePicker",
        defaultLocale: defaultLocale
      }, this.renderTimePicker);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return TimePicker;
}(Component);

export { TimePicker as default };
TimePicker.displayName = 'TimePicker';
TimePicker.defaultProps = {
  align: {
    offset: [0, -2]
  },
  disabled: false,
  disabledHours: undefined,
  disabledMinutes: undefined,
  disabledSeconds: undefined,
  hideDisabledOptions: false,
  placement: 'bottomLeft',
  transitionName: 'slide-up',
  focusOnOpen: true
};
//# sourceMappingURL=index.js.map
