import { __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { useContext, useMemo } from 'react';
import RadioContext from './RadioContext';
import Checker from '../checkbox/Checker';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

const Radio = props => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('radio', prefixCls);

  const _a = useContext(RadioContext),
        {
    parent
  } = _a,
        context = __rest(_a, ["parent"]);

  const checked = useMemo(() => {
    return parent ? context.checked === props.name : props.checked;
  }, [context.checked]);

  const toggle = () => {
    const emitter = parent ? context.toggle : () => {};
    emitter(props.name);
  };

  return _jsx(Checker, Object.assign({}, props, {
    bem: bem,
    role: "radio",
    parent: parent,
    checked: checked,
    onToggle: toggle
  }));
};

export default Radio;