'use strict';

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

exports.__esModule = true;
exports.SelectControl = 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 _lodash = _interopRequireDefault(require("lodash"));

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

var _FormControl3 = _interopRequireDefault(require("react-bootstrap/lib/FormControl"));

var _Static = require("./Static");

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

  function SelectControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = SelectControl.prototype;

  _proto.render = function render() {
    var _this$getPropsFromCon = this.getPropsFromContext(),
        children = _this$getPropsFromCon.children,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$getPropsFromCon, ["children"]);

    if (props.readOnly) {
      var valueStaticText = '';

      var options = _lodash.default.compact(_lodash.default.flatten(children));

      if (options.length > 0) {
        var option = options.find(function (child) {
          return child.props.value == props.value; // eslint-disable-line
        }) || options[0];
        valueStaticText = option.props.children;
      }

      return _react.default.createElement(_Static.Static, (0, _extends2.default)({
        ref: "input"
      }, this.getSafeProps(props), {
        "data-test-automation-id": "select",
        value: valueStaticText,
        hasFeedbackBlock: false
      }));
    } else {
      return _react.default.createElement("span", {
        className: "rc-select",
        "data-test-automation-id": this.props.hasFormGroupContainer ? undefined : this.props.testAutomationId
      }, _react.default.createElement(_FormControl3.default, (0, _extends2.default)({
        ref: "input",
        "data-test-automation-id": "select",
        componentClass: "select"
      }, this.getSafeProps(props)), children));
    }
  };

  return SelectControl;
}(_FormControl2.FormControl);

exports.SelectControl = SelectControl;
(0, _defineProperty2.default)(SelectControl, "propTypes", Object.assign({}, _FormControl2.FormControl.propTypes));
(0, _defineProperty2.default)(SelectControl, "defaultProps", Object.assign({}, _FormControl2.FormControl.defaultProps, {
  testAutomationId: 'select'
}));