/**
 * 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 uuidv4 from "../../global/js/utils/uuidv4.js";
import { stateReducer } from "./Datagrid/addons/stateReducer.js";
import useSkeletons from "./useSkeletonRows.js";
import useDefaultStringRenderer from "./useDefaultStringRenderer.js";
import useRowRenderer from "./useRowRenderer.js";
import useRowSize from "./useRowSize.js";
import useHeaderRow from "./Datagrid/DatagridHeaderRow.js";
import useFlexResize from "./useFlexResize.js";
import useFloatingScroll from "./useFloatingScroll.js";
import { useMemo } from "react";
import { useExpanded, useFilters, useFlexLayout, useGlobalFilter, usePagination, useResizeColumns, useRowSelect, useSortBy, useTable } from "react-table";
//#region src/components/Datagrid/useDatagrid.ts
/**
* Copyright IBM Corp. 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 useDatagrid = (params, ...plugins) => {
	const defaultPlugins = [
		useFlexLayout,
		useHeaderRow,
		useSkeletons,
		useResizeColumns,
		useRowRenderer,
		useDefaultStringRenderer,
		useRowSize,
		useFilters,
		useGlobalFilter,
		useSortBy,
		useExpanded
	];
	if (params.disableResizing) {
		const resizeIndex = defaultPlugins.findIndex((p) => p?.["pluginName"] === "useResizeColumns");
		defaultPlugins.splice(resizeIndex, 1);
	}
	const defaultEndPlugins = [
		usePagination,
		useRowSelect,
		useFlexResize,
		useFloatingScroll
	];
	const clientEndPlugins = params.endPlugins || [];
	const defaultColumn = { minWidth: 50 };
	return useTable({
		tableId: useMemo(() => uuidv4("datagrid-table-id"), []),
		...params,
		stateReducer,
		defaultColumn
	}, ...defaultPlugins, ...plugins, ...defaultEndPlugins, ...clientEndPlugins);
};
//#endregion
export { useDatagrid as default };
