/**
 * 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.
 */

import { pkg } from "../../../settings.js";
import { useIsomorphicEffect } from "../../../global/js/hooks/useIsomorphicEffect.js";
import { useResizeObserver } from "../../../global/js/hooks/useResizeObserver.js";
import DatagridHead from "./DatagridHead.js";
import React, { useEffect, useRef } from "react";
import { TableBody } from "@carbon/react";
import { VariableSizeList } from "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 = `${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 = useRef(null);
	const innerRef = 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();
	};
	useResizeObserver(gridRef, handleVirtualGridResize);
	useEffect(() => {
		handleResize?.();
	}, [handleResize]);
	const rowHeight = rowSize && rowSizeMap[rowSize] || defaultRowHeight;
	useEffect(() => {
		if (listRef && listRef.current) listRef.current.resetAfterIndex(0);
	}, [listRef, rowHeight]);
	const visibleRows = DatagridPagination && page || rows;
	const testRef = useRef(null);
	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);
		};
	});
	useIsomorphicEffect(() => {
		if (headWrapRef.current && headWrapRef.current.style) {
			headWrapRef.current.style.width = `${gridWidth}px`;
			headWrapRef.current.style.overflow = `auto`;
		}
	}, [headWrapRef, gridWidth]);
	useIsomorphicEffect(() => {
		if (testRef?.current && testRef.current.style) testRef.current.style.width = `${gridWidth}px`;
	}, [testRef, gridWidth]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
		className: `${blockClass}__head-wrap`,
		ref: headWrapRef
	}, /* @__PURE__ */ React.createElement(DatagridHead, datagridState)), /* @__PURE__ */ React.createElement(TableBody, {
		...getTableBodyProps({ role: void 0 }),
		"aria-live": void 0
	}, /* @__PURE__ */ React.createElement(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.createElement("div", {
			ref: innerRef,
			style: { ...style }
		}, row?.RowRenderer?.({
			...datagridState,
			row,
			key
		}));
	})));
};

//#endregion
export { DatagridVirtualBody as default };