'use strict';

exports.__esModule = true;

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var TimeSelector = function (_React$Component) {
  (0, _inherits3.default)(TimeSelector, _React$Component);

  function TimeSelector() {
    var _temp, _this, _ret;

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

    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, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
      value: _this.props.value,
      isEditing: false
    }, _this.componentDidUpdate = function (prevProps, prevState) {
      if (prevState.value !== _this.props.value && !_this.state.isEditing) {
        _this.setState({ value: _this.props.value });
      }
    }, _this.handleOnChange = function (e) {
      var _this$props = _this.props,
          unit = _this$props.unit,
          militaryTime = _this$props.militaryTime;

      var newValue = e.currentTarget.value;

      if (/[a-zA-Z]/.test(e.currentTarget.value)) {
        e.stopPropagation();
        return false;
      } else if (unit === 'h') {
        newValue = militaryTime ? (parseInt(newValue, 10) > 23 ? 23 : parseInt(newValue, 10)) || '' : (parseInt(newValue, 10) > 12 ? 12 : parseInt(newValue, 10)) || '';
      } else if (unit === 'm') {
        newValue = parseInt(newValue, 10) > 59 ? 59 : parseInt(newValue, 10) || '';
      }

      _this.setState({ value: newValue, isEditing: true });
    }, _this.submitChange = function (e) {
      var _this$props2 = _this.props,
          onKeyDown = _this$props2.onKeyDown,
          unit = _this$props2.unit;


      _this.setState({ isEditing: false }, onKeyDown(unit, e));
    }, _this.handleKey = function (e) {
      var newValue = e.currentTarget.value;
      var _this$props3 = _this.props,
          onKeyDown = _this$props3.onKeyDown,
          unit = _this$props3.unit;


      if (e.keyCode === 38 || e.keyCode === 40) {
        _this.setState({ isEditing: false }, onKeyDown(unit, e));
        e.stopPropagation();
      } else if (e.keyCode === 65 && unit === 'pre') {
        if (newValue.includes('A')) return;

        _this.setState({ isEditing: false }, onKeyDown(unit, e));
        e.stopPropagation();
      } else if (e.keyCode === 80 && unit === 'pre') {
        if (newValue.includes('P')) return;

        _this.setState({ isEditing: false }, onKeyDown(unit, e));
        e.stopPropagation();
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  TimeSelector.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        onUpClick = _props.onUpClick,
        onDownClick = _props.onDownClick,
        inputRef = _props.inputRef,
        type = _props.type,
        unit = _props.unit,
        _onWheel = _props.onWheel;


    return _react2.default.createElement(
      'div',
      null,
      _react2.default.createElement('i', {
        className: 'icon icon-arrow-up_24',
        onClick: function onClick() {
          return _this2.setState({ isEditing: false }, onUpClick);
        },
        onKeyUp: function onKeyUp() {},
        tabIndex: -1,
        role: 'button'
      }),
      _react2.default.createElement('input', {
        onWheel: function onWheel(e) {
          return _onWheel(unit, e);
        },
        type: type,
        minLength: 2,
        maxLength: 2,
        onClick: function onClick(e) {
          return e.currentTarget.focus();
        },
        ref: inputRef,
        onChange: this.handleOnChange,
        tabIndex: 0,
        value: this.state.value,
        onBlur: this.submitChange,
        onKeyUp: this.handleKey
      }),
      _react2.default.createElement('i', {
        className: 'icon icon-arrow-down_24',
        onClick: function onClick() {
          return _this2.setState({ isEditing: false }, onDownClick);
        },
        onKeyUp: function onKeyUp() {},
        role: 'button',
        tabIndex: -1
      })
    );
  };

  return TimeSelector;
}(_react2.default.Component);

TimeSelector.displayName = 'TimeSelector';
TimeSelector.defaultProps = {
  value: '',
  onUpClick: null,
  onDownClick: null,
  onKeyDown: null,
  inputRef: null,
  type: 'text',
  unit: '',
  onWheel: null,
  miltaryTime: false
};
exports.default = TimeSelector;
module.exports = exports['default'];