import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.replace.js";
import "core-js/modules/es.regexp.constructor.js";
import React, { useMemo } from 'react';
import toNumber from 'lodash/toNumber';
import InputNumber from '../input-number';
import { getColorByRealValue, getSymbolByRealValue, getRealTextWithPrecision } from './util';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * 百分比组件
 *
 * @param PercentPropInt
 */
const FieldPercent = ({
  text,
  prefix,
  precision,
  showSymbol,
  suffix = '%',
  mode,
  showColor = false,
  render,
  renderFormItem,
  fieldProps,
  ...rest
}, ref) => {
  const realValue = useMemo(() => typeof text === 'string' && text.includes('%') ? toNumber(text.replace('%', '')) : toNumber(text), [text]);
  if (mode === 'read') {
    /** 颜色有待确定, 根据提供 colors: ['正', '负'] | boolean */
    const style = showColor ? {
      color: getColorByRealValue(realValue)
    } : {};
    const dom = /*#__PURE__*/_jsxs("span", {
      style: style,
      ref: ref,
      children: [prefix && /*#__PURE__*/_jsx("span", {
        children: prefix
      }), showSymbol && /*#__PURE__*/_jsxs(_Fragment, {
        children: [getSymbolByRealValue(realValue), " "]
      }), getRealTextWithPrecision(Math.abs(realValue), precision), suffix && suffix]
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps,
        prefix,
        precision,
        showSymbol,
        suffix
      }, dom);
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    const dom = /*#__PURE__*/_jsx(InputNumber, {
      ref: ref,
      formatter: value => {
        if (value && prefix) {
          return `${prefix} ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
        }
        return value;
      },
      parser: value => value ? value.replace(new RegExp(`\\${prefix}\\s?|(,*)`, 'g'), '') : '',
      ...fieldProps,
      ...rest
    });
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
export default /*#__PURE__*/React.forwardRef(FieldPercent);