const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useDataSource = require("../../hooks/useDataSource.js");
const require_index = require("../../../filter/components/FilterButton/index.js");
const require_index$1 = require("../../fields/Input/index.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/dataSourceComponents/dataSourceTable/filter/index.tsx
const defaultArr = [];
const Filter = (props) => {
	const { componentId, items = defaultArr, mode = "simple", quickFilterMaxLength = 3, sortButtonShow = false, handleValuesChange, search, filterBy, show, getInitialValues } = props;
	const [form] = antd.Form.useForm();
	const { dataSource } = require_useDataSource.default();
	const [list, setList] = (0, react.useState)(() => {
		return require_utils.getStoredFilterOrder(componentId) || {
			quickFilter: [],
			otherFilter: []
		};
	});
	const filterList = (0, react.useMemo)(() => {
		return items.filter((item) => !(item === null || item === void 0 ? void 0 : item.isHidden));
	}, [items]);
	const filterDom = (0, react.useMemo)(() => {
		if (!show) return null;
		return require_utils.genFilterItems(list.quickFilter, dataSource);
	}, [list.quickFilter, show]);
	(0, react.useEffect)(() => {
		const storedOrder = require_utils.getStoredFilterOrder(componentId);
		if (!storedOrder) {
			const newList = {
				quickFilter: filterList.slice(0, quickFilterMaxLength),
				otherFilter: filterList.slice(quickFilterMaxLength)
			};
			setList(newList);
			require_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);
			}
		});
		Array.from(itemMap.values()).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);
		require_utils.saveFilterOrder(componentId, orderedItems);
	}, [
		filterList,
		quickFilterMaxLength,
		componentId
	]);
	const onChange = (newVal) => {
		setList(newVal);
		require_utils.saveFilterOrder(componentId, newVal);
	};
	const renderItem = (0, ahooks.useMemoizedFn)((item, otherProps) => {
		return require_utils.field2Cpt(require_utils.genGetField(dataSource)(item.name), item, require_objectSpread2._objectSpread2({
			labelCol: { span: 24 },
			style: (style) => {
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style || {}), {}, { width: "100%" });
			}
		}, otherProps));
	});
	const onValuesChange = (0, ahooks.useDebounceFn)((changedFields, allFields) => {
		handleValuesChange(changedFields, allFields);
	}, { wait: 300 }).run;
	const searchDom = (0, react.useMemo)(() => {
		return (search === null || search === void 0 ? void 0 : search.show) ? /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			name: search.key || "_keywords",
			placeholder: search.placeholder,
			renderMode: "edit",
			prefix: /* @__PURE__ */ react.default.createElement(_ant_design_icons.SearchOutlined, null),
			style: {
				width: 200,
				margin: 0
			},
			allowClear: true
		}) : null;
	}, [search]);
	const initialValues = (0, react.useMemo)(() => {
		return getInitialValues === null || getInitialValues === void 0 ? void 0 : getInitialValues();
	}, [search, filterBy]);
	return /* @__PURE__ */ react.default.createElement(antd.Form, {
		labelCol: { span: 0 },
		form,
		size: "large",
		layout: "inline",
		className: "filter-form-wrapper",
		onValuesChange,
		initialValues
	}, searchDom, filterDom, show && sortButtonShow && /* @__PURE__ */ react.default.createElement(require_index.default, {
		value: list,
		quickFilterMaxLength,
		formFiltersPrefix: "",
		onChange,
		renderItem
	}));
};
//#endregion
exports.default = Filter;
