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 DatePicker from '../date-picker';
import ConfigProvider from '../config-provider';
import { parseValueToMoment } from '../utils';
import { useIntl } from '../wau-provider';
import FieldLabel from '../wau-field-label';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const ACTIVE_PICKER_INDEX_LEFT = 0;
const ACTIVE_PICKER_INDEX_RIGHT = 1;

/**
 * 日期范围选择组件
 *
 * @param
 */
const FieldRangePicker = ({
  text,
  mode,
  format,
  label,
  render,
  renderFormItem,
  plain,
  showTime,
  fieldProps,
  light
}, ref) => {
  const intl = useIntl();
  const size = useContext(ConfigProvider.SizeContext);
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-date-picker');
  const [startText, endText] = Array.isArray(text) ? text : [];
  const [open, setOpen] = useState(false);
  // activePickerIndex for https://github.com/ant-design/ant-design/issues/22158
  const [activePickerIndex, setActivePickerIndex] = useState();
  const parsedStartText = startText ? moment(startText).format(format || 'YYYY-MM-DD') : '';
  const parsedEndText = endText ? moment(endText).format(format || 'YYYY-MM-DD') : '';
  if (mode === 'read') {
    const dom = /*#__PURE__*/_jsxs("div", {
      ref: ref,
      children: [/*#__PURE__*/_jsx("div", {
        children: parsedStartText || '-'
      }), /*#__PURE__*/_jsx("div", {
        children: parsedEndText || '-'
      })]
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/_jsx("span", {
        children: dom
      }));
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    const {
      disabled,
      onChange,
      allowClear,
      placeholder = intl.getMessage('tableForm.selectPlaceholder', '请选择')
    } = fieldProps;
    let dom;
    const momentValue = parseValueToMoment(fieldProps.value);
    if (light) {
      const valueStr = parsedStartText && parsedEndText && `${parsedStartText} ~ ${parsedEndText}`;
      dom = /*#__PURE__*/_jsxs("div", {
        className: `${prefixCls}-light`,
        onClick: () => {
          setOpen(true);
        },
        children: [/*#__PURE__*/_jsx(DatePicker.RangePicker, {
          ...fieldProps,
          ref: ref,
          format: format,
          showTime: showTime,
          placeholder: [intl.getMessage('tableForm.selectPlaceholder', '请选择'), intl.getMessage('tableForm.selectPlaceholder', '请选择')],
          bordered: plain === undefined ? true : !plain,
          onChange: v => {
            onChange(v);
            setTimeout(() => {
              setOpen(false);
            }, 0);
          },
          activePickerIndex: activePickerIndex,
          onOpenChange: setOpen,
          open: open,
          onCalendarChange: dates => {
            if (dates && !dates[0]) {
              setActivePickerIndex(ACTIVE_PICKER_INDEX_LEFT);
            } else if (dates && !dates[1]) {
              setActivePickerIndex(ACTIVE_PICKER_INDEX_RIGHT);
            }
          }
        }), /*#__PURE__*/_jsx(FieldLabel, {
          label: label,
          disabled: disabled,
          placeholder: placeholder,
          size: size,
          value: valueStr,
          allowClear: allowClear,
          onClear: () => {
            onChange(null);
            setActivePickerIndex(0);
          },
          expanded: open
        })]
      });
    } else {
      dom = /*#__PURE__*/_jsx(DatePicker.RangePicker, {
        ref: ref,
        format: format,
        showTime: showTime,
        placeholder: [intl.getMessage('tableForm.selectPlaceholder', '请选择'), intl.getMessage('tableForm.selectPlaceholder', '请选择')],
        bordered: plain === undefined ? true : !plain,
        ...fieldProps,
        value: momentValue
      });
    }
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
export default /*#__PURE__*/React.forwardRef(FieldRangePicker);