var _excluded = ["schema", "onChange", "onSubmit"];
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; }
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; }
import React, { memo, useCallback, useEffect, useMemo, useRef } from 'react';
import { usePrefixCls, cls, isValid } from '@teamix/utils';
import ProForm from '../ProForm';
// 初始化Search组件，内置部分属性
var initializeSearch = function initializeSearch(schema) {
  var searchName = '';
  var initializedSchema = schema.map(function (item) {
    if (item.component === 'Search') {
      searchName = item.name;
      return _objectSpread(_objectSpread({}, item), {}, {
        decoratorProps: _objectSpread(_objectSpread({}, item.decoratorProps), {}, {
          inset: false
        }),
        props: _objectSpread({
          shape: 'simple'
        }, item.props)
      });
    }
    return item;
  });
  return {
    searchName: searchName,
    initializedSchema: initializedSchema
  };
};
/**
 * 简单筛选
 */
var SimpleFilter = /*#__PURE__*/memo(function (props) {
  var searchValue = useRef(undefined);
  var schema = props.schema,
    onChange = props.onChange,
    onSubmit = props.onSubmit,
    otherProps = _objectWithoutProperties(props, _excluded);
  var prefixCls = usePrefixCls('teamix-pro-form-query-filter-simple');
  var _initializeSearch = initializeSearch(schema),
    searchName = _initializeSearch.searchName,
    initializedSchema = _initializeSearch.initializedSchema;
  var simpleSchema = useMemo(function () {
    return [{
      component: 'FormFlex',
      props: {
        size: 8
      },
      children: initializedSchema
    }, {
      component: 'Submit',
      props: {
        style: {
          display: 'none'
        }
      }
    }];
  }, [initializedSchema]);
  var onInnerChange = useCallback(function (values, value, name) {
    // 处理Search组件，Select切换时不触发onChange
    if (searchName === name) {
      if (searchValue.current !== value[1]) {
        onChange === null || onChange === void 0 ? void 0 : onChange(values, name, value);
        searchValue.current = value[1];
      }
    } else {
      onChange === null || onChange === void 0 ? void 0 : onChange(values, name, value);
    }
  }, [onChange, searchName, searchValue.current]);
  useEffect(function () {
    var _props$form$initialVa, _props$form$initialVa2;
    // 处理Search组件，缓存input的默认值
    var searchDefaultValue = (_props$form$initialVa = props.form.initialValues) === null || _props$form$initialVa === void 0 ? void 0 : (_props$form$initialVa2 = _props$form$initialVa[searchName]) === null || _props$form$initialVa2 === void 0 ? void 0 : _props$form$initialVa2[1];
    searchValue.current = isValid(searchDefaultValue) ? searchDefaultValue : '';
  }, []);
  return /*#__PURE__*/React.createElement(ProForm, _objectSpread(_objectSpread({}, otherProps), {}, {
    className: cls(prefixCls(), props.className),
    schema: simpleSchema,
    feedbackLayout: "popover",
    breakpoints: [],
    inset: true,
    onSubmit: onSubmit,
    onChange: onInnerChange
  }));
});
export default SimpleFilter;