import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import React, { Children, cloneElement, forwardRef } from 'react';
import PropTypes from 'prop-types';
import { isFunction } from 'ramda-extension';
import Flex from '../Flex';
import Box from '../Box';
var RadioGroup = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var children = _ref.children,
      disabled = _ref.disabled,
      readOnly = _ref.readOnly,
      hasError = _ref.hasError,
      onChangeProp = _ref.onChange,
      valueProp = _ref.value,
      name = _ref.name,
      rest = _objectWithoutProperties(_ref, ["children", "disabled", "readOnly", "hasError", "onChange", "value", "name"]);

  return /*#__PURE__*/React.createElement(Flex, _extends({
    ref: ref,
    flexWrap: "wrap",
    justifyContent: "space-between",
    role: "radiogroup"
  }, rest), Children.map(children, function (item) {
    var _item$props = item.props,
        value = _item$props.value,
        _onChange = _item$props.onChange,
        width = _item$props.width;
    var commonProps = {
      readOnly: readOnly,
      disabled: disabled,
      hasError: hasError,
      width: width != null ? width : 'auto'
    };
    return /*#__PURE__*/React.createElement(Box, null, /*#__PURE__*/cloneElement(item, typeof value !== 'undefined' ? _objectSpread({
      name: name,
      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 (isFunction(_onChange)) {
          return _onChange.apply(void 0, [event].concat(restArgs));
        }
      },
      checked: value === valueProp
    }, commonProps) : commonProps));
  }));
});
RadioGroup.displayName = 'RadioGroup';
process.env.NODE_ENV !== "production" ? RadioGroup.propTypes = {
  /** Children to be rendered in the main container. */
  children: PropTypes.node,

  /** Disabled state */
  disabled: PropTypes.bool,

  /** Error state */
  hasError: PropTypes.bool,
  name: PropTypes.string.isRequired,

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

  /** Read-only state */
  readOnly: PropTypes.bool,

  /** Current value of the radio group. */
  value: PropTypes.any
} : void 0;
export default RadioGroup;