"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _utils = require("../utils");
var _wauFieldLabel = _interopRequireDefault(require("../wau-field-label"));
var _wauFilterDropdown = _interopRequireDefault(require("../wau-filter-dropdown"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const LightWrapper = props => {
  const {
    label,
    size,
    disabled,
    onChange,
    onBlur,
    className,
    style,
    children,
    valuePropName,
    light,
    customLightMode,
    placeholder,
    id,
    labelFormatter,
    bordered,
    value
  } = props;
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-light-wrapper');
  const [tempValue, setTempValue] = (0, _react.useState)(props[valuePropName]);
  const [open, setOpen] = (0, _utils.useMountMergeState)(false);
  const isDropdown = /*#__PURE__*/_react.default.isValidElement(children) && (0, _utils.isDropdownValueType)(children.props.valueType);
  if (!light || customLightMode || isDropdown) {
    if ( /*#__PURE__*/_react.default.isValidElement(children)) {
      return /*#__PURE__*/_react.default.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.default.isValidElement(children)) {
    var _props$fieldProps;
    allowClear = (_props$fieldProps = children.props.fieldProps) === null || _props$fieldProps === void 0 ? void 0 : _props$fieldProps.allowClear;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFilterDropdown.default, {
    disabled: disabled,
    onVisibleChange: setOpen,
    visible: open,
    label: /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFieldLabel.default, {
      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__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(`${prefixCls}-container`, className),
      style: style,
      children: /*#__PURE__*/_react.default.isValidElement(children) ? /*#__PURE__*/_react.default.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
    })
  });
};
var _default = LightWrapper;
exports.default = _default;