function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import * as React from 'react';
import * as moment from 'moment';
import omit from 'omit.js';
import { polyfill } from 'react-lifecycles-compat';
import RcTimePicker from "rc-time-picker/es/TimePicker";
import classNames from 'classnames';
import warning from '../_util/warning';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { ConfigConsumer } from '../config-provider';
import enUS from './locale/en_US';
import interopDefault from '../_util/interopDefault';
import Icon from '../icon';
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 (_React$Component) {
  _inherits(TimePicker, _React$Component);

  var _super = _createSuper(TimePicker);

  function TimePicker(props) {
    var _this;

    _classCallCheck(this, TimePicker);

    _this = _super.call(this, props);

    _this.getDefaultLocale = function () {
      var defaultLocale = _extends(_extends({}, enUS), _this.props.locale);

      return defaultLocale;
    };

    _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.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.renderTimePicker = function (locale) {
      return /*#__PURE__*/React.createElement(ConfigConsumer, null, function (_ref2) {
        var getContextPopupContainer = _ref2.getPopupContainer,
            getPrefixCls = _ref2.getPrefixCls;

        var _a = _this.props,
            getPopupContainer = _a.getPopupContainer,
            customizePrefixCls = _a.prefixCls,
            className = _a.className,
            addon = _a.addon,
            placeholder = _a.placeholder,
            props = __rest(_a, ["getPopupContainer", "prefixCls", "className", "addon", "placeholder"]);

        var size = props.size;
        var pickerProps = omit(props, ['defaultValue', 'suffixIcon', 'allowEmpty', 'allowClear']);

        var format = _this.getDefaultFormat();

        var prefixCls = getPrefixCls('time-picker', customizePrefixCls);
        var pickerClassName = classNames(className, _defineProperty({}, "".concat(prefixCls, "-").concat(size), !!size));

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

        return /*#__PURE__*/React.createElement(RcTimePicker, _extends({}, generateShowHourMinuteSecond(format), pickerProps, {
          allowEmpty: _this.getAllowClear(),
          prefixCls: prefixCls,
          getPopupContainer: getPopupContainer || getContextPopupContainer,
          ref: _this.saveTimePicker,
          format: format,
          className: pickerClassName,
          value: _this.state.value,
          placeholder: placeholder === undefined ? locale.placeholder : placeholder,
          onChange: _this.handleChange,
          onOpen: _this.handleOpenClose,
          onClose: _this.handleOpenClose,
          addon: pickerAddon,
          inputIcon: _this.renderInputIcon(prefixCls),
          clearIcon: _this.renderClearIcon(prefixCls)
        }));
      });
    };

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

    if (value && !interopDefault(moment).isMoment(value)) {
      throw new Error('The value/defaultValue of TimePicker must be a moment object after `antd@2.0`, ' + 'see: https://u.ant.design/time-picker-value');
    }

    _this.state = {
      value: value
    };
    warning(!('allowEmpty' in props), 'TimePicker', '`allowEmpty` is deprecated. Please use `allowClear` instead.');
    return _this;
  }

  _createClass(TimePicker, [{
    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: "getAllowClear",
    value: function getAllowClear() {
      var _this$props3 = this.props,
          allowClear = _this$props3.allowClear,
          allowEmpty = _this$props3.allowEmpty;

      if ('allowClear' in this.props) {
        return allowClear;
      }

      return allowEmpty;
    }
  }, {
    key: "focus",
    value: function focus() {
      this.timePickerRef.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.timePickerRef.blur();
    }
  }, {
    key: "renderInputIcon",
    value: function renderInputIcon(prefixCls) {
      var suffixIcon = this.props.suffixIcon;
      var clockIcon = suffixIcon && /*#__PURE__*/React.isValidElement(suffixIcon) && /*#__PURE__*/React.cloneElement(suffixIcon, {
        className: classNames(suffixIcon.props.className, "".concat(prefixCls, "-clock-icon"))
      }) || /*#__PURE__*/React.createElement(Icon, {
        type: "clock-circle",
        className: "".concat(prefixCls, "-clock-icon")
      });
      return /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-icon")
      }, clockIcon);
    }
  }, {
    key: "renderClearIcon",
    value: function renderClearIcon(prefixCls) {
      var clearIcon = this.props.clearIcon;
      var clearIconPrefixCls = "".concat(prefixCls, "-clear");

      if (clearIcon && /*#__PURE__*/React.isValidElement(clearIcon)) {
        return /*#__PURE__*/React.cloneElement(clearIcon, {
          className: classNames(clearIcon.props.className, clearIconPrefixCls)
        });
      }

      return /*#__PURE__*/React.createElement(Icon, {
        type: "close-circle",
        className: clearIconPrefixCls,
        theme: "filled"
      });
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(LocaleReceiver, {
        componentName: "TimePicker",
        defaultLocale: this.getDefaultLocale()
      }, this.renderTimePicker);
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps) {
      if ('value' in nextProps) {
        return {
          value: nextProps.value
        };
      }

      return null;
    }
  }]);

  return TimePicker;
}(React.Component);

TimePicker.defaultProps = {
  align: {
    offset: [0, -2]
  },
  disabledHours: undefined,
  disabledMinutes: undefined,
  disabledSeconds: undefined,
  hideDisabledOptions: false,
  placement: 'bottomLeft',
  transitionName: 'slide-up',
  focusOnOpen: true
};
polyfill(TimePicker);
export default TimePicker;