import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useDataSource from "../../dataSourceComponents/hooks/useDataSource.js";
import { formatListResult } from "../utils/formatData.js";
import { useCallback, useMemo, useRef, useState } from "react";
//#region src/components/pisellDataSourceContainer/hooks/useHandleAction.ts
const defaultPagination = {
	pageNumber: 1,
	pageSize: 10
};
const useHandleAction = (initialParams = {}, options, form) => {
	const { list, get, update, create, destroy, getCurrentRecord } = useDataSource();
	const { pageNumber, pageSize } = options === true ? defaultPagination : typeof options === "object" ? options : {};
	const enablePagination = pageNumber !== void 0 && pageSize !== void 0;
	const initialParamsRef = useRef(initialParams);
	const [pagination, setPagination] = useState({
		pageNumber: pageNumber !== null && pageNumber !== void 0 ? pageNumber : 1,
		pageSize: pageSize !== null && pageSize !== void 0 ? pageSize : 10
	});
	const { run, params, data: listData } = list;
	/**
	* 查询方法 - 合并当前分页和新参数进行查询
	* @param newParams 新的查询参数
	* @description 如果启用分页，会自动重置到第一页
	*/
	const onSearch = useCallback((newParams) => {
		var _initialParamsRef$cur;
		let searchParams = _objectSpread2(_objectSpread2(_objectSpread2({}, (params === null || params === void 0 ? void 0 : params[0]) || {}), ((_initialParamsRef$cur = initialParamsRef.current) === null || _initialParamsRef$cur === void 0 ? void 0 : _initialParamsRef$cur.list) || {}), newParams);
		if (enablePagination) {
			searchParams = _objectSpread2(_objectSpread2({}, searchParams), {}, {
				pageNumber: 1,
				pageSize: pagination.pageSize
			});
			if (pagination.pageNumber !== 1) setPagination((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { pageNumber: 1 }));
		}
		run(searchParams);
	}, [
		enablePagination,
		pagination.pageNumber,
		pagination.pageSize,
		params,
		run
	]);
	/**
	* 重置方法 - 重置到初始参数和初始分页
	*/
	const onReset = useCallback(() => {
		var _initialParamsRef$cur2;
		const formValues = (form === null || form === void 0 ? void 0 : form.getFieldsValue()) || {};
		let resetParams = _objectSpread2(_objectSpread2({}, ((_initialParamsRef$cur2 = initialParamsRef.current) === null || _initialParamsRef$cur2 === void 0 ? void 0 : _initialParamsRef$cur2.list) || {}), formValues);
		if (enablePagination) {
			setPagination({
				pageNumber: pageNumber !== null && pageNumber !== void 0 ? pageNumber : 1,
				pageSize: pageSize !== null && pageSize !== void 0 ? pageSize : 10
			});
			resetParams = _objectSpread2(_objectSpread2({}, resetParams), {}, {
				pageNumber: pageNumber !== null && pageNumber !== void 0 ? pageNumber : 1,
				pageSize: pageSize !== null && pageSize !== void 0 ? pageSize : 10
			});
		}
		run(resetParams);
	}, [
		run,
		enablePagination,
		pagination,
		pageNumber,
		pageSize
	]);
	/**
	*  分页
	*/
	const onPageChange = useCallback((pageNumber, pageSize) => {
		var _initialParamsRef$cur3;
		if (!enablePagination) {
			console.warn("分页功能未启用");
			return;
		}
		if (pageNumber < 1 || pageSize < 1) return;
		setPagination({
			pageNumber,
			pageSize
		});
		run(_objectSpread2(_objectSpread2({}, (params === null || params === void 0 ? void 0 : params[0]) || _objectSpread2({}, ((_initialParamsRef$cur3 = initialParamsRef.current) === null || _initialParamsRef$cur3 === void 0 ? void 0 : _initialParamsRef$cur3.list) || {})), {}, {
			pageNumber,
			pageSize
		}));
	}, [
		enablePagination,
		params,
		run
	]);
	return useMemo(() => {
		return {
			list: _objectSpread2(_objectSpread2({}, list), {}, {
				data: formatListResult(listData),
				onSearch,
				onReset,
				onPageChange
			}, enablePagination ? { pagination } : {}),
			get,
			update,
			create,
			destroy,
			getCurrentRecord
		};
	}, [
		list,
		get,
		update,
		create,
		destroy,
		getCurrentRecord,
		pagination
	]);
};
//#endregion
export { useHandleAction as default };
