"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "FieldCode", {
  enumerable: true,
  get: function () {
    return _wauCodeField.default;
  }
});
Object.defineProperty(exports, "FieldDatePicker", {
  enumerable: true,
  get: function () {
    return _wauDatePickerField.default;
  }
});
Object.defineProperty(exports, "FieldIndexColumn", {
  enumerable: true,
  get: function () {
    return _wauIndexColumnField.default;
  }
});
Object.defineProperty(exports, "FieldMoney", {
  enumerable: true,
  get: function () {
    return _wauMoneyField.default;
  }
});
Object.defineProperty(exports, "FieldPercent", {
  enumerable: true,
  get: function () {
    return _wauPercentField.default;
  }
});
Object.defineProperty(exports, "FieldProgress", {
  enumerable: true,
  get: function () {
    return _wauProgressField.default;
  }
});
Object.defineProperty(exports, "FieldRangePicker", {
  enumerable: true,
  get: function () {
    return _wauRangePickerField.default;
  }
});
Object.defineProperty(exports, "FieldStatus", {
  enumerable: true,
  get: function () {
    return _wauStatusField.default;
  }
});
Object.defineProperty(exports, "FieldText", {
  enumerable: true,
  get: function () {
    return _wauTextField.default;
  }
});
Object.defineProperty(exports, "FieldTimePicker", {
  enumerable: true,
  get: function () {
    return _wauTimePickerField.default;
  }
});
Object.defineProperty(exports, "FiledSelect", {
  enumerable: true,
  get: function () {
    return _wauSelectField.default;
  }
});
exports.defaultRenderText = exports.default = void 0;
Object.defineProperty(exports, "proFieldParsingText", {
  enumerable: true,
  get: function () {
    return _wauSelectField.proFieldParsingText;
  }
});
Object.defineProperty(exports, "proFieldParsingValueEnumToArray", {
  enumerable: true,
  get: function () {
    return _wauSelectField.proFieldParsingValueEnumToArray;
  }
});
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _avatar = _interopRequireDefault(require("../avatar"));
var _utils = require("../utils");
var _wauProvider = _interopRequireWildcard(require("../wau-provider"));
var _wauPercentField = _interopRequireDefault(require("../wau-percent-field"));
var _wauIndexColumnField = _interopRequireDefault(require("../wau-index-column-field"));
var _wauProgressField = _interopRequireDefault(require("../wau-progress-field"));
var _wauMoneyField = _interopRequireDefault(require("../wau-money-field"));
var _wauDatePickerField = _interopRequireDefault(require("../wau-date-picker-field"));
var _wauFromNowField = _interopRequireDefault(require("../wau-from-now-field"));
var _wauRangePickerField = _interopRequireDefault(require("../wau-range-picker-field"));
var _wauCodeField = _interopRequireDefault(require("../wau-code-field"));
var _wauTimePickerField = _interopRequireDefault(require("../wau-time-picker-field"));
var _wauTextField = _interopRequireDefault(require("../wau-text-field"));
var _wauTextAreaField = _interopRequireDefault(require("../wau-text-area-field"));
var _wauPasswordField = _interopRequireDefault(require("../wau-password-field"));
var _wauStatusField = _interopRequireDefault(require("../wau-status-field"));
var _wauOptionsField = _interopRequireDefault(require("../wau-options-field"));
var _wauSelectField = _interopRequireWildcard(require("../wau-select-field"));
var _wauCheckboxField = _interopRequireDefault(require("../wau-checkbox-field"));
var _wauRateField = _interopRequireDefault(require("../wau-rate-field"));
var _wauSwitchField = _interopRequireDefault(require("../wau-switch-field"));
var _wauDigitField = _interopRequireDefault(require("../wau-digit-field"));
var _wauSecondField = _interopRequireDefault(require("../wau-second-field"));
var _wauRadioField = _interopRequireDefault(require("../wau-radio-field"));
var _wauImageField = _interopRequireDefault(require("../wau-image-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; }
/** 默认的 Field 需要实现的功能 */

/** Value type by function */

/**
 * Render valueType object
 *
 * @param text String | number
 * @param valueType ProColumnsValueObjectType
 */
const defaultRenderTextByObject = (text, valueType, props) => {
  const pickFormItemProps = (0, _utils.pickProProps)(props.fieldProps);
  if (valueType.type === 'progress') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauProgressField.default, {
      ...props,
      text: text,
      fieldProps: {
        status: valueType.status ? valueType.status : undefined,
        ...pickFormItemProps
      }
    });
  }
  if (valueType.type === 'money') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauMoneyField.default, {
      locale: valueType.locale,
      ...props,
      fieldProps: pickFormItemProps,
      text: text,
      moneySymbol: valueType.moneySymbol
    });
  }
  if (valueType.type === 'percent') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauPercentField.default, {
      ...props,
      text: text,
      showSymbol: valueType.showSymbol,
      precision: valueType.precision,
      fieldProps: pickFormItemProps,
      showColor: valueType.showColor
    });
  }
  if (valueType.type === 'image') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauImageField.default, {
      ...props,
      text: text,
      width: valueType.width
    });
  }
  return text;
};

/**
 * 根据不同的类型来转化数值
 *
 * @param text
 * @param valueType
 */
const defaultRenderText = (text, valueType, props, valueTypeMap) => {
  if (typeof valueType === 'object') {
    return defaultRenderTextByObject(text, valueType, props);
  }
  const {
    mode = 'read',
    emptyText = '-'
  } = props;
  const customValueTypeConfig = valueTypeMap && valueTypeMap[valueType];
  if (customValueTypeConfig) {
    // eslint-disable-next-line no-param-reassign
    delete props.ref;
    if (mode === 'read') {
      var _customValueTypeConfi;
      return (_customValueTypeConfi = customValueTypeConfig.render) === null || _customValueTypeConfi === void 0 ? void 0 : _customValueTypeConfi.call(customValueTypeConfig, text, {
        text: text,
        ...props,
        mode: mode || 'read'
      }, /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: text
      }));
    }
    if (mode === 'update' || mode === 'edit') {
      var _customValueTypeConfi2;
      return (_customValueTypeConfi2 = customValueTypeConfig.renderFormItem) === null || _customValueTypeConfi2 === void 0 ? void 0 : _customValueTypeConfi2.call(customValueTypeConfig, text, {
        text: text,
        ...props
      }, /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: text
      }));
    }
  }
  if (emptyText !== false && mode === 'read' && valueType !== 'option' && valueType !== 'switch') {
    if (typeof text !== 'boolean' && typeof text !== 'number' && !text) {
      const {
        fieldProps,
        render
      } = props;
      if (render) {
        return render(text, {
          mode,
          ...fieldProps
        }, /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
          children: emptyText
        }));
      }
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: emptyText
      });
    }
  }

  // eslint-disable-next-line no-param-reassign
  delete props.emptyText;

  /** 如果是金额的值 */
  if (valueType === 'money') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauMoneyField.default, {
      ...props,
      text: text
    });
  }

  /** 如果是日期的值 */
  if (valueType === 'date') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDatePickerField.default, {
      text: text,
      format: "YYYY-MM-DD",
      ...props
    });
  }

  /** 如果是周的值 */
  if (valueType === 'dateWeek') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDatePickerField.default, {
      text: text,
      format: "YYYY-wo",
      picker: "week",
      ...props
    });
  }

  /** 如果是月的值 */
  if (valueType === 'dateMonth') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDatePickerField.default, {
      text: text,
      format: "YYYY-MM",
      picker: "month",
      ...props
    });
  }

  /** 如果是季度的值 */
  if (valueType === 'dateQuarter') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDatePickerField.default, {
      text: text,
      format: "YYYY-\\QQ",
      picker: "quarter",
      ...props
    });
  }

  /** 如果是年的值 */
  if (valueType === 'dateYear') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDatePickerField.default, {
      text: text,
      format: "YYYY",
      picker: "year",
      ...props
    });
  }

  /** 如果是日期范围的值 */
  if (valueType === 'dateRange') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauRangePickerField.default, {
      text: text,
      format: "YYYY-MM-DD",
      ...props
    });
  }

  /** 如果是日期加时间类型的值 */
  if (valueType === 'dateTime') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDatePickerField.default, {
      text: text,
      format: "YYYY-MM-DD HH:mm:ss",
      showTime: true,
      ...props
    });
  }

  /** 如果是日期加时间类型的值的值 */
  if (valueType === 'dateTimeRange') {
    // 值不存在的时候显示 "-"
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauRangePickerField.default, {
      text: text,
      format: "YYYY-MM-DD HH:mm:ss",
      showTime: true,
      ...props
    });
  }

  /** 如果是时间类型的值 */
  if (valueType === 'time') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauTimePickerField.default, {
      text: text,
      format: "HH:mm:ss",
      ...props
    });
  }
  if (valueType === 'fromNow') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFromNowField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'index') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauIndexColumnField.default, {
      children: text + 1
    });
  }
  if (valueType === 'indexBorder') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauIndexColumnField.default, {
      border: true,
      children: text + 1
    });
  }
  if (valueType === 'progress') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauProgressField.default, {
      ...props,
      text: text
    });
  }
  /** 百分比, 默认展示符号, 不展示小数位 */
  if (valueType === 'percent') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauPercentField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'avatar' && typeof text === 'string' && props.mode === 'read') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_avatar.default, {
      src: text,
      size: 22,
      shape: "circle"
    });
  }
  if (valueType === 'code') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauCodeField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'jsonCode') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauCodeField.default, {
      text: text,
      language: "json",
      ...props
    });
  }
  if (valueType === 'textarea') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauTextAreaField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'digit') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauDigitField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'second') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauSecondField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'select' || valueType === 'text' && (props.valueEnum || props.request)) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauSelectField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'checkbox') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauCheckboxField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'radio') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauRadioField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'radioButton') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauRadioField.default, {
      radioType: "button",
      text: text,
      ...props
    });
  }
  if (valueType === 'rate') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauRateField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'switch') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauSwitchField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'option') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauOptionsField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'password') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauPasswordField.default, {
      text: text,
      ...props
    });
  }
  if (valueType === 'image') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauImageField.default, {
      text: text,
      ...props
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauTextField.default, {
    text: text,
    ...props
  });
};
exports.defaultRenderText = defaultRenderText;
/** ProField 的类型 */

const ProField = ({
  text,
  valueType = 'text',
  onChange,
  value,
  ...rest
}, ref) => {
  var _ref;
  const intl = (0, _wauProvider.useIntl)();
  const context = (0, _react.useContext)(_wauProvider.default);
  const fieldProps = (value || onChange || (rest === null || rest === void 0 ? void 0 : rest.fieldProps)) && {
    value,
    // fieldProps 优先级更高，在类似 LightFilter 场景下需要覆盖默认的 value 和 onChange
    ...(0, _utils.omitUndefined)(rest === null || rest === void 0 ? void 0 : rest.fieldProps),
    onChange: (...restParams) => {
      var _rest$fieldProps, _rest$fieldProps$onCh;
      onChange === null || onChange === void 0 ? void 0 : onChange(...restParams);
      rest === null || rest === void 0 || (_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 || (_rest$fieldProps$onCh = _rest$fieldProps.onChange) === null || _rest$fieldProps$onCh === void 0 ? void 0 : _rest$fieldProps$onCh.call(_rest$fieldProps, ...restParams);
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: defaultRenderText((_ref = text !== null && text !== void 0 ? text : fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) !== null && _ref !== void 0 ? _ref : '', valueType || 'text', {
      ...rest,
      mode: rest.mode || 'read',
      ref,
      placeholder: intl.getMessage('tableForm.inputPlaceholder', '请输入'),
      fieldProps: (0, _utils.pickProProps)(fieldProps)
    }, context.valueTypeMap)
  });
};
var _default = /*#__PURE__*/_react.default.forwardRef(ProField);
exports.default = _default;