/**
 * 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_settings = require("../../../settings.js");
const require_useIsomorphicEffect = require("../../../global/js/hooks/useIsomorphicEffect.js");
const require_useResizeObserver = require("../../../global/js/hooks/useResizeObserver.js");
const require_DatagridHead = require("./DatagridHead.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _carbon_react = require("@carbon/react");
let react_window = require("react-window");
//#region src/components/Datagrid/Datagrid/DatagridVirtualBody.tsx
/**
* Copyright IBM Corp. 2020, 2024
*
* 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 blockClass = `${require_settings.pkg.prefix}--datagrid`;
const rowSizeMap = {
	xs: 24,
	sm: 32,
	md: 40,
	lg: 48,
	xl: 64
};
const defaultRowHeight = rowSizeMap.lg;
const DatagridVirtualBody = (datagridState) => {
	const { getTableBodyProps, rows, prepareRow, onScroll, innerListRef, tableHeight = 400, virtualHeight, listRef, rowSize, DatagridPagination, page, handleResize, gridRef, tableId, onVirtualScroll } = datagridState;
	const headWrapRef = (0, react.useRef)(null);
	const innerRef = (0, react.useRef)(null);
	const gridWidth = gridRef?.current?.clientWidth;
	/* istanbul ignore next */
	const handleVirtualGridResize = () => {
		const gridRefElement = gridRef?.current;
		const isHidden = gridRefElement?.offsetParent === null;
		if (gridRefElement && !isHidden) gridRefElement.style.width = gridRefElement?.clientWidth?.toString();
	};
	require_useResizeObserver.useResizeObserver(gridRef, handleVirtualGridResize);
	(0, react.useEffect)(() => {
		handleResize?.();
	}, [handleResize]);
	const rowHeight = rowSize && rowSizeMap[rowSize] || defaultRowHeight;
	(0, react.useEffect)(() => {
		if (listRef && listRef.current) listRef.current.resetAfterIndex(0);
	}, [listRef, rowHeight]);
	const visibleRows = DatagridPagination && page || rows;
	const testRef = (0, react.useRef)(null);
	(0, react.useEffect)(() => {
		const headWrapEl = document?.querySelector(`#${tableId} .${blockClass}__head-wrap`);
		function handleVirtualScrollX(event) {
			const virtualBody = event.target;
			if (headWrapEl) headWrapEl.scrollLeft = virtualBody?.scrollLeft;
		}
		function handleHeaderScrollX(event) {
			const header = event.target;
			if (testRef && testRef.current) {
				testRef.current.scrollLeft = header?.scrollLeft;
				header.scrollLeft = testRef.current.scrollLeft;
			}
		}
		const testRefValue = testRef?.current;
		testRefValue?.addEventListener("scroll", handleVirtualScrollX);
		headWrapEl?.addEventListener("scroll", handleHeaderScrollX);
		return () => {
			testRefValue?.removeEventListener("scroll", handleVirtualScrollX);
			headWrapEl?.removeEventListener("scroll", handleHeaderScrollX);
		};
	});
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		if (headWrapRef.current && headWrapRef.current.style) {
			headWrapRef.current.style.width = `${gridWidth}px`;
			headWrapRef.current.style.overflow = `auto`;
		}
	}, [headWrapRef, gridWidth]);
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		if (testRef?.current && testRef.current.style) testRef.current.style.width = `${gridWidth}px`;
	}, [testRef, gridWidth]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__head-wrap`,
		ref: headWrapRef
	}, /* @__PURE__ */ react.default.createElement(require_DatagridHead.default, datagridState)), /* @__PURE__ */ react.default.createElement(_carbon_react.TableBody, {
		...getTableBodyProps({ role: void 0 }),
		"aria-live": void 0
	}, /* @__PURE__ */ react.default.createElement(react_window.VariableSizeList, {
		height: virtualHeight || tableHeight,
		itemCount: visibleRows.length,
		itemSize: (index) => visibleRows[index]?.isExpanded ? (visibleRows[index].expandedContentHeight || 0) + rowHeight : rowHeight,
		estimatedItemSize: rowHeight,
		onScroll,
		onItemsRendered: (e) => onVirtualScroll?.(e),
		innerRef: innerListRef,
		outerRef: testRef,
		ref: listRef,
		className: `${blockClass}__virtual-scrollbar`
	}, ({ index, style }) => {
		const row = visibleRows[index];
		prepareRow(row);
		const { key } = row.getRowProps();
		return /* @__PURE__ */ react.default.createElement("div", {
			ref: innerRef,
			style: { ...style }
		}, row?.RowRenderer?.({
			...datagridState,
			row,
			key
		}));
	})));
};
//#endregion
exports.default = DatagridVirtualBody;
