"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.spellNamePath = exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireDefault(require("react"));
var _form = _interopRequireDefault(require("../form"));
var _wauField = _interopRequireDefault(require("../wau-field"));
var _utils = require("../utils");
var _wauInlineFormItem = _interopRequireDefault(require("../wau-inline-form-item"));
var _jsxRuntime = require("react/jsx-runtime");
const SHOW_EMPTY_TEXT_LIST = ['', null, undefined];

/**
 * 拼接用于编辑的 key
 *
 * @param base 基本的 key
 * @param dataIndex 需要拼接的key
 */
const spellNamePath = (base, dataIndex) => {
  if (Array.isArray(dataIndex)) {
    return [base, ...dataIndex];
  }
  return [base, dataIndex];
};

/**
 * 根据不同的类型来转化数值
 *
 * @param text
 * @param valueType
 */
exports.spellNamePath = spellNamePath;
function defaultRenderText(config) {
  var _columnProps$dataInde;
  const {
    text,
    valueType,
    rowData,
    columnProps
  } = config;
  // 如果 valueType === text ，没必要多走一次 render
  if ((!valueType || valueType === 'text') &&
  // valueEnum 存在说明是个select
  !(columnProps !== null && columnProps !== void 0 && columnProps.valueEnum) && config.mode === 'read') {
    // 如果是''、null、undefined 显示columnEmptyText
    return SHOW_EMPTY_TEXT_LIST.includes(text) ? config.columnEmptyText : text;
  }
  if (typeof valueType === 'function' && rowData) {
    // 防止valueType是函数,并且text是''、null、undefined跳过显式设置的columnEmptyText
    return defaultRenderText({
      ...config,
      valueType: valueType(rowData, config.type) || 'text'
    });
  }

  /** 生成公用的 proField dom 配置 */
  const proFieldProps = {
    valueEnum: (0, _utils.runFunction)(columnProps === null || columnProps === void 0 ? void 0 : columnProps.valueEnum, rowData),
    request: columnProps === null || columnProps === void 0 ? void 0 : columnProps.request,
    params: columnProps === null || columnProps === void 0 ? void 0 : columnProps.params,
    proFieldKey: (columnProps === null || columnProps === void 0 || (_columnProps$dataInde = columnProps.dataIndex) === null || _columnProps$dataInde === void 0 ? void 0 : _columnProps$dataInde.toString()) || (columnProps === null || columnProps === void 0 ? void 0 : columnProps.key),
    text: valueType === 'index' || valueType === 'indexBorder' ? config.index : text,
    mode: config.mode,
    emptyText: config.columnEmptyText,
    renderFormItem: undefined,
    valueType: valueType
  };
  const fieldProps = (0, _utils.getFieldPropsOrFormItemProps)(columnProps === null || columnProps === void 0 ? void 0 : columnProps.fieldProps, undefined, columnProps);
  if (config.mode !== 'edit') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauField.default, {
      fieldProps: fieldProps,
      ...proFieldProps
    });
  }

  // 如果是编辑模式，需要用 Form.Item 包一下
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_form.default.Item, {
    shouldUpdate: true,
    noStyle: true,
    children: form => {
      /** 获取 formItemProps Props */
      const formItemProps = (0, _utils.getFieldPropsOrFormItemProps)(columnProps === null || columnProps === void 0 ? void 0 : columnProps.formItemProps, form, {
        rowKey: config.recordKey || config.index,
        rowIndex: config.index,
        ...columnProps,
        isEditable: true
      });
      const messageVariables = {
        label: (columnProps === null || columnProps === void 0 ? void 0 : columnProps.title) || '此项',
        type: (columnProps === null || columnProps === void 0 ? void 0 : columnProps.valueType) || '文本',
        ...(formItemProps === null || formItemProps === void 0 ? void 0 : formItemProps.messageVariables)
      };
      const name = spellNamePath(config.recordKey || config.index, (columnProps === null || columnProps === void 0 ? void 0 : columnProps.key) || (columnProps === null || columnProps === void 0 ? void 0 : columnProps.dataIndex) || config.index);
      const inputDom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauInlineFormItem.default, {
        name: name,
        ...formItemProps,
        messageVariables: messageVariables,
        initialValue: text || (formItemProps === null || formItemProps === void 0 ? void 0 : formItemProps.initialValue),
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauField.default, {
          fieldProps: (0, _utils.getFieldPropsOrFormItemProps)(columnProps === null || columnProps === void 0 ? void 0 : columnProps.fieldProps, form, {
            ...columnProps,
            rowKey: config.recordKey || config.index,
            rowIndex: config.index,
            isEditable: true
          }),
          ...proFieldProps
        })
      });
      /** RenderFormItem 需要被自定义 */
      if (columnProps !== null && columnProps !== void 0 && columnProps.renderFormItem) {
        var _columnProps$renderFo;
        const renderDom = (_columnProps$renderFo = columnProps.renderFormItem) === null || _columnProps$renderFo === void 0 ? void 0 : _columnProps$renderFo.call(columnProps, {
          ...columnProps,
          index: config.index,
          isEditable: true,
          type: 'table'
        }, {
          defaultRender: () => inputDom,
          type: 'form',
          recordKey: config.recordKey,
          record: form.getFieldValue([config.recordKey || config.index]),
          isEditable: true
        }, form);
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauInlineFormItem.default, {
          name: spellNamePath(config.recordKey || config.index, (columnProps === null || columnProps === void 0 ? void 0 : columnProps.key) || (columnProps === null || columnProps === void 0 ? void 0 : columnProps.dataIndex) || config.index),
          ...formItemProps,
          initialValue: text || (formItemProps === null || formItemProps === void 0 ? void 0 : formItemProps.initialValue),
          messageVariables: messageVariables,
          children: renderDom
        });
      }
      return inputDom;
    }
  });
}
var _default = defaultRenderText;
exports.default = _default;