'use strict';

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

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

exports.default = TextInput;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _LineInput = require('./LineInput');

var _LineInput2 = _interopRequireDefault(_LineInput);

var _MultilineInput = require('./MultilineInput');

var _MultilineInput2 = _interopRequireDefault(_MultilineInput);

var _pathHelpers = require('../helpers/pathHelpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; }

var propTypes = {
  embedded: _react.PropTypes.bool,
  name: _react.PropTypes.string,
  parentPath: _pathHelpers.pathPropType,
  subpath: _pathHelpers.pathPropType,
  value: _react.PropTypes.string,
  readOnly: _react.PropTypes.bool,
  multiline: _react.PropTypes.bool
};

var defaultProps = {
  multiline: null
};

function getBaseComponent(multiline, value) {
  var isMultiline = false;

  if (typeof multiline === 'undefined' || multiline == null) {
    isMultiline = value && value.indexOf('\n') >= 0;
  } else if (multiline) {
    isMultiline = true;
  }

  return isMultiline ? _MultilineInput2.default : _LineInput2.default;
}

/**
 * A text input. There are three modes: single line, multiline, and fallback.
 *
 * In single line mode, the input is rendered as a LineInput.
 *
 * In multiline mode, the input is rendered as a MultilineInput.
 *
 * In fallback mode, the input is rendered as a LineInput, unless the supplied value contains a
 * newline; then it is rendered as a MultilineInput. Fallback mode is appropriate when existing
 * data to be rendered may contain a mix of single line and multiline values, but new values
 * should be prohibited from containing multiple lines.
 */
function TextInput(props) {
  var multiline = props.multiline,
      value = props.value,
      remainingProps = _objectWithoutProperties(props, ['multiline', 'value']);

  var BaseComponent = getBaseComponent(multiline, value);

  return _react2.default.createElement(BaseComponent, _extends({}, remainingProps, {
    value: value
  }));
}

TextInput.propTypes = propTypes;
TextInput.defaultProps = defaultProps;