'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Input = require('../Input');

var _Input2 = _interopRequireDefault(_Input);

var _RadioGroup = require('../RadioGroup');

var _RadioGroup2 = _interopRequireDefault(_RadioGroup);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _TimeContainer = require('./styles/TimeContainer');

var _TimeContainer2 = _interopRequireDefault(_TimeContainer);

var _TimeRadioContainer = require('./styles/TimeRadioContainer');

var _TimeRadioContainer2 = _interopRequireDefault(_TimeRadioContainer);

var _InternalTimePicker = require('./InternalTimePicker');

var _InternalTimePicker2 = _interopRequireDefault(_InternalTimePicker);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var RadioButton = _RadioGroup2.default.Button;

var TimePicker = function (_React$PureComponent) {
  (0, _inherits3.default)(TimePicker, _React$PureComponent);

  function TimePicker() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, TimePicker);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = TimePicker.__proto__ || (0, _getPrototypeOf2.default)(TimePicker)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      internalValue: undefined
    }, _this.handleOnChange = function (_ref2) {
      var value = _ref2.value,
          amSelected = _ref2.amSelected;

      if (!value) {
        return;
      }
      var timeValue = (0, _moment2.default)(value, _this.props.timeFormat);
      var timeValueIsAm = timeValue.hours() < 12;
      var sign = 0;
      if (!_this.twentyFourHour && amSelected !== timeValueIsAm) sign = amSelected ? -1 : 1;
      var newValue = timeValue.add(sign * 12, 'hours');
      _this.setState({ internalValue: newValue });
      _this.props.onChange(newValue);
    }, _this.handleTimeChange = function (date) {
      return _this.handleOnChange({ value: date, amSelected: _this.isAm });
    }, _this.handleAmPm = function (amSelected) {
      return function (ev) {
        return _this.handleOnChange({ value: _this.momentValue, amSelected: amSelected });
      };
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(TimePicker, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          name = _props.name,
          disabled = _props.disabled;

      return _react2.default.createElement(
        _TimeContainer2.default,
        null,
        _react2.default.createElement(_InternalTimePicker2.default, {
          disabled: disabled,
          onChange: this.handleTimeChange,
          value: this.momentValue.format(this.props.timeFormat)
        }),
        !this.twentyFourHour && _react2.default.createElement(
          _TimeRadioContainer2.default,
          null,
          _react2.default.createElement(RadioButton.Small, {
            onChange: this.handleAmPm(true),
            checked: this.isAm,
            name: name + '-ampm',
            value: 'am',
            label: 'AM'
          }),
          _react2.default.createElement(RadioButton.Small, {
            onChange: this.handleAmPm(false),
            checked: !this.isAm,
            name: name + '-ampm',
            value: 'pm',
            label: 'PM'
          })
        )
      );
    }
  }, {
    key: 'twentyFourHour',
    get: function get() {
      return (/[Hk]/g.test(this.props.timeFormat)
      );
    }
  }, {
    key: 'value',
    get: function get() {
      return this.props.value ? this.props.value : this.state.internalValue;
    }
  }, {
    key: 'momentValue',
    get: function get() {
      return (0, _moment2.default)(this.value);
    }
  }, {
    key: 'isAm',
    get: function get() {
      return this.momentValue.hours() <= 11;
    }
  }]);
  return TimePicker;
}(_react2.default.PureComponent);

TimePicker.propTypes = {
  /**
   * The current time value of the widget. The value should be a moment object or a unix time.
   * If value is not passed, the component will control its own time.
   */
  value: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.number]),
  /**
   * Time format to pass to moment to format the string. Probably hh:mm or HH:mm for 24 hour time
   */
  timeFormat: _propTypes2.default.string,
  /**
   * Should the time input be disabled?
   */
  disabled: _propTypes2.default.bool
};
TimePicker.defaultProps = {
  value: undefined,
  timeFormat: 'hh:mm',
  disabled: false
};
exports.default = TimePicker;