"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 _bem = require("@zarm-design/bem");

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

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

var _list = _interopRequireDefault(require("../list"));

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

var getValue = function getValue(props, defaultValue) {
  var _ref, _props$value;

  return (_ref = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : props.defaultValue) !== null && _ref !== void 0 ? _ref : defaultValue;
};

var RadioGroup = function RadioGroup(props) {
  var _ref2;

  var _useState = (0, _react.useState)(getValue(props)),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  var type = props.type,
      block = props.block,
      disabled = props.disabled,
      listIconAlign = props.listIconAlign,
      compact = props.compact,
      className = props.className,
      style = props.style;

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls;

  var bem = (0, _bem.createBEM)('radio-group', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref2 = {}, (0, _defineProperty2.default)(_ref2, "".concat(type), !!type), (0, _defineProperty2.default)(_ref2, "block", block), (0, _defineProperty2.default)(_ref2, "disabled", disabled), (0, _defineProperty2.default)(_ref2, 'button-compact', compact), _ref2), className]);
  (0, _react.useEffect)(function () {
    if (props.value === undefined) return;
    if (props.value === value) return;
    setValue(getValue(props));
  }, [props.value]);
  return /*#__PURE__*/_react.default.createElement(_context.RadioGroupContext.Provider, {
    value: {
      value: value,
      type: type,
      block: block,
      disabled: disabled,
      listIconAlign: listIconAlign,
      compact: compact,
      check: function check(v) {
        var _props$onChange;

        setValue(v);
        (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, v);
      }
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: cls,
    style: style
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('inner')
  }, type === 'list' ? /*#__PURE__*/_react.default.createElement(_list.default, null, props.children) : props.children)));
};

RadioGroup.displayName = 'RadioGroup';
RadioGroup.defaultProps = {
  block: false,
  disabled: false,
  compact: false,
  listIconAlign: 'before'
};
var _default = RadioGroup;
exports.default = _default;