import { LogicalSearchOperatorEnums, SearchGroupingType, SearchSortDirection } from "../../enums/unitySystemEnums.mjs";
import { getSearchObject } from "../../utilities/search/SearchUtilities.mjs";
import { FilterOperators } from "../../models/SearchOperator.mjs";
//#region src/inquiry/search/InquirySearchDatasource.js
const InquirySearchDatasource = (queryData, apiMethodId, searchFilterElements, logicalSearchOperator = LogicalSearchOperatorEnums.And, executeQuery, searchFilterGroups) => {
	return { async getRows(params) {
		const { startRow, endRow, rowGroupCols, groupKeys, valueCols, filterModel, sortModel } = params.request;
		const searchObject = getSearchObject(searchFilterElements, endRow / 20, null, false, logicalSearchOperator, searchFilterGroups);
		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);
				const element = filterElements.find((element) => element.searchField === formattedPropertyName);
				if (element) {
					element.searchValue = filterItem.filter;
					element.searchOperator = FilterOperators[filterItem.type];
				} else 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);
				if (element) {
					element.searchValue = key;
					element.searchOperator = "=";
				} else filterElements.push({
					searchField: formattedPropertyName,
					searchValue: key,
					searchOperator: "="
				});
			});
		} else {
			groupKeys.forEach((key, index) => {
				const formattedPropertyNameForSearch = rowGroupCols[index].field.charAt(0).toUpperCase() + rowGroupCols[index].field.slice(1);
				searchObject.filterElements.push({
					searchField: formattedPropertyNameForSearch,
					searchValue: key,
					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.successCallback(queryData.pageList, queryData.total);
		else params.successCallback([], 0);
		else {
			let response = null;
			if (executeQuery) response = await executeQuery({ data: searchObject }, apiMethodId);
			const rowData = response?.data;
			if (rowData) if (rowData?.pageList?.length > 0) params.successCallback(rowData.pageList, rowData.total);
			else params.successCallback([], 0);
			else params.successCallback([], 0);
		}
	} };
};
//#endregion
export { InquirySearchDatasource };

//# sourceMappingURL=InquirySearchDatasource.mjs.map