"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

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

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

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

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

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

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

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

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

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

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

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

var _RadioButton = _interopRequireDefault(require("../RadioButton"));

var _warning = _interopRequireDefault(require("warning"));

var _js = require("../../globals/js");

var _excluded = ["value"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var prefix = _js.settings.prefix;

var RadioButtonGroup = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2.default)(RadioButtonGroup, _React$Component);

  var _super = _createSuper(RadioButtonGroup);

  function RadioButtonGroup() {
    var _this;

    (0, _classCallCheck2.default)(this, RadioButtonGroup);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      selected: _this.props.valueSelected || _this.props.defaultSelected
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getRadioButtons", function () {
      var children = _react.default.Children.map(_this.props.children, function (radioButton) {
        var _radioButton$props = radioButton.props,
            value = _radioButton$props.value,
            other = (0, _objectWithoutProperties2.default)(_radioButton$props, _excluded);
        /* istanbul ignore if */

        if (radioButton.props.hasOwnProperty('checked')) {
          process.env.NODE_ENV !== "production" ? (0, _warning.default)(false, "Instead of using the checked property on the RadioButton, set\n            the defaultSelected property or valueSelected property on the RadioButtonGroup.") : void 0;
        }

        return /*#__PURE__*/_react.default.createElement(_RadioButton.default, (0, _extends2.default)({}, other, {
          name: _this.props.name,
          key: value,
          value: value,
          onChange: _this.handleChange,
          checked: value === _this.state.selected
        }));
      });

      return children;
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleChange", function (newSelection, value, evt) {
      if (newSelection !== _this.state.selected) {
        _this.setState({
          selected: newSelection
        });

        _this.props.onChange(newSelection, _this.props.name, evt);
      }
    });
    return _this;
  }

  (0, _createClass2.default)(RadioButtonGroup, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          children = _this$props.children,
          disabled = _this$props.disabled,
          labelText = _this$props.labelText,
          helperText = _this$props.helperText,
          controlled = _this$props.controlled,
          vertical = _this$props.vertical,
          hideLabel = _this$props.hideLabel,
          _this$props$className = _this$props.className,
          className = _this$props$className === void 0 ? "".concat(prefix, "--radio-button-group") : _this$props$className;
      var labelClasses = (0, _classnames.default)("".concat(prefix, "--label"), (0, _defineProperty2.default)({}, "".concat(prefix, "--visually-hidden"), hideLabel));
      var wrapperClasses = (0, _classnames.default)("".concat(prefix, "--form-item"), (0, _defineProperty2.default)({}, "".concat(prefix, "--radio-button-group__vertical"), vertical));
      var label = labelText ? /*#__PURE__*/_react.default.createElement("span", {
        className: labelClasses
      }, labelText) : null;
      var helper = helperText ? /*#__PURE__*/_react.default.createElement("div", {
        className: "wfp--form__helper-text"
      }, helperText) : null; // console.warn(
      //   'RadioButtonGroup is depreciated and will be removed in the next versions. Use InputGroup instead'
      // );

      return /*#__PURE__*/_react.default.createElement("div", {
        className: wrapperClasses
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: className,
        disabled: disabled
      }, label, helper, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(prefix, "--radio-button-group-inside")
      }, controlled ? this.getRadioButtons() : children)));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(_ref, state) {
      var valueSelected = _ref.valueSelected,
          defaultSelected = _ref.defaultSelected;
      var prevValueSelected = state.prevValueSelected;
      return prevValueSelected === valueSelected ? null : {
        selected: valueSelected || defaultSelected,
        prevValueSelected: valueSelected
      };
    }
  }]);
  return RadioButtonGroup;
}(_react.default.Component);

exports.default = RadioButtonGroup;
(0, _defineProperty2.default)(RadioButtonGroup, "propTypes", {
  /**
   * Provide a collection of <RadioButton> components to render in the group
   */
  children: _propTypes.default.node,

  /**
   * Provide an optional className to be applied to the container node
   */
  className: _propTypes.default.string,

  /**
   * Provide an optional className to be applied to the container node
   */
  controlled: _propTypes.default.bool,

  /**
   * Specify the <RadioButton> to be selected by default
   */
  defaultSelected: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),

  /**
   * Specify the name of the underlying `input` nodes
   */
  name: _propTypes.default.string.isRequired,

  /**
   * Specify whether the group is disabled
   */
  disabled: _propTypes.default.bool,

  /**
   * Provide an optional `onChange` hook that is called whenever the value of
   * the group changes
   */
  onChange: _propTypes.default.func,

  /**
   * Specify the value that is currently selected in the group
   */
  valueSelected: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),

  /**
   * Specify whether the elements should be displayed vertically.
   */
  vertical: _propTypes.default.bool,

  /**
   * Specify whether you want the underlying label to be visually hidden
   */
  hideLabel: _propTypes.default.bool,

  /**
   * Provide text that is used alongside the control label for additional help
   */
  helperText: _propTypes.default.node,

  /**
   * Provide the text that will be read by a screen reader when visiting this
   * control
   */
  labelText: _propTypes.default.node
});
(0, _defineProperty2.default)(RadioButtonGroup, "defaultProps", {
  onChange:
  /* istanbul ignore next */
  function onChange() {},
  controlled: false
});