import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useState, useContext } from 'react';
import classNames from 'classnames';
import ConfigProvider from '../config-provider';
import { isDropdownValueType, useMountMergeState } from '../utils';
import FieldLabel from '../wau-field-label';
import FilterDropdown from '../wau-filter-dropdown';
import { jsx as _jsx } from "react/jsx-runtime";
const LightWrapper = props => {
  const {
    label,
    size,
    disabled,
    onChange,
    onBlur,
    className,
    style,
    children,
    valuePropName,
    light,
    customLightMode,
    placeholder,
    id,
    labelFormatter,
    bordered,
    value
  } = props;
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-light-wrapper');
  const [tempValue, setTempValue] = useState(props[valuePropName]);
  const [open, setOpen] = useMountMergeState(false);
  const isDropdown = /*#__PURE__*/React.isValidElement(children) && isDropdownValueType(children.props.valueType);
  if (!light || customLightMode || isDropdown) {
    if ( /*#__PURE__*/React.isValidElement(children)) {
      return /*#__PURE__*/React.cloneElement(children, {
        value,
        onChange,
        onBlur,
        ...children.props,
        fieldProps: {
          id,
          [valuePropName]: props[valuePropName],
          // 这个 onChange 是 Form.Item 添加上的，要通过 fieldProps 透传给 ProField 调用
          onChange,
          onBlur,
          // 优先使用 children.props.fieldProps，比如 LightFilter 中可能需要通过 fieldProps 覆盖 Form.Item 默认的 onChange
          ...children.props.fieldProps
        }
      });
    }
    return children;
  }
  let allowClear;
  if (children && /*#__PURE__*/React.isValidElement(children)) {
    var _props$fieldProps;
    allowClear = (_props$fieldProps = children.props.fieldProps) === null || _props$fieldProps === void 0 ? void 0 : _props$fieldProps.allowClear;
  }
  return /*#__PURE__*/_jsx(FilterDropdown, {
    disabled: disabled,
    onVisibleChange: setOpen,
    visible: open,
    label: /*#__PURE__*/_jsx(FieldLabel, {
      ellipsis: true,
      size: size,
      onClear: () => {
        onChange === null || onChange === void 0 ? void 0 : onChange();
        setTempValue(undefined);
      },
      bordered: bordered,
      style: style,
      className: className,
      label: label,
      placeholder: placeholder,
      value: props[valuePropName],
      disabled: disabled,
      expanded: open,
      formatter: labelFormatter,
      allowClear: allowClear
    }),
    footer: {
      onClear: () => setTempValue(undefined),
      onConfirm: () => {
        onChange === null || onChange === void 0 ? void 0 : onChange(tempValue);
        setOpen(false);
      }
    },
    children: /*#__PURE__*/_jsx("div", {
      className: classNames(`${prefixCls}-container`, className),
      style: style,
      children: /*#__PURE__*/React.isValidElement(children) ? /*#__PURE__*/React.cloneElement(children, {
        ...children.props,
        fieldProps: {
          className: `${prefixCls}-field`,
          [valuePropName]: tempValue,
          id,
          onChange: e => {
            setTempValue(e !== null && e !== void 0 && e.target ? e.target.value : e);
          },
          allowClear,
          ...children.props.fieldProps
        }
      }) : children
    })
  });
};
export default LightWrapper;