"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _ramdaExtension = require("ramda-extension");

var _Flex = _interopRequireDefault(require("../Flex"));

var RadioGroup = function RadioGroup(_ref) {
  var children = _ref.children,
      disabled = _ref.disabled,
      readOnly = _ref.readOnly,
      hasError = _ref.hasError,
      onChangeProp = _ref.onChange,
      valueProp = _ref.value,
      rest = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["children", "disabled", "readOnly", "hasError", "onChange", "value"]);
  return _react.default.createElement(_Flex.default, (0, _extends2.default)({
    flexWrap: "wrap",
    justifyContent: "space-between",
    role: "radiogroup"
  }, rest), _react.Children.map(children, function (item) {
    var _item$props = item.props,
        value = _item$props.value,
        _onChange = _item$props.onChange,
        width = _item$props.width;

    if (typeof value === 'undefined') {
      return item;
    }

    return (0, _react.cloneElement)(item, {
      onChange: function onChange(event) {
        if (readOnly || disabled) {
          return;
        }

        for (var _len = arguments.length, restArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
          restArgs[_key - 1] = arguments[_key];
        }

        onChangeProp.apply(void 0, [event].concat(restArgs));

        if ((0, _ramdaExtension.isFunction)(_onChange)) {
          return _onChange.apply(void 0, [event].concat(restArgs));
        }
      },
      checked: value === valueProp,
      disabled: disabled,
      width: width != null ? width : 'auto',
      readOnly: readOnly,
      hasError: hasError
    });
  }));
};

RadioGroup.propTypes = {
  /** Children to be rendered in the main container. */
  children: _propTypes.default.node,

  /** Disabled state */
  disabled: _propTypes.default.bool,

  /** Error state */
  hasError: _propTypes.default.bool,

  /** Called when `onChange` event is triggered on any of children which has `value` prop set. */
  onChange: _propTypes.default.func,

  /** Read-only state */
  readOnly: _propTypes.default.bool,

  /** Current value of the radio group. */
  value: _propTypes.default.any
};
var _default = RadioGroup;
exports.default = _default;