import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.replace.js";
import toNumber from 'lodash/toNumber';
import React, { useMemo } from 'react';
import Progress from '../progress';
import InputNumber from '../input-number';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
export function getProgressStatus(text) {
  if (typeof text !== 'number') {
    return 'exception';
  }
  if (text === 100) {
    return 'success';
  }
  if (text < 0) {
    return 'exception';
  }
  if (text < 100) {
    return 'active';
  }
  return 'normal';
}

/**
 * 进度条组件
 *
 * @param
 */
const FieldProgress = ({
  text,
  mode,
  render,
  plain,
  renderFormItem,
  fieldProps,
  ...rest
}, ref) => {
  const realValue = useMemo(() => typeof text === 'string' && text.includes('%') ? toNumber(text.replace('%', '')) : toNumber(text), [text]);
  if (mode === 'read') {
    const dom = /*#__PURE__*/_jsx(Progress, {
      ref: ref,
      size: "small",
      style: {
        minWidth: 100,
        maxWidth: 320
      },
      percent: realValue,
      steps: plain ? 10 : undefined,
      status: getProgressStatus(realValue),
      ...fieldProps
    });
    if (render) {
      return render(realValue, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    const dom = /*#__PURE__*/_jsx(InputNumber, {
      ref: ref,
      ...rest,
      ...fieldProps
    });
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
export default /*#__PURE__*/React.forwardRef(FieldProgress);