"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.array.reduce.js");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _radio = _interopRequireDefault(require("../radio"));
var _spin = _interopRequireDefault(require("../spin"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _wauSelectField = require("../wau-select-field");
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }
/**
 * 单选组件
 *
 * @param param0
 * @param ref
 */
const FieldRadio = ({
  radioType,
  renderFormItem,
  mode,
  render,
  ...rest
}, ref) => {
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const layoutClassName = getPrefixCls('pro-field-radio');
  const [loading, options, fetchData] = (0, _wauSelectField.useFieldFetchData)(rest);
  const radioRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(ref, () => ({
    ...(radioRef.current || {}),
    fetchData: () => fetchData()
  }));
  if (loading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_spin.default, {
      size: "small"
    });
  }
  if (mode === 'read') {
    const optionsValueEnum = options !== null && options !== void 0 && options.length ? options === null || options === void 0 ? void 0 : options.reduce((pre, cur) => {
      return {
        ...pre,
        [cur.value]: cur.label
      };
    }, {}) : undefined;
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: (0, _wauSelectField.proFieldParsingText)(rest.text, (0, _wauSelectField.ObjToMap)(rest.valueEnum || optionsValueEnum))
    });
    if (render) {
      return render(rest.text, {
        mode,
        ...rest.fieldProps
      }, dom) || null;
    }
    return dom;
  }
  if (mode === 'edit') {
    var _rest$fieldProps;
    const RadioComponents = radioType === 'button' ? _radio.default.Button : _radio.default;
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_radio.default.Group, {
      ref: radioRef,
      ...rest.fieldProps,
      className: (0, _classnames.default)((_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 ? void 0 : _rest$fieldProps.className, `${layoutClassName}-${rest.fieldProps.layout || 'horizontal'}`),
      options: undefined,
      children: options === null || options === void 0 ? void 0 : options.map(option => {
        if (typeof option === 'string') {
          return {
            label: option,
            value: option
          };
        }
        return option;
      }).map(item => /*#__PURE__*/(0, _jsxRuntime.jsx)(RadioComponents, {
        ...item,
        children: item.label
      }, item.value))
    });
    if (renderFormItem) {
      return renderFormItem(rest.text, {
        mode,
        ...rest.fieldProps
      }, dom) || null;
    }
    return dom;
  }
  return null;
};
var _default = /*#__PURE__*/_react.default.forwardRef(FieldRadio);
exports.default = _default;