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 { 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 FieldTimePicker = ({
  text,
  mode,
  light,
  label,
  format,
  render,
  renderFormItem,
  plain,
  fieldProps
}, ref) => {
  const [open, setOpen] = useState(false);
  const size = useContext(ConfigProvider.SizeContext);
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-date-picker');
  if (mode === 'read') {
    const dom = /*#__PURE__*/_jsx("span", {
      ref: ref,
      children: text ? moment(text).format(format || 'HH:mm:ss') : '-'
    });
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/_jsx("span", {
        children: dom
      }));
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    let dom;
    const {
      disabled,
      onChange,
      placeholder,
      allowClear,
      value
    } = fieldProps;
    const momentValue = parseValueToMoment(value);
    if (light) {
      const valueStr = momentValue && momentValue.format(format || 'HH:mm:ss') || '';
      dom = /*#__PURE__*/_jsxs("div", {
        className: `${prefixCls}-light`,
        onClick: () => {
          setOpen(true);
        },
        children: [/*#__PURE__*/_jsx(DatePicker.TimePicker, {
          value: momentValue,
          format: format,
          ref: ref,
          ...fieldProps,
          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,
          allowClear: allowClear,
          onClear: () => {
            if (onChange) {
              onChange(null);
            }
          },
          expanded: open
        })]
      });
    } else {
      dom = /*#__PURE__*/_jsx(DatePicker.TimePicker, {
        ref: ref,
        format: format,
        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(FieldTimePicker);