var _excluded = ["form", "schema", "onSubmit", "onChange", "defaultFilterValue", "filterValues", "activeFilterValueRef"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { memo, useMemo, useCallback, useRef, useEffect } from 'react';
import { observable } from '@formily/reactive';
import { usePrefixCls, cls } from '@teamix/utils';
import ProForm from '../ProForm';
/**
 * 轻量筛选
 */
var LightFilter = /*#__PURE__*/memo(function (props) {
  var _schema$;
  var form = props.form,
    schema = props.schema,
    onSubmit = props.onSubmit,
    onChange = props.onChange,
    defaultFilterValue = props.defaultFilterValue,
    filterValues = props.filterValues,
    activeFilterValueRef = props.activeFilterValueRef,
    otherProps = _objectWithoutProperties(props, _excluded);
  var filterValue = useRef(defaultFilterValue || (schema === null || schema === void 0 ? void 0 : (_schema$ = schema[0]) === null || _schema$ === void 0 ? void 0 : _schema$.name));
  var prefixCls = usePrefixCls('teamix-pro-form-query-filter-light');
  var lightFilterObs = useMemo(function () {
    return observable({
      onFilter: function onFilter() {}
    });
  }, []);
  useEffect(function () {
    if (activeFilterValueRef) {
      activeFilterValueRef.current = filterValue.current;
    }
  }, []);
  // 筛选提交切换
  var onFilterChange = useCallback(function (value) {
    filterValue.current = value;
    if (activeFilterValueRef) {
      activeFilterValueRef.current = value;
    }
  }, [form]);
  // 实际输出onFilter
  var onFilter = useCallback(function () {
    var _Object$keys;
    var key = filterValue.current;
    var value = form.values[key];
    // 删除旧值
    (_Object$keys = Object.keys(form.values)) === null || _Object$keys === void 0 ? void 0 : _Object$keys.forEach(function (item) {
      if (item !== key) {
        form.setValuesIn(item, undefined);
      }
    });
    onChange === null || onChange === void 0 ? void 0 : onChange(_defineProperty({}, key, value), value, key);
  }, [onChange]);
  var LightSchema = useMemo(function () {
    return [{
      component: 'LightFilter',
      props: {
        defaultFilterValue: defaultFilterValue,
        filterValues: filterValues,
        onFilterChange: onFilterChange,
        buttonProps: {
          onClick: "{{lightFilterObs.onFilter}}"
        }
      },
      children: schema
    }, {
      component: 'Submit',
      props: {
        style: {
          display: 'none'
        }
      }
    }];
  }, [schema, onFilterChange, defaultFilterValue]);
  useEffect(function () {
    lightFilterObs.onFilter = onFilter;
  }, [onFilter]);
  return /*#__PURE__*/React.createElement(ProForm, _objectSpread(_objectSpread({}, otherProps), {}, {
    scope: _objectSpread(_objectSpread({}, otherProps === null || otherProps === void 0 ? void 0 : otherProps.scope), {}, {
      lightFilterObs: lightFilterObs
    }),
    form: form,
    className: cls(prefixCls(), props.className),
    schema: LightSchema,
    feedbackLayout: "none",
    breakpoints: [],
    bordered: false,
    onSubmit: onSubmit,
    onChange: onFilter
  }));
});
export default LightFilter;