/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

const require_runtime = require('../../_virtual/_rolldown/runtime.js');
const require_debounce = require('../../global/js/utils/debounce.js');
const require_useParentDimensions = require('./useParentDimensions.js');
const require_useResizeTable = require('./useResizeTable.js');
let react = require("react");

//#region src/components/Datagrid/useInfiniteScroll.ts
/**
* Copyright IBM Corp. 2020, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const useInfiniteScroll = (hooks) => {
	require_useParentDimensions.default(hooks);
	require_useResizeTable.default(hooks);
	const useInstance = (instance) => {
		const { isFetching, tableHeight, innerListRef, fetchMoreData, tableId, loadMoreThreshold } = instance;
		let tableElement;
		if (typeof document !== "undefined") tableElement = document.querySelector(`#${tableId}`);
		const totalTableHeight = tableHeight || tableElement?.clientHeight;
		const loadMoreThresholdValue = typeof loadMoreThreshold === "number" ? loadMoreThreshold : 200;
		const emptyFetchData = () => {};
		const fetchMore = (0, react.useCallback)(require_debounce.debounce(fetchMoreData || emptyFetchData, 3e3), [fetchMoreData]);
		const onScroll = ({ scrollDirection, scrollOffset }) => {
			if (innerListRef && innerListRef.current) {
				if (!isFetching && scrollDirection === "forward" && scrollOffset + totalTableHeight >= innerListRef.current.clientHeight - loadMoreThresholdValue) {
					if (fetchMoreData) fetchMore();
				}
			}
		};
		Object.assign(instance, {
			onScroll,
			withVirtualScroll: true
		});
	};
	hooks.useInstance.push(useInstance);
};

//#endregion
exports.default = useInfiniteScroll;