require("../../../_virtual/_rolldown/runtime.js");
const require_utils = require("../Table/utils.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
//#region src/components/table/hooks/useTransDataSource.ts
const defaultObj = {};
const useTransDataSource = (params) => {
	const { form, localPagination, dataSource: pDataSource, rowKey, filter, currentView } = params;
	const sort = antd.Form.useWatch("order_by", {
		form,
		preserve: true
	});
	const group = antd.Form.useWatch("group_by", {
		form,
		preserve: true
	});
	const filters = antd.Form.useWatch("filters", {
		form,
		preserve: true
	}) || defaultObj;
	/**
	* 根据排序字符串对对象进行排序
	* @param sort 排序字符串，格式为"key:value:type"，例如"age:asc:number"
	* @returns 排序后的对象
	*/
	const sortObj = (0, react.useMemo)(() => {
		if (!sort) return;
		const [key, value, type] = sort.split(":");
		return {
			sortKey: key,
			sortValue: value,
			sortType: type
		};
	}, [sort]);
	const groupDetail = (0, react.useMemo)(() => {
		if ((0, _pisell_utils.isArr)(group) && group.length) return group;
	}, [group]);
	/**
	* 过滤本地筛选数组
	* @returns 返回过滤后的本地筛选数组
	*/
	const filterLocalArr = (0, react.useMemo)(() => {
		if (!(filter === null || filter === void 0 ? void 0 : filter.show) || !(filter === null || filter === void 0 ? void 0 : filter.list)) return [];
		return filter === null || filter === void 0 ? void 0 : filter.list.filter((item) => item.localFilter);
	}, [require_utils.stringify(filter)]);
	return (0, react.useMemo)(() => {
		if (!localPagination) return pDataSource;
		let newDataSource = pDataSource;
		if (filterLocalArr.length) newDataSource = require_utils.filterDataSource(newDataSource, filters, filterLocalArr);
		if (sortObj) newDataSource = require_utils.sortDataSource(newDataSource, sortObj);
		if (groupDetail && currentView === "table") newDataSource = require_utils.groupDataSource(newDataSource, groupDetail, rowKey);
		return newDataSource;
	}, [
		pDataSource,
		localPagination,
		sortObj,
		groupDetail,
		filterLocalArr,
		filters,
		currentView
	]);
};
//#endregion
exports.default = useTransDataSource;
