'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.FormGroup = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _FormGroup = _interopRequireDefault(require("react-bootstrap/lib/FormGroup"));

var _ControlLabel = _interopRequireDefault(require("react-bootstrap/lib/ControlLabel"));

var _HelpBlock = _interopRequireDefault(require("react-bootstrap/lib/HelpBlock"));

var _FormControl2 = require("./FormControl");

var _Block = require("../Block");

var _lodash = _interopRequireDefault(require("lodash"));

var FormGroup =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(FormGroup, _FormControl);

  function FormGroup(props, context) {
    var _this;

    _this = _FormControl.call(this, props, context) || this;
    _this.ariaDescId = "descId-" + FormGroup.initialAriaDescId++;
    return _this;
  }

  var _proto = FormGroup.prototype;

  _proto.getAriaDescProps = function getAriaDescProps(hasError) {
    return hasError ? {
      'aria-describedby': this.props.ariaDescId || this.ariaDescId
    } : {};
  };

  _proto.getValue = function getValue() {
    if (this.refs.control) {
      return this.refs.control.getValue();
    }

    return null;
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    if (this.refs.control) {
      return this.refs.control.getInputDOMNode();
    }

    return null;
  };

  _proto.controlLabelFallback = function controlLabelFallback() {
    return true;
  };

  _proto.renderLabel = function renderLabel() {
    var _this$getPropsFromCon = this.getPropsFromContext(this.props),
        label = _this$getPropsFromCon.label,
        controlLabel = _this$getPropsFromCon.controlLabel,
        labelClassName = _this$getPropsFromCon.labelClassName,
        disabled = _this$getPropsFromCon.disabled,
        readOnly = _this$getPropsFromCon.readOnly,
        id = _this$getPropsFromCon.id,
        htmlFor = _this$getPropsFromCon.htmlFor;

    var controlLabelNode = null;

    if (!controlLabel && label && this.controlLabelFallback()) {
      controlLabel = label;
    }

    if (controlLabel) {
      controlLabelNode = readOnly ? _react.default.createElement(_Block.Block, {
        condensed: true,
        className: labelClassName
      }, _react.default.createElement("strong", null, controlLabel)) : _react.default.createElement(_ControlLabel.default, {
        htmlFor: htmlFor || id,
        "data-test-automation-id": "label",
        className: (0, _classnames.default)(labelClassName, {
          'disabled-label': disabled
        })
      }, controlLabel);
    }

    return controlLabelNode;
  };

  _proto.render = function render() {
    var _this$getPropsFromCon2 = this.getPropsFromContext(this.props),
        validationState = _this$getPropsFromCon2.validationState,
        bsStyle = _this$getPropsFromCon2.bsStyle,
        controlId = _this$getPropsFromCon2.controlId,
        help = _this$getPropsFromCon2.help,
        wrapperClassName = _this$getPropsFromCon2.wrapperClassName,
        groupClassName = _this$getPropsFromCon2.groupClassName,
        onChange = _this$getPropsFromCon2.onChange,
        noOffset = _this$getPropsFromCon2.noOffset,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$getPropsFromCon2, ["validationState", "bsStyle", "controlId", "help", "wrapperClassName", "groupClassName", "onChange", "noOffset"]);

    onChange = null;

    if (!validationState && bsStyle) {
      validationState = bsStyle;
    }

    if (!controlId) {
      controlId = this.props.testAutomationId;
    }

    groupClassName = (0, _classnames.default)(groupClassName, {
      'rc-form-group-noOffset': noOffset
    });
    var control = wrapperClassName ? _react.default.createElement("div", {
      className: wrapperClassName
    }, this.renderInput(help)) : this.renderInput(help); // we have to remove tabIndex and id from group props, otherwise Filter for ex. will have 2 focusable elements

    var _this$getSafeProps = this.getSafeProps(props),
        tabIndex = _this$getSafeProps.tabIndex,
        id = _this$getSafeProps.id,
        className = _this$getSafeProps.className,
        groupProps = (0, _objectWithoutPropertiesLoose2.default)(_this$getSafeProps, ["tabIndex", "id", "className"]);

    var filteredProps = this.filterGroupProps(groupProps);
    return _react.default.createElement(_FormGroup.default, (0, _extends2.default)({
      validationState: validationState,
      "data-test-automation-id": this.props.testAutomationId,
      controlId: controlId,
      className: (0, _classnames.default)(className, groupClassName)
    }, filteredProps), this.renderLabel(), control, help ? _react.default.createElement(_HelpBlock.default, {
      id: this.props.ariaDescId || this.ariaDescId,
      "data-test-automation-id": "helpBlock"
    }, help) : help);
  };

  _proto.filterGroupProps = function filterGroupProps(props) {
    var attrs = ['name', 'value', 'aria-label', 'aria-hidden', 'aria-checked', 'removeName'];

    var cloneProps = _lodash.default.clone(props);

    attrs.forEach(function (item) {
      delete cloneProps[item];
    });
    return cloneProps;
  };

  _proto.renderInput = function renderInput() {
    return this.props.children;
  };

  return FormGroup;
}(_FormControl2.FormControl);

exports.FormGroup = FormGroup;
(0, _defineProperty2.default)(FormGroup, "initialAriaDescId", Date.now());
(0, _defineProperty2.default)(FormGroup, "propTypes", Object.assign({}, _FormControl2.FormControl.propTypes, {
  groupClassName: _propTypes.default.string,
  ariaDescId: _propTypes.default.string,
  noOffset: _propTypes.default.bool
}));
(0, _defineProperty2.default)(FormGroup, "defaultProps", Object.assign({}, _FormControl2.FormControl.defaultProps, {
  ariaDescId: null
}));