"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.replace.js");
require("core-js/modules/es.regexp.constructor.js");
var _react = _interopRequireDefault(require("react"));
var _inputNumber = _interopRequireDefault(require("../input-number"));
var _wauProvider = require("../wau-provider");
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
const defaultMoneyIntl = new Intl.NumberFormat('zh-Hans-CN', {
  currency: 'CNY'
});
const moneyIntl = new Intl.NumberFormat('zh-Hans-CN', {
  currency: 'CNY',
  style: 'currency',
  minimumFractionDigits: 2
});
const enMoneyIntl = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD'
});
const ruMoneyIntl = new Intl.NumberFormat('ru-RU', {
  style: 'currency',
  currency: 'RUB'
});
const msMoneyIntl = new Intl.NumberFormat('ms-MY', {
  style: 'currency',
  currency: 'MYR'
});
const getTextByLocale = (locale, paramsText) => {
  let text = paramsText;
  if (typeof text === 'string') {
    text = Number(text);
  }
  if (locale === 'en_US') {
    // english
    return enMoneyIntl.format(text);
  }
  // russian
  if (locale === 'ru_RU') {
    return ruMoneyIntl.format(text);
  }
  // malay
  if (locale === 'ms_MY') {
    return msMoneyIntl.format(text);
  }
  if (locale === undefined) {
    return defaultMoneyIntl.format(text);
  }
  return moneyIntl.format(text);
};
/**
 * 金额组件
 *
 * @param FieldMoneyProps {
 *     text: number;
 *     moneySymbol?: string; }
 */
const FieldMoney = ({
  text,
  mode: type,
  locale = '',
  render,
  renderFormItem,
  fieldProps,
  ...rest
}, ref) => {
  const intl = (0, _wauProvider.useIntl)();
  const moneySymbol = rest.moneySymbol === undefined ? intl.getMessage('moneySymbol', '￥') : rest.moneySymbol;
  if (type === 'read') {
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      ref: ref,
      children: getTextByLocale(moneySymbol ? locale || intl.locale || 'zh-CN' : undefined, text)
    });
    if (render) {
      return render(text, {
        mode: type,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  if (type === 'edit' || type === 'update') {
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_inputNumber.default, {
      ref: ref,
      min: 0,
      precision: 2,
      formatter: value => {
        if (value) {
          return `${moneySymbol} ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
        }
        return '';
      },
      parser: value => value ? value.replace(new RegExp(`\\${moneySymbol}\\s?|(,*)`, 'g'), '') : '',
      style: {
        width: '100%'
      },
      ...rest,
      ...fieldProps
    });
    if (renderFormItem) {
      return renderFormItem(text, {
        mode: type,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
var _default = /*#__PURE__*/_react.default.forwardRef(FieldMoney);
exports.default = _default;