"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 _react = _interopRequireDefault(require("react"));

var _index = _interopRequireDefault(require("./index"));

var _box = _interopRequireDefault(require("../box"));

var _isComponentOfType = _interopRequireDefault(require("../utils/is-component-of-type"));

var _lodash = _interopRequireDefault(require("lodash.omit"));

var _isReactElement = _interopRequireDefault(require("../utils/is-react-element"));

var _excluded = ["children", "className", "disabled", "onChange", "value"];

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 = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var rest = (0, _lodash.default)(others, ['onChange']);

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

  return /*#__PURE__*/_react.default.createElement(_box.default, (0, _extends2.default)({
    "data-teamleader-ui": "radio-group",
    className: className
  }, rest), _react.default.Children.map(children, function (child) {
    var isRadioButtonComponent = (0, _isReactElement.default)(child) && (0, _isComponentOfType.default)(_index.default, child);

    if (!isRadioButtonComponent) {
      return child;
    }

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

var _default = RadioGroup;
exports.default = _default;