"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RadioGroup = void 0;

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

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

var _autoId = require("@reach/auto-id");

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

var _Box = require("../Box");

var _Form = require("../Form");

var _FormControl = require("../FormControl");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;
var RadioGroup = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var onChange = _ref.onChange,
      name = _ref.name,
      variantColor = _ref.variantColor,
      size = _ref.size,
      defaultValue = _ref.defaultValue,
      isInline = _ref.isInline,
      skipFormChange = _ref.skipFormChange,
      valueProp = _ref.value,
      schema = _ref.schema,
      children = _ref.children,
      rest = (0, _objectWithoutProperties2["default"])(_ref, ["onChange", "name", "variantColor", "size", "defaultValue", "isInline", "skipFormChange", "value", "schema", "children"]);

  var _useRef = (0, _react.useRef)(valueProp != null),
      isControlled = _useRef.current;

  var _useState = (0, _react.useState)(defaultValue || null),
      value = _useState[0],
      setValue = _useState[1];

  var _value = isControlled ? valueProp : value; // If no name is passed, we'll generate a random, unique name


  var fallbackName = "radio-".concat((0, _autoId.useId)());

  var _name = name || fallbackName;

  var _useFormField = (0, _Form.useFormField)({
    name: _name,
    onChange: onChange,
    schema: schema
  }),
      formOnChange = _useFormField.onChange,
      initialRadioValue = _useFormField.value,
      errors = _useFormField.errors;

  var rootRef = (0, _react.useRef)();

  var _onChange = function _onChange(event) {
    if (!isControlled) {
      setValue(event.target.value);
    }

    if (onChange) {
      onChange(event, event.target.value);
    }

    if (formOnChange && typeof formOnChange === 'function' && !skipFormChange) {
      formOnChange(event);
    }
  };

  var clones = _react.Children.map(children, function (child, index) {
    if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
      return null;
    }

    var isLastRadio = children.length === index + 1;
    var spacingProps = isInline ? {
      mr: 'spacing'
    } : {
      mb: 'spacing-xs'
    };
    var isChecked = child.props.value === _value ? true : undefined;
    return __jsx(_Box.Box, (0, _extends2["default"])({
      display: isInline ? 'inline-block' : 'block'
    }, !isLastRadio && spacingProps), /*#__PURE__*/(0, _react.cloneElement)(child, {
      size: child.props.size || size,
      variantColor: child.props.variantColor || variantColor,
      name: _name,
      onChange: _onChange,
      isChecked: isChecked,
      // set undefined to avoid having values for both isChecked and defaultIsChecked
      defaultIsChecked: !isChecked ? child.props.value === initialRadioValue : undefined
    }));
  }); // Calling focus() on the radiogroup should focus on the selected option or first enabled option


  (0, _react.useImperativeHandle)(ref, function () {
    return {
      focus: function focus() {
        var input = rootRef.current.querySelector('input:not(:disabled):checked');

        if (!input) {
          input = rootRef.current.querySelector('input:not(:disabled)');
        }

        if (input) {
          input.focus();
        }
      }
    };
  }, []);
  var spacingProps = isInline ? {
    mr: 'spacing'
  } : {
    mb: 'spacing'
  };
  return __jsx(_FormControl.FormControlWrapper, (0, _extends2["default"])({
    id: name,
    error: errors
  }, rest, spacingProps), __jsx(_Box.Box, (0, _extends2["default"])({
    ref: rootRef,
    role: "radiogroup"
  }, rest), clones));
});
exports.RadioGroup = RadioGroup;
//# sourceMappingURL=RadioGroup.js.map