'use strict';

exports.__esModule = 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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _AvInput = require('./AvInput');

var _AvInput2 = _interopRequireDefault(_AvInput);

var _AvGroup = require('./AvGroup');

var _AvGroup2 = _interopRequireDefault(_AvGroup);

var _AvFeedback = require('./AvFeedback');

var _AvFeedback2 = _interopRequireDefault(_AvFeedback);

var _reactstrap = require('reactstrap');

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

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 colSizes = ['xs', 'sm', 'md', 'lg', 'xl'];

var AvField = function (_Component) {
  _inherits(AvField, _Component);

  function AvField() {
    _classCallCheck(this, AvField);

    return _possibleConstructorReturn(this, _Component.apply(this, arguments));
  }

  AvField.prototype.render = function render() {
    var row = false;
    var col = {};
    var labelCol = {};
    var _props = this.props;
    var helpMessage = _props.helpMessage;
    var label = _props.label;
    var labelHidden = _props.labelHidden;
    var inputClass = _props.inputClass;
    var labelClass = _props.labelClass;
    var children = _props.children;
    var omit = _props.id;
    var size = _props.size;
    var disabled = _props.disabled;
    var readOnly = _props.readOnly;
    var grid = _props.grid;
    var labelAttrs = _props.labelAttrs;
    var groupAttrs = _props.groupAttrs;

    var attributes = _objectWithoutProperties(_props, ['helpMessage', 'label', 'labelHidden', 'inputClass', 'labelClass', 'children', 'id', 'size', 'disabled', 'readOnly', 'grid', 'labelAttrs', 'groupAttrs']);

    var id = this.props.id || attributes.name;

    if (grid) {
      colSizes.forEach(function (colSize) {
        if (grid[colSize]) {
          row = true;
          var sizeNum = parseInt(grid[colSize], 10);
          col[colSize] = sizeNum;
          labelCol[colSize] = 12 - sizeNum;
        }
      });
    }

    var input = _react2.default.createElement(
      _AvInput2.default,
      _extends({
        id: id,
        className: inputClass,
        size: size,
        disabled: disabled,
        readOnly: readOnly
      }, attributes),
      children
    );

    var validation = this.context.FormCtrl.getInputState(this.props.name);

    var feedback = validation.errorMessage ? _react2.default.createElement(
      _AvFeedback2.default,
      null,
      validation.errorMessage
    ) : null;
    var help = helpMessage ? _react2.default.createElement(
      _reactstrap.FormText,
      null,
      helpMessage
    ) : null;

    return _react2.default.createElement(
      _AvGroup2.default,
      _extends({ disabled: disabled, row: row }, groupAttrs),
      _react2.default.createElement(
        _reactstrap.Label,
        _extends({
          'for': id,
          className: labelClass,
          hidden: labelHidden,
          size: size
        }, labelCol, labelAttrs),
        label
      ),
      row ? _react2.default.createElement(
        _reactstrap.Col,
        col,
        input,
        feedback,
        help
      ) : input,
      !row && feedback,
      !row && help
    );
  };

  return AvField;
}(_react.Component);

AvField.propTypes = Object.assign({}, _AvInput2.default.propTypes, {
  label: _react.PropTypes.node,
  labelHidden: _react.PropTypes.bool,
  disabled: _react.PropTypes.bool,
  readOnly: _react.PropTypes.bool,
  id: _react.PropTypes.string,
  inputClass: _react.PropTypes.string,
  labelClass: _react.PropTypes.string,
  helpMessage: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.object]),
  errorMessage: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.object]),
  labelAttrs: _react.PropTypes.object,
  groupAttrs: _react.PropTypes.object,
  grid: _react.PropTypes.object
});
AvField.contextTypes = {
  FormCtrl: _react.PropTypes.object.isRequired
};
exports.default = AvField;