'use strict';

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

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

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 _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

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

var _createClass3 = _interopRequireDefault(_createClass2);

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);

var _recompose = require('recompose');

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

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _Anchor = require('../Anchor');

var _Anchor2 = _interopRequireDefault(_Anchor);

var _InputStyles = require('./styles/InputStyles');

var _InputStyles2 = _interopRequireDefault(_InputStyles);

var _InputRevealPasswordWrapper = require('./styles/InputRevealPasswordWrapper');

var _InputRevealPasswordWrapper2 = _interopRequireDefault(_InputRevealPasswordWrapper);

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

var Input = function (_React$PureComponent) {
  (0, _inherits3.default)(Input, _React$PureComponent);
  (0, _createClass3.default)(Input, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.setState({
        _type: this.props.type
      });
    }
  }]);

  function Input(props) {
    (0, _classCallCheck3.default)(this, Input);

    var _this = (0, _possibleConstructorReturn3.default)(this, (Input.__proto__ || (0, _getPrototypeOf2.default)(Input)).call(this, props));

    _this.onBlur = function (event) {
      _this.setState({ visited: true });
      if (_this.props.onBlur) {
        _this.props.onBlur(event);
      }
    };

    _this.getAutoComplete = function () {
      var _this$props = _this.props,
          autocomplete = _this$props.autocomplete,
          autoComplete = _this$props.autoComplete;

      if (autocomplete === true || autocomplete === 'on' || autoComplete === true || autoComplete === 'on') return 'on';
      // Prevent browsers from completing this field like a password.
      if (_this.state._type === 'password') return 'new-password';
      return 'off';
    };

    _this.renderPasswordField = function () {
      var RevealPasswordWrapper = _this.props.RevealPasswordWrapper;

      var toggleState = function toggleState() {
        return _this.state._type === 'password' ? 'text' : 'password';
      };
      var handleClick = function handleClick(evt) {
        evt.preventDefault();
        _this.setState({
          _type: toggleState(_this.state._type)
        });
      };

      var inlineNode = _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _Anchor2.default,
          { href: '', onClick: handleClick },
          _this.state._type === 'password' ? 'Show' : 'Hide'
        )
      );

      return _this.renderInlineContent(inlineNode);
    };

    _this.renderInlineContent = function (node) {
      var RevealPasswordWrapper = _this.props.RevealPasswordWrapper;

      return _react2.default.createElement(
        RevealPasswordWrapper,
        null,
        _this.renderInputField(),
        node
      );
    };

    _this.renderInputField = function () {
      var _React$createElement;

      var _this$props2 = _this.props,
          disabled = _this$props2.disabled,
          id = _this$props2.id,
          className = _this$props2.className,
          invalid = _this$props2.invalid,
          onFocus = _this$props2.onFocus,
          onChange = _this$props2.onChange,
          onKeyDown = _this$props2.onKeyDown,
          value = _this$props2.value,
          required = _this$props2.required,
          error = _this$props2.error,
          placeholder = _this$props2.placeholder,
          inputRef = _this$props2.inputRef,
          onBlur = _this$props2.onBlur,
          Styles = _this$props2.Styles,
          inlineContent = _this$props2.inlineContent,
          readonly = _this$props2.readonly,
          name = _this$props2.name,
          maxLength = _this$props2.maxLength,
          min = _this$props2.min,
          max = _this$props2.max;
      var _this$state = _this.state,
          visited = _this$state.visited,
          type = _this$state._type;


      return _react2.default.createElement(Styles, (_React$createElement = {
        onBlur: _this.onBlur,
        onKeyDown: onKeyDown,
        onFocus: onFocus,
        visited: visited,
        id: id,
        name: name,
        className: className,
        invalid: invalid,
        onChange: onChange,
        value: value,
        required: required,
        type: type,
        error: error,
        disabled: disabled,
        readonly: readonly,
        placeholder: placeholder,
        innerRef: inputRef
      }, (0, _defineProperty3.default)(_React$createElement, 'onBlur', onBlur), (0, _defineProperty3.default)(_React$createElement, 'inlineContent', inlineContent), (0, _defineProperty3.default)(_React$createElement, 'maxLength', maxLength), (0, _defineProperty3.default)(_React$createElement, 'min', min), (0, _defineProperty3.default)(_React$createElement, 'max', max), (0, _defineProperty3.default)(_React$createElement, 'autoComplete', _this.getAutoComplete()), _React$createElement));
    };

    _this.state = { visited: false };
    return _this;
  }

  (0, _createClass3.default)(Input, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          type = _props.type,
          disableShowPassword = _props.disableShowPassword,
          inlineContent = _props.inlineContent;


      if (type === 'password' && !disableShowPassword) {
        return this.renderPasswordField();
      }

      if (inlineContent) {
        return this.renderInlineContent(inlineContent);
      }

      return this.renderInputField();
    }
  }]);
  return Input;
}(_react2.default.PureComponent);

Input.propTypes = {
  /**
   * The value of the input.
   */
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  /**
   * Handler for the onchange event.
   */
  onChange: _propTypes2.default.func,
  /**
   * Handler for the onblur event.
   */
  onBlur: _propTypes2.default.func,
  /**
   * Handler for the onfocus event.
   */
  onFocus: _propTypes2.default.func,
  /**
   * Handler for the onkeydown event.
   */
  onKeyDown: _propTypes2.default.func,
  /**
   * Controls whether the user can change this element.
   */
  disabled: _propTypes2.default.bool,
  /**
   * Controls whether the element is marked as required for form submission.
   */
  required: _propTypes2.default.bool,
  /**
   * Controls whether the user can modify the element - typically displays differently from disabled.
   */
  readonly: _propTypes2.default.bool,
  /**
   * Limits the maximum length of the input.
   */
  maxLength: _propTypes2.default.number,
  /**
   * Limits the minimum value of the input - note that this value is primarily for its semantic meaning.
   */
  min: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
  /**
   * Limits the maximum value of the input - note that this value is primarily for its semantic meaning.
   */
  max: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
  /**
   * Adds an id to the element.
   */
  id: _propTypes2.default.string,
  /**
   * Adds an name to the element.
   */
  name: _propTypes2.default.string,
  /**
   * Adds a class name to the element.
   */
  className: _propTypes2.default.string,
  /**
   * Sets the type of data expected for this input.
   */
  type: _propTypes2.default.oneOf(['search', 'email', 'url', 'tel', 'number', 'range', 'date', 'month', 'week', 'time', 'datetime', 'datetime-local', 'color', 'password', 'text']),
  /**
   * Styles this input as being invalid
   */
  invalid: _propTypes2.default.bool,
  /**
   * Styles this input as having an error related to it
   */
  error: _propTypes2.default.bool,
  /**
   * Placeholder text to display when the input is blank
   */
  placeholder: _propTypes2.default.string,
  /**
   * Disables the ability to show a password
   */
  disableShowPassword: _propTypes2.default.bool,
  /**
   * Suggests to most browsers whether they should autocomplete the field
   */
  autoComplete: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.bool]),
  /**
   * Provides a reference to the input element
   */
  inputRef: _propTypes2.default.func,
  /**
   * A component that renders the internal input element
   */
  Styles: _propTypes2.default.func,
  /**
   * A component that wraps the whole element and helps control spacing
   * when reveal password is enabled
   */
  RevealPasswordWrapper: _propTypes2.default.func,
  /**
   * A react node that is displayed inside input element
   */
  inlineContent: _propTypes2.default.node
};
Input.defaultProps = {
  disabled: false,
  required: false,
  readonly: false,
  id: null,
  name: null,
  type: 'text',
  onChange: function onChange() {
    return null;
  },
  onBlur: function onBlur() {
    return null;
  },
  onFocus: function onFocus() {
    return null;
  },
  onKeyDown: function onKeyDown() {
    return null;
  },
  className: null,
  invalid: false,
  error: false,
  placeholder: '',
  disableShowPassword: false,
  Styles: _InputStyles2.default,
  RevealPasswordWrapper: _InputRevealPasswordWrapper2.default,
  inlineContent: null,
  maxLength: null,
  min: null,
  max: null,
  autoComplete: true
};


Input.Small = (0, _recompose.defaultProps)({
  Styles: _InputStyles2.default.Small
})(Input);

exports.default = Input;