import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "disabled", "onChange", "value"];
import React from 'react';
import RadioButton from './index';
import Box from '../box';
import isComponentOfType from '../utils/is-component-of-type';
import omit from 'lodash.omit';
import isReactElement from '../utils/is-react-element';

var RadioGroup = function RadioGroup(_ref) {
  var children = _ref.children,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === void 0 ? false : _ref$disabled,
      onChange = _ref.onChange,
      value = _ref.value,
      others = _objectWithoutProperties(_ref, _excluded);

  var rest = omit(others, ['onChange']);

  var handleChange = function handleChange(value, event) {
    if (onChange) {
      onChange(value, event);
    }
  };

  return /*#__PURE__*/React.createElement(Box, _extends({
    "data-teamleader-ui": "radio-group",
    className: className
  }, rest), React.Children.map(children, function (child) {
    var isRadioButtonComponent = isReactElement(child) && isComponentOfType(RadioButton, child);

    if (!isRadioButtonComponent) {
      return child;
    }

    if (isRadioButtonComponent) {
      return /*#__PURE__*/React.cloneElement(child, {
        checked: child.props.value === value,
        disabled: disabled || child.props.disabled,
        onChange: function onChange(event) {
          return handleChange(child.props.value, event);
        }
      });
    }
  }));
};

export default RadioGroup;