import { SearchGroupingType } from "../../enums/unitySystemEnums.mjs";
import { useConvertFilterModelToSearchFilters } from "../search/SearchUtilities.mjs";
import { c } from "react-compiler-runtime";
//#region src/utilities/aggrid/aggridHelpers.ts
const useServerSideDatasource = (refetchData, refetchGroupData, filterChanged, setFilterChanged) => {
	const $ = c(6);
	const convertFilterModelToSearchFilters = useConvertFilterModelToSearchFilters();
	let t0;
	if ($[0] !== convertFilterModelToSearchFilters || $[1] !== filterChanged || $[2] !== refetchData || $[3] !== refetchGroupData || $[4] !== setFilterChanged) {
		t0 = { async getRows(params) {
			if (!params) return;
			const { endRow, sortModel, filterModel, rowGroupCols, groupKeys } = params?.request || {};
			let refetch = refetchData;
			const pageSize = params?.api?.paginationGetPageSize();
			let pageNumber = filterChanged.current === true ? 1 : endRow / pageSize;
			if (isNaN(pageNumber)) pageNumber = 1;
			const column = sortModel[0]?.colId ?? "";
			const sortDirection = sortModel[0]?.sort ?? "";
			const sortInfo = [];
			if (column && sortDirection) sortInfo.push({
				sortColumn: column,
				sortDirection
			});
			let searchFilters = [];
			searchFilters = convertFilterModelToSearchFilters(filterModel);
			const groupingColumns = rowGroupCols?.map(_temp);
			let groupingType = "";
			if (groupingColumns.length > 0 && groupKeys.length === 0) {
				groupingType = SearchGroupingType.Distinct;
				refetch = refetchGroupData ? refetchGroupData : refetchData;
			}
			if (groupKeys.length) {
				if (groupKeys.length === rowGroupCols.length) groupKeys.forEach((key, index) => {
					const rowGroupCol = rowGroupCols[index];
					const element_0 = searchFilters.find((element) => element.searchField === rowGroupCol.field);
					if (element_0) {
						element_0.searchValue = key;
						element_0.searchOperator = "=";
					} else searchFilters.push({
						searchField: rowGroupCol.field ?? rowGroupCol.id,
						searchValue: key,
						searchOperator: "=",
						searchValueType: 0
					});
				});
			}
			let search;
			search = {
				eagerLoad: groupingType ? false : true,
				pageNumber,
				pageSize,
				orderElements: sortInfo,
				filterElements: searchFilters,
				groupingType,
				groupProperty: groupingColumns
			};
			params.api.hideOverlay();
			const response = await refetch({ data: search }).catch(() => {
				params.success({
					rowData: [],
					rowCount: 0
				});
				params.api.showNoRowsOverlay();
			});
			if (response) {
				const rowData = response.data;
				if (rowData) {
					if (typeof rowData === "object" && rowData.pageList) params.success({
						rowData: rowData.pageList,
						rowCount: parseInt(rowData.total)
					});
					else if (rowData.length > 0) params.success({
						rowData,
						rowCount: parseInt(response.headers.totalrows)
					});
					else params.success({
						rowData: [],
						rowCount: 0
					});
					if (filterChanged.current === true) {
						params.api.paginationGoToFirstPage();
						setFilterChanged(false);
					}
				} else {
					params.success({
						rowData: [],
						rowCount: 0
					});
					params.api.showNoRowsOverlay();
				}
			} else params.fail();
		} };
		$[0] = convertFilterModelToSearchFilters;
		$[1] = filterChanged;
		$[2] = refetchData;
		$[3] = refetchGroupData;
		$[4] = setFilterChanged;
		$[5] = t0;
	} else t0 = $[5];
	return t0;
};
function _temp(col) {
	return col.id;
}
//#endregion
export { useServerSideDatasource };

//# sourceMappingURL=aggridHelpers.mjs.map