var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/dataSourceComponents/dataSourceTable/filter/index.tsx
var filter_exports = {};
__export(filter_exports, {
  default: () => filter_default
});
module.exports = __toCommonJS(filter_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_icons = require("@ant-design/icons");
var import_Input = __toESM(require("../../fields/Input"));
var import_useDataSource = __toESM(require("../../hooks/useDataSource"));
var import_FilterButton = __toESM(require("../../../filter/components/FilterButton"));
var import_utils = require("./utils");
var import_index = require("./index.less");
var defaultArr = [];
var Filter = (props) => {
  const {
    componentId,
    items = defaultArr,
    mode = "simple",
    quickFilterMaxLength = 3,
    sortButtonShow = false,
    handleValuesChange,
    search,
    filterBy,
    show,
    getInitialValues
  } = props;
  const [form] = import_antd.Form.useForm();
  const { dataSource } = (0, import_useDataSource.default)();
  const [list, setList] = (0, import_react.useState)(() => {
    const storedOrder = (0, import_utils.getStoredFilterOrder)(componentId);
    return storedOrder || {
      quickFilter: [],
      otherFilter: []
    };
  });
  const filterList = (0, import_react.useMemo)(() => {
    return items.filter((item) => !(item == null ? void 0 : item.isHidden));
  }, [items]);
  const filterDom = (0, import_react.useMemo)(() => {
    if (!show) {
      return null;
    }
    return (0, import_utils.genFilterItems)(list.quickFilter, dataSource);
  }, [list.quickFilter, show]);
  (0, import_react.useEffect)(() => {
    const storedOrder = (0, import_utils.getStoredFilterOrder)(componentId);
    if (!storedOrder) {
      const newList = {
        quickFilter: filterList.slice(0, quickFilterMaxLength),
        otherFilter: filterList.slice(quickFilterMaxLength)
      };
      setList(newList);
      (0, import_utils.saveFilterOrder)(componentId, newList);
      return;
    }
    const orderedItems = {
      quickFilter: [],
      otherFilter: []
    };
    const itemMap = new Map(filterList.map((item) => [item.name, item]));
    storedOrder.quickFilter.forEach((stored) => {
      const item = itemMap.get(stored.name);
      if (item) {
        orderedItems.quickFilter.push(item);
        itemMap.delete(stored.name);
      }
    });
    storedOrder.otherFilter.forEach((stored) => {
      const item = itemMap.get(stored.name);
      if (item) {
        orderedItems.otherFilter.push(item);
        itemMap.delete(stored.name);
      }
    });
    const remainingItems = Array.from(itemMap.values());
    remainingItems.forEach((item) => {
      if (orderedItems.quickFilter.length < quickFilterMaxLength) {
        orderedItems.quickFilter.push(item);
      } else {
        orderedItems.otherFilter.push(item);
      }
    });
    if (quickFilterMaxLength && orderedItems.quickFilter.length > quickFilterMaxLength) {
      const overflow = orderedItems.quickFilter.splice(quickFilterMaxLength);
      orderedItems.otherFilter.unshift(...overflow);
    }
    setList(orderedItems);
    (0, import_utils.saveFilterOrder)(componentId, orderedItems);
  }, [filterList, quickFilterMaxLength, componentId]);
  const onChange = (newVal) => {
    setList(newVal);
    (0, import_utils.saveFilterOrder)(componentId, newVal);
  };
  const renderItem = (0, import_ahooks.useMemoizedFn)((item, otherProps) => {
    const getField = (0, import_utils.genGetField)(dataSource);
    const field = getField(item.name);
    const cptDom = (0, import_utils.field2Cpt)(field, item, {
      labelCol: { span: 24 },
      style: (style) => {
        return {
          ...style || {},
          width: "100%"
        };
      },
      ...otherProps
    });
    return cptDom;
  });
  const onValuesChange = (0, import_ahooks.useDebounceFn)(
    (changedFields, allFields) => {
      handleValuesChange(changedFields, allFields);
    },
    { wait: 300 }
  ).run;
  const searchDom = (0, import_react.useMemo)(() => {
    return (search == null ? void 0 : search.show) ? /* @__PURE__ */ import_react.default.createElement(
      import_Input.default,
      {
        name: search.key || "_keywords",
        placeholder: search.placeholder,
        renderMode: "edit",
        prefix: /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, null),
        style: { width: 200, margin: 0 },
        allowClear: true
      }
    ) : null;
  }, [search]);
  const initialValues = (0, import_react.useMemo)(() => {
    return getInitialValues == null ? void 0 : getInitialValues();
  }, [search, filterBy]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form,
    {
      labelCol: { span: 0 },
      form,
      size: "large",
      layout: "inline",
      className: "filter-form-wrapper",
      onValuesChange,
      initialValues
    },
    searchDom,
    filterDom,
    show && sortButtonShow && /* @__PURE__ */ import_react.default.createElement(
      import_FilterButton.default,
      {
        value: list,
        quickFilterMaxLength,
        formFiltersPrefix: "",
        onChange,
        renderItem
      }
    )
  );
};
var filter_default = Filter;
