require("../../../_virtual/_rolldown/runtime.js");
const require_phoneGridInfiniteDataUtils = require("./phoneGridInfiniteDataUtils.js");
let react = require("react");
//#region src/plus/pisellReservation/hooks/useReservationPhoneGridInfiniteData.ts
/**
* 手机 Table 无限滚动数据：
* - 内置列表：按页累积服务端返回行（与 RecordBoard `grid.infiniteScroll` 语义一致）；
* - 受控列表：在已有全量 `data` 上做客户端累积切片。
*/
function useReservationPhoneGridInfiniteData(options) {
	const { enabled, isInternalListData, rowKey, data, pagination, searchParams } = options;
	const [accumulatedRows, setAccumulatedRows] = (0, react.useState)([]);
	const lastSearchParamsRef = (0, react.useRef)(searchParams);
	const lastAppliedPageRef = (0, react.useRef)(0);
	(0, react.useEffect)(() => {
		if (!enabled || !isInternalListData) {
			setAccumulatedRows([]);
			lastAppliedPageRef.current = 0;
			return;
		}
		const searchChanged = lastSearchParamsRef.current !== searchParams;
		if (searchChanged) {
			lastSearchParamsRef.current = searchParams;
			lastAppliedPageRef.current = 0;
		}
		const pageNumber = Math.max(1, pagination.pageNumber);
		if (pageNumber <= 1 || searchChanged) {
			setAccumulatedRows(data);
			lastAppliedPageRef.current = 1;
			return;
		}
		if (pageNumber <= lastAppliedPageRef.current) return;
		setAccumulatedRows((prev) => require_phoneGridInfiniteDataUtils.mergePhoneGridRowsByRowKey(prev, data, rowKey));
		lastAppliedPageRef.current = pageNumber;
	}, [
		enabled,
		isInternalListData,
		data,
		pagination.pageNumber,
		searchParams,
		rowKey
	]);
	return (0, react.useMemo)(() => {
		if (!enabled) return data;
		if (isInternalListData) return accumulatedRows;
		const safeSize = Math.max(1, pagination.pageSize);
		const end = Math.min(data.length, pagination.pageNumber * safeSize);
		return data.slice(0, end);
	}, [
		enabled,
		isInternalListData,
		accumulatedRows,
		data,
		pagination.pageNumber,
		pagination.pageSize
	]);
}
//#endregion
exports.useReservationPhoneGridInfiniteData = useReservationPhoneGridInfiniteData;
