/**
 * 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 DatagridExpandedRow from "./Datagrid/DatagridExpandedRow.js";
import useRowExpander from "./useRowExpander.js";
import { useState } from "react";
//#region src/components/Datagrid/useExpandedRow.ts
/**
* 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 useExpandedRow = (hooks) => {
	useRowExpander(hooks);
	const useInstance = (instance) => {
		const { rows, expandedContentHeight, ExpandedRowContentComponent, onRowExpand } = instance;
		const [expandedRowsHeight, setExpandedRowsHeight] = useState({});
		const setExpandedRowHeight = (rowIndex, height) => setExpandedRowsHeight({
			...expandedRowsHeight,
			[rowIndex]: height
		});
		const rowsWithExpand = rows.map((row) => ({
			...row,
			canExpand: row.original && !row.original.notExpandable,
			expandedContentHeight: expandedRowsHeight[row.index] || expandedContentHeight,
			RowExpansionRenderer: ExpandedRowContentComponent && DatagridExpandedRow(ExpandedRowContentComponent),
			onClick: (row, event) => onRowExpand?.(row, event)
		}));
		Object.assign(instance, {
			rows: rowsWithExpand,
			setExpandedRowHeight,
			withExpandedRows: true
		});
	};
	hooks.useInstance.push(useInstance);
};
//#endregion
export { useExpandedRow as default };
