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