import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useState, useContext } from 'react';
import moment from 'moment';
import ConfigProvider from '../config-provider';
import DatePicker from '../date-picker';
import { useIntl } from '../wau-provider';
import { parseValueToMoment } from '../utils';
import FieldLabel from '../wau-field-label';
import './style';

/**
 * 日期选择组件
 *
 * @param
 */
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const FieldDatePicker = ({
  text,
  mode,
  format,
  label,
  light,
  render,
  renderFormItem,
  plain,
  showTime,
  fieldProps,
  picker,
  bordered
}, ref) => {
  const intl = useIntl();
  const size = useContext(ConfigProvider.SizeContext);
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-date-picker');
  const [open, setOpen] = useState(false);
  if (mode === 'read') {
    const dom = /*#__PURE__*/_jsx("span", {
      ref: ref,
      children: text ? moment(text).format(format || 'YYYY-MM-DD') : '-'
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/_jsx("span", {
        children: dom
      }));
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    let dom;
    const {
      disabled,
      value,
      onChange,
      allowClear,
      placeholder = intl.getMessage('tableForm.selectPlaceholder', '请选择')
    } = fieldProps;
    const momentValue = parseValueToMoment(value);
    if (light) {
      const valueStr = momentValue && momentValue.format(format) || '';
      dom = /*#__PURE__*/_jsxs("div", {
        className: `${prefixCls}-light`,
        onClick: () => {
          setOpen(true);
        },
        children: [/*#__PURE__*/_jsx(DatePicker, {
          picker: picker,
          showTime: showTime,
          format: format,
          ref: ref,
          ...fieldProps,
          value: momentValue,
          onChange: v => {
            if (onChange) {
              onChange(v);
            }
            setTimeout(() => {
              setOpen(false);
            }, 0);
          },
          onOpenChange: setOpen,
          open: open
        }), /*#__PURE__*/_jsx(FieldLabel, {
          label: label,
          disabled: disabled,
          placeholder: placeholder,
          size: size,
          value: valueStr,
          onClear: () => {
            if (onChange) {
              onChange(null);
            }
          },
          allowClear: allowClear,
          bordered: bordered,
          expanded: open
        })]
      });
    } else {
      dom = /*#__PURE__*/_jsx(DatePicker, {
        picker: picker,
        showTime: showTime,
        format: format,
        placeholder: placeholder,
        bordered: plain === undefined ? true : !plain,
        ref: ref,
        ...fieldProps,
        value: momentValue
      });
    }
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
export default /*#__PURE__*/React.forwardRef(FieldDatePicker);