import { LogicalSearchOperatorEnums, SearchGroupingType, SearchSortDirection } from "../../../enums/unitySystemEnums.mjs";
import { getSearchObject } from "../../search/SearchUtilities.mjs";
import { FilterOperators } from "../../../models/SearchOperator.mjs";
import { cloneDeep } from "lodash-es";
//#region src/utilities/aggrid/datasources/SearchServiceDatasource.js
const SearchServiceDatasource = (queryData, apiMethodId, searchFilterElements, logicalSearchOperator = LogicalSearchOperatorEnums.And, executeQuery, storeRowData, lookupsMap, eagerLoad = false, getCleanData = null) => {
	return { async getRows(params) {
		const { startRow, endRow, rowGroupCols, groupKeys, valueCols, filterModel, sortModel } = params.request;
		const searchObject = cloneDeep(getSearchObject(searchFilterElements, endRow / 20, null, eagerLoad, logicalSearchOperator));
		if (sortModel.length) {
			const orderElements = searchObject.orderElements;
			sortModel.forEach((sortItem) => {
				const formattedPropertyName = sortItem.colId.charAt(0).toUpperCase() + sortItem.colId.slice(1);
				const element = orderElements.find((element) => element.sortColumn === formattedPropertyName);
				if (element) element.sortDirection = sortItem.sort.toUpperCase();
				else orderElements.push({
					sortColumn: formattedPropertyName,
					sortDirection: sortItem.sort.toUpperCase()
				});
			});
		}
		if (Object.keys(filterModel).length) {
			const filterElements = searchObject.filterElements;
			Object.keys(filterModel).forEach((key) => {
				const filterItem = filterModel[key];
				const formattedPropertyName = key.charAt(0).toUpperCase() + key.slice(1);
				filterElements.push({
					searchField: formattedPropertyName,
					searchValue: filterItem.filter,
					searchOperator: FilterOperators[filterItem.type]
				});
			});
		}
		if (rowGroupCols.length && !groupKeys.length) {
			const formattedPropertyName = rowGroupCols[0].field.charAt(0).toUpperCase() + rowGroupCols[0].field.slice(1);
			searchObject.groupingType = SearchGroupingType.Distinct;
			if (!searchObject.groupProperty.includes(formattedPropertyName)) {
				searchObject.groupProperty.push(formattedPropertyName);
				const orderElements = searchObject.orderElements;
				if (!orderElements.find((element) => element.sortColumn === formattedPropertyName)) orderElements.push({
					sortColumn: formattedPropertyName,
					sortDirection: SearchSortDirection.Asc
				});
			}
		}
		if (groupKeys.length) if (groupKeys.length === rowGroupCols.length) {
			searchObject.groupingType = "";
			const filterElements = searchObject.filterElements;
			groupKeys.forEach((key, index) => {
				const rowGroupCol = rowGroupCols[index];
				const formattedPropertyName = rowGroupCol.field.charAt(0).toUpperCase() + rowGroupCol.field.slice(1);
				const element = filterElements.find((element) => element.searchField === formattedPropertyName);
				let currentNode = null;
				params.api.forEachNode((node) => {
					const nodeKey = node.field;
					if (nodeKey?.toLowerCase() === formattedPropertyName?.toLowerCase() && node?.data?.[nodeKey] === key) {
						currentNode = node;
						return;
					}
				});
				let searchValue = key;
				if (currentNode) {
					const cleanData = getCleanData ? getCleanData(currentNode.data, lookupsMap) : null;
					if (cleanData) searchValue = cleanData[currentNode.field];
				}
				if (element) {
					element.searchValue = searchValue;
					element.searchOperator = "=";
				} else filterElements.push({
					searchField: formattedPropertyName,
					searchValue,
					searchOperator: "="
				});
			});
		} else {
			groupKeys.forEach((key, index) => {
				const formattedPropertyNameForSearch = rowGroupCols[index].field.charAt(0).toUpperCase() + rowGroupCols[index].field.slice(1);
				let currentNode = null;
				params.api.forEachNode((node) => {
					const nodeKey = node.field;
					if (nodeKey?.toLowerCase() === formattedPropertyNameForSearch?.toLowerCase() && node?.data?.[nodeKey] === key) {
						currentNode = node;
						return;
					}
				});
				let searchValue = key;
				if (currentNode) {
					const cleanData = getCleanData ? getCleanData(currentNode.data, lookupsMap) : null;
					if (cleanData) searchValue = cleanData[currentNode.field];
				}
				searchObject.filterElements.push({
					searchField: formattedPropertyNameForSearch,
					searchValue,
					searchOperator: "="
				});
			});
			searchObject.groupingType = SearchGroupingType.Distinct;
			const rowGroupCol = rowGroupCols[groupKeys.length];
			const formattedPropertyName = rowGroupCol.field.charAt(0).toUpperCase() + rowGroupCol.field.slice(1);
			if (!searchObject.groupProperty.includes(formattedPropertyName)) {
				searchObject.groupProperty.push(formattedPropertyName);
				const orderElements = searchObject.orderElements;
				if (!orderElements.find((element) => element.sortColumn === formattedPropertyName)) orderElements.push({
					sortColumn: formattedPropertyName,
					sortDirection: SearchSortDirection.Asc
				});
			}
		}
		if (!startRow && rowGroupCols.length === 0 && !sortModel.length && !Object.keys(filterModel).length && !groupKeys.length && queryData) if (queryData.pageList && queryData.pageList) {
			params.success({
				rowData: queryData.pageList,
				rowCount: parseInt(queryData.total)
			});
			if (storeRowData) storeRowData(queryData.pageList);
		} else {
			params.success({
				rowData: [],
				rowCount: 0
			});
			if (storeRowData) storeRowData([]);
		}
		else {
			let response = null;
			if (executeQuery) response = await executeQuery({ data: searchObject }, apiMethodId, lookupsMap).catch((e) => params.success({
				rowData: [],
				rowCount: 0
			}));
			const rowData = response?.status === 200 ? response?.data : response;
			if (rowData && response) if (rowData?.pageList?.length > 0) {
				params.success({
					rowData: rowData.pageList,
					rowCount: parseInt(rowData.total)
				});
				if (storeRowData) storeRowData(rowData.pageList);
			} else {
				params.success({
					rowData: [],
					rowCount: 0
				});
				if (storeRowData) storeRowData([]);
			}
			else {
				params.success({
					rowData: [],
					rowCount: 0
				});
				if (storeRowData) storeRowData([]);
			}
		}
	} };
};
//#endregion
export { SearchServiceDatasource };

//# sourceMappingURL=SearchServiceDatasource.mjs.map