'use strict';

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

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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

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 _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) : subClass.__proto__ = superClass; }

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

  _createClass(Input, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.setState({
        _type: this.props.type
      });
    }
  }]);

  function Input(props) {
    _classCallCheck(this, Input);

    var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props));

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

    _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$props = _this.props,
          disabled = _this$props.disabled,
          id = _this$props.id,
          className = _this$props.className,
          invalid = _this$props.invalid,
          onFocus = _this$props.onFocus,
          onChange = _this$props.onChange,
          onKeyDown = _this$props.onKeyDown,
          value = _this$props.value,
          required = _this$props.required,
          error = _this$props.error,
          placeholder = _this$props.placeholder,
          inputRef = _this$props.inputRef,
          onBlur = _this$props.onBlur,
          Styles = _this$props.Styles,
          inlineContent = _this$props.inlineContent,
          readonly = _this$props.readonly,
          name = _this$props.name,
          maxLength = _this$props.maxLength,
          min = _this$props.min,
          max = _this$props.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
      }, _defineProperty(_React$createElement, 'onBlur', onBlur), _defineProperty(_React$createElement, 'inlineContent', inlineContent), _defineProperty(_React$createElement, 'maxLength', maxLength), _defineProperty(_React$createElement, 'min', min), _defineProperty(_React$createElement, 'max', max), _React$createElement));
    };

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

  _createClass(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.Component);

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,
  /**
   * 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
};


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

exports.default = Input;