"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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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 _Box = _interopRequireDefault(require("../Box"));

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) { (0, _defineProperty2.default)(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; }

var RadioGroup = function RadioGroup(_ref) {
  var children = _ref.children,
      disabled = _ref.disabled,
      readOnly = _ref.readOnly,
      hasError = _ref.hasError,
      onChangeProp = _ref.onChange,
      valueProp = _ref.value,
      name = _ref.name,
      rest = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["children", "disabled", "readOnly", "hasError", "onChange", "value", "name"]);
  return (
    /*#__PURE__*/
    _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;
      var commonProps = {
        readOnly: readOnly,
        disabled: disabled,
        hasError: hasError,
        width: width != null ? width : 'auto'
      };
      return (
        /*#__PURE__*/
        _react.default.createElement(_Box.default, null, (0, _react.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 ((0, _ramdaExtension.isFunction)(_onChange)) {
              return _onChange.apply(void 0, [event].concat(restArgs));
            }
          },
          checked: value === valueProp
        }, commonProps) : commonProps))
      );
    }))
  );
};

process.env.NODE_ENV !== "production" ? 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,
  name: _propTypes.default.string.isRequired,

  /** 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
} : void 0;
var _default = RadioGroup;
exports.default = _default;