"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.proFormItemRender = exports.formInputRender = exports.default = void 0;
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.promise.js");
require("core-js/modules/es.array.sort.js");
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 _omit = _interopRequireDefault(require("omit.js"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _wauQueryFilter = _interopRequireDefault(require("../wau-query-filter"));
var _wauLightFilter = _interopRequireDefault(require("../wau-light-filter"));
var _wauForm = _interopRequireWildcard(require("../wau-form"));
var _utils = require("../utils");
var _wauProvider = require("../wau-provider");
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; }
/**
 * 获取当前选择的 Form Layout 配置
 *
 * @param isForm
 * @param searchConfig
 * @returns LightFilter | QueryFilter | ProForm
 */
const getFormCompetent = (isForm, searchConfig) => {
  if (!isForm && searchConfig !== false) {
    if ((searchConfig === null || searchConfig === void 0 ? void 0 : searchConfig.filterType) === 'light') {
      return {
        Competent: _wauLightFilter.default,
        competentName: 'light-filter'
      };
    }
    return {
      Competent: _wauQueryFilter.default,
      competentName: 'query-filter'
    };
  }
  return {
    Competent: _wauForm.default,
    competentName: 'form'
  };
};

/**
 * 获取需要传给相应表单的props
 *
 * @param searchConfig
 * @param name
 */
const getFromProps = (isForm, searchConfig, name) => {
  if (!isForm && name === 'light-filter') {
    // 传给 lightFilter 的问题
    return (0, _omit.default)({
      ...searchConfig
    }, ['labelWidth', 'defaultCollapsed', 'filterType', 'show']);
  }
  if (!isForm) {
    // 传给 QueryFilter 的配置
    return (0, _omit.default)({
      labelWidth: searchConfig ? searchConfig === null || searchConfig === void 0 ? void 0 : searchConfig.labelWidth : undefined,
      defaultCollapsed: true,
      ...searchConfig
    }, ['filterType', 'show']);
  }
  return {};
};

/**
 * 从formConfig中获取传给相应表单的配置
 *
 * @param isForm
 * @param formConfig
 */
const getFormConfigs = (isForm, formConfig) => {
  if (isForm) {
    // 传给Form的配置
    return (0, _omit.default)(formConfig, ['ignoreRules']);
  }
  // 传给Filter的配置
  return {
    ignoreRules: true,
    ...formConfig
  };
};
/**
 * 把配置转化为输入控件
 *
 * @param props
 * @param ref
 */
const formInputRender = (props, ref) => {
  var _item$search;
  const {
    item,
    // eslint-disable-next-line
    intl,
    form,
    type,
    formItemProps: propsFormItemProps,
    ...rest
  } = props;
  const formItemProps = (0, _utils.getFieldPropsOrFormItemProps)(propsFormItemProps, form, item);
  const {
    valueType: itemValueType = 'text'
  } = item;
  // if function， run it
  const valueType = (typeof itemValueType === 'function' ? itemValueType({}, type) : itemValueType) || 'text';
  const {
    onChange,
    ...restFieldProps
  } = (0, _utils.getFieldPropsOrFormItemProps)(item.fieldProps || {}, form, item);

  /** 公共的 props */
  const initialProps = {
    name: item.key || item.dataIndex,
    initialValue: item.initialValue,
    params: item.params,
    key: `${item.dataIndex || ''}-${item.key || ''}-${item.index}`,
    formItemProps,
    transform: item.search ? (_item$search = item.search) === null || _item$search === void 0 ? void 0 : _item$search.transform : undefined
  };

  /** 自定义 render */
  if (item.renderFormItem && form) {
    /** 删除 renderFormItem 防止重复的 dom 渲染 */
    const {
      renderFormItem,
      ...restItem
    } = item;
    const defaultRender = newItem => formInputRender({
      ...{
        ...props,
        item: newItem
      }
    });

    /** 拼接 renderFormItem 的配置 */
    const renderFormItemProps = (0, _omit.default)({
      ...rest,
      type,
      defaultRender
    }, ['colSize']);

    // 自动注入 onChange 和 value，用户自己很有可能忘记
    const dom = renderFormItem({
      ...restItem,
      type: 'form'
    }, renderFormItemProps, form);

    // 有可能不是一个组件
    if (! /*#__PURE__*/_react.default.isValidElement(dom)) {
      return dom;
    }
    const defaultProps = dom.props;
    if (defaultProps.isDefaultDom) {
      return dom;
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauForm.ProFormField, {
      ...rest,
      ...initialProps,
      ref: ref,
      fieldProps: {
        style: {
          width: undefined
        },
        ...item.fieldProps
      },
      children: /*#__PURE__*/_react.default.cloneElement(dom, (0, _omit.default)({
        ...rest,
        ...defaultProps
      }, ['colSize']))
    });
  }
  const finalValueType = !valueType || ['textarea', 'jsonCode', 'code'].includes(valueType) && type === 'table' ? 'text' : valueType;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauForm.ProFormField, {
    ref: ref,
    tooltip: item.tooltip || item.tip,
    isDefaultDom: true,
    valueEnum: (0, _utils.runFunction)(item.valueEnum, undefined),
    onChange: onChange,
    fieldProps: {
      style: {
        width: undefined
      },
      ...restFieldProps
    },
    ...rest,
    ...initialProps,
    // valueType = textarea，但是在 查询表单这里，应该是个 input 框
    valueType: finalValueType
  });
};
exports.formInputRender = formInputRender;
const proFormItemRender = ({
  item,
  intl,
  formInstance,
  type
}) => {
  const {
    // eslint-disable-next-line
    valueType,
    // eslint-disable-next-line
    dataIndex,
    // eslint-disable-next-line
    valueEnum,
    // eslint-disable-next-line
    renderFormItem,
    // eslint-disable-next-line
    render,
    // eslint-disable-next-line
    hideInForm,
    // eslint-disable-next-line
    hideInSearch,
    // eslint-disable-next-line
    search,
    // eslint-disable-next-line
    hideInTable,
    // eslint-disable-next-line
    renderText,
    // eslint-disable-next-line
    order,
    // eslint-disable-next-line
    width,
    // eslint-disable-next-line
    sorter,
    formItemProps,
    // eslint-disable-next-line
    initialValue,
    // eslint-disable-next-line
    ellipsis,
    // eslint-disable-next-line
    index,
    // eslint-disable-next-line
    filters,
    request,
    params,
    colSize,
    ...rest
  } = item;

  // 支持 function 的 title
  const getTitle = () => {
    if (rest.title && typeof rest.title === 'function') {
      return rest.title(item, 'form', '');
    }
    return rest.title;
  };
  const dom = formInputRender({
    item,
    type,
    intl,
    form: formInstance,
    label: getTitle(),
    request,
    params,
    formItemProps,
    colSize
  });
  if (!dom) {
    return null;
  }
  return dom;
};
exports.proFormItemRender = proFormItemRender;
const FormSearch = ({
  onSubmit,
  formRef,
  dateFormatter = 'string',
  type,
  columns,
  manualRequest,
  onReset,
  submitButtonLoading,
  search: searchConfig,
  form: formConfig = {},
  bordered
}) => {
  /** 为了支持 dom 的消失，支持了这个 api */
  const intl = (0, _wauProvider.useIntl)();
  const isForm = type === 'form';
  /** 提交表单，根据两种模式不同，方法不相同 */
  const submit = async (values, firstLoad) => {
    if (onSubmit) {
      onSubmit(values, firstLoad);
    }
  };
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const columnsList = (0, _react.useMemo)(() => {
    return columns.filter(item => {
      const {
        valueType
      } = item;
      if ((item.hideInSearch || item.search === false) && type !== 'form') {
        return false;
      }
      if (type === 'form' && item.hideInForm) {
        return false;
      }
      if (valueType !== 'index' && valueType !== 'indexBorder' && valueType !== 'option' && (item.key || item.dataIndex)) {
        return true;
      }
      return false;
    }).sort((a, b) => {
      if (b.order || a.order) {
        return (b.order || 0) - (a.order || 0);
      }
      return (b.index || 0) - (a.index || 0);
    });
  }, [columns, type]);
  const columnsListRef = (0, _react.useRef)([]);
  const updateDomList = (0, _react.useCallback)(list => {
    const newFormItemList = list.map((item, index) => proFormItemRender({
      isForm,
      formInstance: formRef.current || undefined,
      item: {
        index,
        ...item
      },
      type,
      intl
    })).filter(item => !!item);
    columnsListRef.current = newFormItemList;
    return newFormItemList;
  }, [formRef, intl, isForm, type]);
  const [domList, setDomList] = (0, _react.useState)(() => updateDomList(columnsList));
  (0, _utils.useDeepCompareEffect)(() => {
    if (columnsList.length < 1) return;
    setDomList(updateDomList(columnsList));
  }, [columnsList]);
  const className = getPrefixCls('pro-table-search');
  const formClassName = getPrefixCls('pro-table-form');
  const {
    Competent,
    competentName
  } = (0, _react.useMemo)(() => getFormCompetent(isForm, searchConfig), [searchConfig, isForm]);

  // 传给每个表单的配置，理论上大家都需要
  const loadingProps = (0, _react.useMemo)(() => ({
    submitter: {
      submitButtonProps: {
        loading: submitButtonLoading
      }
    }
  }), [submitButtonLoading]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)(className, {
      [formClassName]: isForm,
      [getPrefixCls(`pro-table-search-${competentName}`)]: true,
      [`${getPrefixCls('card')}-bordered`]: !!bordered
    }),
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Competent, {
      ...loadingProps,
      ...getFromProps(isForm, searchConfig, competentName),
      ...getFormConfigs(isForm, formConfig),
      formRef: formRef,
      onValuesChange: (change, all) => {
        setDomList(updateDomList(columnsList));
        if (formConfig.onValuesChange) {
          formConfig.onValuesChange(change, all);
        }
      },
      dateFormatter: dateFormatter,
      onInit: values => {
        // 触发一个 submit，之所以这里触发是为了保证 value 都被 format了
        if (type !== 'form') {
          // 重新计算一下dom
          setDomList(updateDomList(columnsList));
          /** 如果是手动模式不需要提交 */
          if (manualRequest) return;
          submit(values, true);
        }
      },
      onReset: values => {
        onReset === null || onReset === void 0 ? void 0 : onReset(values);
      },
      onFinish: values => {
        submit(values, false);
      },
      initialValues: formConfig.initialValues,
      children: domList
    })
  });
};
var _default = /*#__PURE__*/_react.default.memo(FormSearch);
exports.default = _default;