import React from 'react';
import moment from 'moment';
import Tooltip from '../tooltip';
import DatePicker from '../date-picker';
import { parseValueToMoment } from '../utils';
import { useIntl } from '../wau-provider';
import './style';

/**
 * 与当前的时间进行比较 http://momentjs.cn/docs/displaying/fromnow.html
 *
 * @param
 */
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
const FieldFromNow = ({
  text,
  mode,
  render,
  renderFormItem,
  format,
  fieldProps
}) => {
  const intl = useIntl();
  if (mode === 'read') {
    const dom = /*#__PURE__*/_jsx(Tooltip, {
      title: moment(text).format(format || 'YYYY-MM-DD HH:mm:ss'),
      children: moment(text).fromNow()
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/_jsx(_Fragment, {
        children: dom
      }));
    }
    return /*#__PURE__*/_jsx(_Fragment, {
      children: dom
    });
  }
  if (mode === 'edit' || mode === 'update') {
    const placeholder = intl.getMessage('tableForm.selectPlaceholder', '请选择');
    const momentValue = parseValueToMoment(fieldProps.value);
    const dom = /*#__PURE__*/_jsx(DatePicker, {
      placeholder: placeholder,
      ...fieldProps,
      value: momentValue
    });
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
export default FieldFromNow;