'use strict';

exports.__esModule = true;

var _get2 = require('lodash/get');

var _get3 = _interopRequireDefault(_get2);

var _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; };

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _compose = require('recompose/compose');

var _compose2 = _interopRequireDefault(_compose);

var _utils = require('./utils');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var Input = function (_React$Component) {
  _inherits(Input, _React$Component);

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

    _classCallCheck(this, Input);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleChange = function (event) {
      var onChange = _this.props.onChange;

      var nextValue = event.target.value;
      onChange && onChange(nextValue, event);
    }, _this.handleKeyDown = function (event) {
      var _this$props = _this.props,
          onKeyDown = _this$props.onKeyDown,
          onPressEnter = _this$props.onPressEnter;


      if (event.keyCode === 13) {
        onPressEnter && onPressEnter(event);
      }

      onKeyDown && onKeyDown(event);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  Input.prototype.render = function render() {
    var controlId = (0, _get3.default)(this.context, 'formGroup.controlId');

    var _props = this.props,
        type = _props.type,
        className = _props.className,
        classPrefix = _props.classPrefix,
        componentClass = _props.componentClass,
        onChange = _props.onChange,
        _props$id = _props.id,
        id = _props$id === undefined ? controlId : _props$id,
        onFocus = _props.onFocus,
        onBlur = _props.onBlur,
        disabled = _props.disabled,
        value = _props.value,
        defaultValue = _props.defaultValue,
        inputRef = _props.inputRef,
        rest = _objectWithoutProperties(_props, ['type', 'className', 'classPrefix', 'componentClass', 'onChange', 'id', 'onFocus', 'onBlur', 'disabled', 'value', 'defaultValue', 'inputRef']);

    var classes = (0, _classnames2.default)(classPrefix, className);
    var inputGroup = this.context.inputGroup;

    var Component = componentClass;
    var unhandled = (0, _utils.getUnhandledProps)(Input, rest);

    return React.createElement(Component, _extends({}, unhandled, {
      ref: inputRef,
      type: type,
      id: id,
      value: value,
      defaultValue: defaultValue,
      disabled: disabled,
      onKeyDown: this.handleKeyDown,
      onFocus: (0, _utils.createChainedFunction)(onFocus, (0, _get3.default)(inputGroup, 'onFocus')),
      onBlur: (0, _utils.createChainedFunction)(onBlur, (0, _get3.default)(inputGroup, 'onBlur')),
      className: classes,
      onChange: this.handleChange
    }));
  };

  return Input;
}(React.Component);

Input.defaultProps = {
  classPrefix: 'input',
  type: 'text'
};
Input.contextTypes = {
  formGroup: _propTypes2.default.object,
  inputGroup: _propTypes2.default.object
};
Input.handledProps = ['className', 'classPrefix', 'componentClass', 'defaultValue', 'disabled', 'id', 'inputRef', 'onBlur', 'onChange', 'onFocus', 'onKeyDown', 'onPressEnter', 'type', 'value'];
Input.propTypes = {
  type: _propTypes2.default.string.isRequired,
  componentClass: function componentClass() {
    return (typeof (React.ElementType == null ? {} : React.ElementType) === 'function' ? _propTypes2.default.instanceOf(React.ElementType == null ? {} : React.ElementType).isRequired : _propTypes2.default.any.isRequired).apply(this, arguments);
  },
  id: _propTypes2.default.string,
  classPrefix: _propTypes2.default.string.isRequired,
  className: _propTypes2.default.string,
  disabled: _propTypes2.default.bool,
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  inputRef: function inputRef() {
    return (typeof (React.ElementRef == null ? {} : React.ElementRef) === 'function' ? _propTypes2.default.instanceOf(React.ElementRef == null ? {} : React.ElementRef) : _propTypes2.default.any).apply(this, arguments);
  },
  onChange: _propTypes2.default.func,
  onFocus: _propTypes2.default.func,
  onBlur: _propTypes2.default.func,
  onKeyDown: _propTypes2.default.func,
  onPressEnter: _propTypes2.default.func
};
exports.default = (0, _compose2.default)((0, _utils.defaultProps)({
  classPrefix: 'input',
  componentClass: 'input'
}), (0, _utils.withStyleProps)({
  hasSize: true
}))(Input);