import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["name", "required", "disabled", "invalid", "direction", "children", "className", "defaultValue", "value", "onChange"];
import * as React from 'react';
import { generateId } from '../utils';
import cx from 'classnames';
import Flex from '../flex/Flex';
import { CardRadioGroupContext } from './CardRadioGroupContext';
import { CardRadio, CardRadioIndicator } from './CardRadio';
var CardRadioGroupRoot = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var name = props.name,
      _props$required = props.required,
      required = _props$required === void 0 ? false : _props$required,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$invalid = props.invalid,
      invalid = _props$invalid === void 0 ? false : _props$invalid,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'row' : _props$direction,
      children = props.children,
      className = props.className,
      _props$defaultValue = props.defaultValue,
      defaultValue = _props$defaultValue === void 0 ? '' : _props$defaultValue,
      controlledValue = props.value,
      onChange = props.onChange,
      other = _objectWithoutProperties(props, _excluded);

  var _React$useState = React.useState(defaultValue),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      internalValue = _React$useState2[0],
      setInternalValue = _React$useState2[1];

  var isControlled = controlledValue !== undefined;
  var value = isControlled ? controlledValue : internalValue;

  var handleChange = function handleChange(newValue) {
    if (!isControlled) {
      setInternalValue(newValue);
    }

    if (onChange) {
      onChange(newValue);
    }
  };

  var contextValue = {
    name: name || "card-radio-group-".concat(generateId()),
    required: required,
    disabled: disabled,
    invalid: invalid,
    value: value,
    onChange: handleChange
  };
  return /*#__PURE__*/React.createElement(CardRadioGroupContext.Provider, {
    value: contextValue
  }, /*#__PURE__*/React.createElement(Flex, _extends({
    ref: ref,
    gap: "s",
    className: cx('card-radio-group', className),
    role: "radiogroup",
    direction: direction,
    "aria-disabled": disabled
  }, other), children));
});
var CardRadioGroup = Object.assign(CardRadioGroupRoot, {
  Item: CardRadio,
  Indicator: CardRadioIndicator
});
CardRadioGroup.displayName = 'CardRadioGroup';
CardRadio.displayName = 'CardRadioGroup.Item';
CardRadioIndicator.displayName = 'CardRadioGroup.Indicator';
export default CardRadioGroup;