"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");
require("core-js/modules/es.array.flat-map.js");
require("core-js/modules/es.array.unscopables.flat-map.js");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _icons = require("@wii/icons");
var _form = _interopRequireDefault(require("../form"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _wauFilterDropdown = _interopRequireDefault(require("../wau-filter-dropdown"));
var _wauFieldLabel = _interopRequireDefault(require("../wau-field-label"));
var _wauProvider = require("../wau-provider");
var _wauBaseForm = _interopRequireDefault(require("../wau-base-form"));
require("./style");
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; }
/**
 * 单行的查询表单，一般用于配合 table 或者 list使用 有时也会用于 card 的额外区域
 *
 * @param props
 */
const LightFilterContainer = props => {
  const {
    items,
    prefixCls,
    size = 'middle',
    collapse,
    collapseLabel,
    onValuesChange,
    bordered,
    values = {}
  } = props;
  const intl = (0, _wauProvider.useIntl)();
  const lightFilterClassName = `${prefixCls}-light-filter`;
  const outsideItems = [];
  const collapseItems = [];
  const [open, setOpen] = (0, _react.useState)(false);
  const [moreValues, setMoreValues] = (0, _react.useState)(() => {
    return {
      ...values
    };
  });
  (0, _react.useEffect)(() => {
    setMoreValues({
      ...values
    });
  }, [values]);
  items.forEach(item => {
    const {
      secondary,
      name
    } = item.props || {};
    if (secondary && !values[name] || collapse) {
      collapseItems.push(item);
    } else {
      outsideItems.push(item);
    }
  });
  const collapseLabelRender = () => {
    if (collapseLabel) {
      return collapseLabel;
    }
    if (collapse) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.FilterOutlined, {
        className: `${lightFilterClassName}-collapse-icon`
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFieldLabel.default, {
      size: size,
      label: intl.getMessage('form.lightFilter.more', '更多筛选'),
      expanded: open
    });
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)(lightFilterClassName, `${lightFilterClassName}-${size}`, {
      [`${lightFilterClassName}-effective`]: Object.keys(values).some(key => values[key])
    }),
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: `${lightFilterClassName}-container`,
      children: [outsideItems.map(child => {
        const {
          key
        } = child;
        return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${lightFilterClassName}-item`,
          children: /*#__PURE__*/_react.default.cloneElement(child, {
            // proFieldProps 会直接作为 ProField 的 props 传递过去
            proFieldProps: {
              light: true,
              label: child.props.label,
              bordered
            },
            bordered
          })
        }, key);
      }), collapseItems.length ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${lightFilterClassName}-item`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFilterDropdown.default, {
          padding: 24,
          onVisibleChange: setOpen,
          visible: open,
          label: collapseLabelRender(),
          footer: {
            onConfirm: () => {
              onValuesChange({
                ...moreValues
              });
              setOpen(false);
            },
            onClear: () => {
              const clearValues = {};
              Object.keys(moreValues).forEach(key => {
                clearValues[key] = undefined;
              });
              setMoreValues(clearValues);
            }
          },
          children: collapseItems.map(child => {
            const {
              key
            } = child;
            const {
              name,
              fieldProps
            } = child.props;
            const newFieldProps = {
              ...fieldProps,
              onChange: e => {
                setMoreValues({
                  ...moreValues,
                  [name]: e !== null && e !== void 0 && e.target ? e.target.value : e
                });
                return false;
              }
            };
            // eslint-disable-next-line
            if (moreValues.hasOwnProperty(name)) {
              newFieldProps[child.props.valuePropName || 'value'] = moreValues[name];
            }
            return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: `${lightFilterClassName}-line`,
              children: /*#__PURE__*/_react.default.cloneElement(child, {
                fieldProps: newFieldProps
              })
            }, key);
          })
        })
      }, "more") : null]
    })
  });
};
function LightFilter(props) {
  const {
    size,
    collapse,
    collapseLabel,
    initialValues,
    onValuesChange,
    form: userForm,
    bordered,
    // eslint-disable-next-line
    ignoreRules,
    ...reset
  } = props;
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-form');
  const [form] = _form.default.useForm();
  const realForm = userForm || form;
  const [values, setValues] = (0, _react.useState)(() => {
    return {
      ...initialValues
    };
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauBaseForm.default, {
    size: size,
    initialValues: initialValues,
    form: realForm,
    contentRender: items => {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(LightFilterContainer, {
        prefixCls: prefixCls,
        items: items.flatMap(item => {
          if ((item === null || item === void 0 ? void 0 : item.type.displayName) === 'ProForm-Group') {
            return item.props.children;
          }
          return item;
        }),
        size: size,
        bordered: bordered,
        collapse: collapse,
        collapseLabel: collapseLabel,
        values: values,
        onValuesChange: newValues => {
          const newAllValues = {
            ...values,
            ...newValues
          };
          setValues(newAllValues);
          realForm.setFieldsValue(newAllValues);
          realForm.submit();
          if (onValuesChange) {
            onValuesChange(newValues, newAllValues);
          }
        }
      });
    },
    formItemProps: {
      colon: false,
      labelAlign: 'left'
    },
    fieldProps: {
      style: {
        width: undefined
      }
    },
    ...reset,
    onValuesChange: (_, allValues) => {
      setValues(allValues);
      if (onValuesChange) {
        onValuesChange(_, allValues);
      }
      realForm.submit();
    }
  });
}
var _default = LightFilter;
exports.default = _default;