"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _radio = _interopRequireDefault(require("./radio"));

var _context = require("../config-provider/context");

var _SizeContext = _interopRequireDefault(require("../config-provider/SizeContext"));

var _context2 = require("./context");

var _grid = _interopRequireDefault(require("../grid"));

// import useMergedState from '../components/rc-util/hooks/useMergedState';
var Row = _grid.default.Row,
    Col = _grid.default.Col;
var RadioGroup = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _React$useContext = React.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var size = React.useContext(_SizeContext.default);

  var _React$useState = React.useState(),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      value = _React$useState2[0],
      setValue = _React$useState2[1];

  var optionType = props.optionType;

  var _React$useState3 = React.useState(optionType),
      _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
      radiotype = _React$useState4[0],
      setRadiotype = _React$useState4[1];

  var prevRadiotype = React.useRef(); // const [value, setValue] = useMergedState(props.defaultValue, {
  //   value: props.value,
  // });

  React.useEffect(function () {
    setValue(typeof props.value === 'undefined' ? props.defaultValue : props.value);
  }, [props.value, props.defaultValue]);

  var onRadioChange = function onRadioChange(ev) {
    var lastValue = value;
    var val = ev.target.value;

    if (!('value' in props)) {
      setValue(val);
    }

    var onChange = props.onChange;

    if (onChange && val !== lastValue) {
      onChange(ev);
    }
  };

  var updateRadioType = function updateRadioType(type) {
    if (prevRadiotype.current === type) return;
    setRadiotype(type);
    prevRadiotype.current = type;
  };

  var renderGroup = function renderGroup() {
    var customizePrefixCls = props.prefixCls,
        _props$className = props.className,
        className = _props$className === void 0 ? '' : _props$className,
        options = props.options,
        _props$buttonStyle = props.buttonStyle,
        buttonStyle = _props$buttonStyle === void 0 ? 'outline' : _props$buttonStyle,
        disabled = props.disabled,
        children = props.children,
        customizeSize = props.size,
        style = props.style,
        id = props.id,
        onMouseEnter = props.onMouseEnter,
        onMouseLeave = props.onMouseLeave,
        rowProps = props.rowProps,
        colProps = props.colProps,
        direction = props.direction;
    var prefixCls = getPrefixClassName('radio', customizePrefixCls);
    var groupPrefixCls = "".concat(prefixCls, "-group");
    var childrenToRender = children; // 如果存在 options, 优先使用

    if (options && options.length > 0) {
      var optionsPrefixCls = radiotype === 'button' ? "".concat(prefixCls, "-button") : prefixCls;
      childrenToRender = options.map(function (option) {
        if (typeof option === 'string') {
          // 此处类型自动推导为 string
          return /*#__PURE__*/React.createElement(_radio.default, {
            key: option,
            prefixCls: optionsPrefixCls,
            disabled: disabled,
            value: option,
            checked: value === option
          }, option);
        } // 此处类型自动推导为 { label: string value: string }


        return /*#__PURE__*/React.createElement(_radio.default, {
          key: "radio-group-value-options-".concat(option.value),
          prefixCls: optionsPrefixCls,
          disabled: option.disabled || disabled,
          value: option.value,
          checked: value === option.value,
          style: option.style
        }, option.label);
      });
    }

    var mergedSize = customizeSize || size;
    var classString = (0, _classnames.default)(groupPrefixCls, "".concat(groupPrefixCls, "-").concat(buttonStyle), (0, _defineProperty2.default)({}, "".concat(groupPrefixCls, "-").concat(mergedSize), mergedSize), className);
    var rowCls = (0, _classnames.default)(rowProps === null || rowProps === void 0 ? void 0 : rowProps.className, (0, _defineProperty2.default)({}, "".concat(groupPrefixCls, "-row-vertical"), direction === 'vertical'));
    var rowGutter = direction === 'vertical' ? [0, 20] : [32];
    return /*#__PURE__*/React.createElement("div", {
      className: classString,
      style: style,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      id: id,
      ref: ref
    }, radiotype !== 'button' ? /*#__PURE__*/React.createElement(Row, Object.assign({
      gutter: rowGutter,
      className: rowCls
    }, rowProps), React.Children.map(childrenToRender, function (item, index) {
      return (
        /* eslint-disable react/no-array-index-key */
        item ? /*#__PURE__*/React.createElement(Col, Object.assign({
          key: index
        }, colProps), item) : null
      );
    })) : /*#__PURE__*/React.createElement(React.Fragment, null, React.Children.map(childrenToRender, function (item) {
      return item;
    })));
  };

  return /*#__PURE__*/React.createElement(_context2.RadioGroupContextProvider, {
    value: {
      onChange: onRadioChange,
      value: value,
      disabled: props.disabled,
      name: props.name,
      updateRadioType: updateRadioType
    }
  }, renderGroup());
});

var _default = /*#__PURE__*/React.memo(RadioGroup);

exports.default = _default;