'use strict';

import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import BSFormGroup from 'react-bootstrap/lib/FormGroup';
import ControlLabel from 'react-bootstrap/lib/ControlLabel';
import HelpBlock from 'react-bootstrap/lib/HelpBlock';
import { FormControl } from './FormControl';
import { Block } from '../Block';
import _ from 'lodash';
export var FormGroup =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(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.createElement(Block, {
        condensed: true,
        className: labelClassName
      }, React.createElement("strong", null, controlLabel)) : React.createElement(ControlLabel, {
        htmlFor: htmlFor || id,
        "data-test-automation-id": "label",
        className: classnames(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 = _objectWithoutPropertiesLoose(_this$getPropsFromCon2, ["validationState", "bsStyle", "controlId", "help", "wrapperClassName", "groupClassName", "onChange", "noOffset"]);

    onChange = null;

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

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

    groupClassName = classnames(groupClassName, {
      'rc-form-group-noOffset': noOffset
    });
    var control = wrapperClassName ? React.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 = _objectWithoutPropertiesLoose(_this$getSafeProps, ["tabIndex", "id", "className"]);

    var filteredProps = this.filterGroupProps(groupProps);
    return React.createElement(BSFormGroup, _extends({
      validationState: validationState,
      "data-test-automation-id": this.props.testAutomationId,
      controlId: controlId,
      className: classnames(className, groupClassName)
    }, filteredProps), this.renderLabel(), control, help ? React.createElement(HelpBlock, {
      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 = _.clone(props);

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

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

  return FormGroup;
}(FormControl);

_defineProperty(FormGroup, "initialAriaDescId", Date.now());

_defineProperty(FormGroup, "propTypes", Object.assign({}, FormControl.propTypes, {
  groupClassName: PropTypes.string,
  ariaDescId: PropTypes.string,
  noOffset: PropTypes.bool
}));

_defineProperty(FormGroup, "defaultProps", Object.assign({}, FormControl.defaultProps, {
  ariaDescId: null
}));