import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import * as React from 'react';
import cx from 'classnames';
import { RcCheckbox } from '../components';
import { composeRef } from '../components/rc-util/ref';
import { ConfigContext } from '../config-provider/context';
import RadioGroupContext from './context';

var InternalRadio = function InternalRadio(props, ref) {
  var context = React.useContext(RadioGroupContext);

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var innerRef = React.useRef();
  var mergedRef = composeRef(ref, innerRef);

  var handleChange = function handleChange(evt) {
    var _props$onChange, _context$onChange;

    (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, evt);
    context === null || context === void 0 ? void 0 : (_context$onChange = context.onChange) === null || _context$onChange === void 0 ? void 0 : _context$onChange.call(context, evt);
  };

  var className = props.className,
      children = props.children,
      style = props.style,
      prefixCls = props.prefixCls,
      restProps = _objectWithoutProperties(props, ["className", "children", "style", "prefixCls"]);

  var radioCls = getPrefixClassName('radio', prefixCls);

  var radioProps = _objectSpread({}, restProps);

  if (context) {
    radioProps.name = context.name;
    radioProps.onChange = handleChange;
    radioProps.checked = props.value === context.value;
    radioProps.disabled = props.disabled || context.disabled;
  }

  return /*#__PURE__*/React.createElement("label", {
    className: cx("".concat(radioCls, "-wrapper"), radioProps.checked && "".concat(radioCls, "-wrapper-checked"), radioProps.disabled && "".concat(radioCls, "-wrapper-disabled"), radioProps.showType && "".concat(radioCls, "-wrapper-show-type-").concat(radioProps.showType), className),
    style: style,
    onMouseEnter: props.onMouseEnter,
    onMouseLeave: props.onMouseLeave
  }, /*#__PURE__*/React.createElement(RcCheckbox, Object.assign({}, radioProps, {
    prefixCls: radioCls,
    ref: mergedRef
  })), children !== undefined ? /*#__PURE__*/React.createElement("span", null, children) : null);
};

var Radio = /*#__PURE__*/React.forwardRef(InternalRadio);
Radio.displayName = 'Radio';
Radio.defaultProps = {
  type: 'radio'
};
export default Radio;