"use strict";

exports.__esModule = true;
exports.default = void 0;

var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");

var _tslib = require("tslib");

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

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

var _meetReactComponentOne = require("@alifd/meet-react-component-one");

var _utils = require("../utils");

var _tree = require("../utils/tree");

var _view = _interopRequireDefault(require("../view"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var getInitValue = function (props) {
  var ret = [];

  if ('value' in props) {
    ret = props.value;
  } else if (!(0, _utils.isUndef)(props.defaultValue)) {
    ret = props.defaultValue;
  }

  return ret;
};

var RadioGroup = function (props, ref) {
  var _props$size = props.size,
      size = _props$size === void 0 ? 'medium' : _props$size,
      _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'ver' : _props$direction,
      children = props.children,
      groupDisabled = props.disabled,
      defaultValue = props.defaultValue,
      value = props.value,
      readOnly = props.readOnly,
      onChange = props.onChange,
      className = props.className,
      isPreview = props.isPreview,
      renderPreview = props.renderPreview,
      previewClassNameProp = props.previewClassName,
      previewStyle = props.previewStyle,
      dataSource = props.dataSource,
      labelAlign = props.labelAlign,
      others = (0, _tslib.__rest)(props, ["size", "prefix", "direction", "children", "disabled", "defaultValue", "value", "readOnly", "onChange", "className", "isPreview", "renderPreview", "previewClassName", "previewStyle", "dataSource", "labelAlign"]);

  var _useState = (0, _react.useState)(getInitValue(props)),
      groupValue = _useState[0],
      setGroupValue = _useState[1];

  var isControlled = ('value' in props);
  var previewClassName = (0, _classnames.default)(previewClassNameProp, prefix + "form-item-input--preview-" + size);
  var groupCls = (0, _classnames.default)(prefix + "radio-group", prefix + "radio-group--" + direction, prefix + "radio-group--" + labelAlign + "-" + size, className);
  (0, _react.useEffect)(function () {
    var newValue = getInitValue(props);

    if (isControlled) {
      setGroupValue(newValue);
    }
  }, [value, groupValue]);

  var handleChange = function (selected, radioValue, e, isDisabled) {
    if (!isDisabled && !readOnly) {
      if (!isControlled) {
        setGroupValue(radioValue);
      }

      if (selected && (0, _utils.isFunction)(onChange)) {
        onChange(radioValue, e);
      }
    }
  };

  var content;
  var ds = dataSource;

  if (!ds && _react.Children.count(children) > 0) {
    ds = (0, _utils.getDataSourceFromChildren)(children);
  }

  if ((0, _utils.isValidArray)(ds)) {
    content = ds.map(function (item, index) {
      var _classNames;

      var itemData = {
        style: {},
        value: null,
        checked: false,
        label: '',
        text: '',
        disabled: false,
        size: size
      };
      var itemOnClick = item.onClick,
          itemOnChange = item.onChange,
          itemLabel = item.label,
          itemText = item.text,
          itemRest = (0, _tslib.__rest)(item, ["onClick", "onChange", "label", "text"]);

      if ((0, _utils.isString)(item) || (0, _utils.isNumber)(item)) {
        itemData.value = item;
      } else {
        itemData = Object.assign(Object.assign({}, itemData), itemRest);
      }

      var isDisabled = groupDisabled || itemData.disabled;
      var label = itemLabel || itemText || itemData.value;
      var isLast = index === ds.length - 1;
      return /*#__PURE__*/(0, _react.createElement)(_radio.default, _extends({}, itemData, {
        key: "radio_" + index,
        className: (0, _classnames.default)((_classNames = {}, _classNames[prefix + "radio-item--" + direction + "-" + size] = !isLast, _classNames)),
        disabled: isDisabled,
        checked: itemData.checked || itemData.value === groupValue,
        onChange: function (selected, e) {
          if ((0, _utils.isFunction)(itemOnChange)) {
            itemOnChange(selected, e);
          }

          handleChange(selected, itemData.value, e, isDisabled);
        },
        onClick: itemOnClick
      }), label);
    });
  }

  if (isPreview) {
    var arr = (0, _tree.getItemsFromDataSource)(ds, groupValue);
    var previewItem = {};

    if ((0, _utils.isValidArray)(arr)) {
      previewItem = arr[0];
    }

    if ((0, _utils.isFunction)(renderPreview)) {
      return /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: previewClassName,
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(previewStyle)
      }, renderPreview(previewItem));
    }

    return /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
      className: previewClassName,
      style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(previewStyle)
    }, previewItem.label || previewItem.text);
  }

  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    ref: ref,
    className: groupCls
  }, others), content);
};

RadioGroup.displayName = 'Radio.Group';

var _default = /*#__PURE__*/(0, _react.forwardRef)(RadioGroup);

exports.default = _default;