import { LogicalSearchOperatorEnums, SearchGroupingType, UdpSearchEnums } from "../../enums/unitySystemEnums.mjs";
import { useUser } from "../auth/useUser.mjs";
import { AgGridFilterOperators, AgGridFilterTypes, SearchValueType, SqlFilterOperators } from "../../enums/aggridEnums.mjs";
import { c } from "react-compiler-runtime";
import { DateTime, IANAZone } from "luxon";
//#region src/utilities/search/SearchUtilities.ts
const getSearchObject = (searchFilterElements, page, pageSize, eagerLoad = false, logicalSearchOperator = LogicalSearchOperatorEnums.And, searchFilterGroups = []) => {
	const searchObject = {
		pageNumber: page || 1,
		pageSize: pageSize || 20,
		filterElements: [],
		filterGroups: [],
		orderElements: [],
		groupingType: "",
		groupProperty: [],
		groupOperationList: [],
		eagerLoad,
		logicalSearchOperator
	};
	if (searchFilterElements) searchFilterElements.forEach((property) => {
		const formattedPropertyName = property.searchField;
		if ("sortDirection" in property && property.sortDirection) {
			const orderElement = {
				sortColumn: formattedPropertyName,
				sortDirection: property.sortDirection
			};
			searchObject.orderElements.push(orderElement);
		} else searchObject.filterElements.push(property);
		if ("distinct" in property && property.distinct) {
			searchObject.groupingType = SearchGroupingType.Distinct;
			searchObject.groupProperty?.push(formattedPropertyName);
		}
	});
	searchObject.filterGroups = searchFilterGroups;
	return searchObject;
};
const getApiMethodIdByPath = (apiMethodList, path) => {
	return apiMethodList.find((method) => path.includes(method.path))?.apiMethodId ?? null;
};
const getSearchIdentifiers = (catalogObjects, catalogReturnObject) => {
	if (!catalogObjects) return [];
	return (catalogObjects.find((co) => co.catalogObjectName === catalogReturnObject)?.properties?.filter((property) => property?.extendedProperties?.identifier))?.map((property) => property.name) ?? [];
};
/**
* Returns a reference to a function that takes an Ag-grid FilterModel object and
* processes it to create an array of SearchFilters.
* @param timeZone string or null;
* @returns Function for processing Ag-grid FilterModel
*/
const useConvertFilterModelToSearchFilters = (t0) => {
	const $ = c(5);
	const timeZone = t0 === void 0 ? null : t0;
	const user = useUser();
	const tz = timeZone ?? user?.additionalProperties?.activeUtilityTimeZone ?? "local";
	let t1;
	if ($[0] !== tz || $[1] !== user?.additionalProperties?.activeUtilityTimeZone) {
		t1 = tz !== "local" && (tz === user?.additionalProperties?.activeUtilityTimeZone || IANAZone.isValidZone(tz)) ? tz : "local";
		$[0] = tz;
		$[1] = user?.additionalProperties?.activeUtilityTimeZone;
		$[2] = t1;
	} else t1 = $[2];
	const zone = t1;
	let t2;
	if ($[3] !== zone) {
		t2 = (t3) => {
			const filterModel = t3 === void 0 ? null : t3;
			if (!filterModel) return [];
			let searchFilters;
			searchFilters = Object.entries(filterModel).map((item) => {
				const field = item[0];
				let operator;
				let type;
				let value;
				if (item[1]?.filterType === AgGridFilterTypes.DATE) {
					const dateSplit = (item[1]?.dateFrom?.substring(0, 10)).split("-");
					const date = new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2]);
					value = DateTime.fromJSDate(date).setZone(zone, { keepLocalTime: true }).toJSDate().toISOString();
				} else if (item[1]?.filterType === AgGridFilterTypes.SET) {
					value = item[1].values.join(",");
					operator = SqlFilterOperators.IN;
				} else value = item[1]?.filter;
				bb78: switch (item[1]?.type?.toLowerCase()) {
					case AgGridFilterOperators.CONTAINS:
						operator = SqlFilterOperators.LIKE;
						break bb78;
					case AgGridFilterOperators.EQUALS:
						operator = SqlFilterOperators.EQUALS;
						if (item[1]?.filterType === AgGridFilterTypes.DATE) type = SearchValueType.Type_DateTime;
						break bb78;
					case AgGridFilterOperators.NOT_EQUALS:
						operator = SqlFilterOperators.NOT_EQUALS;
						break bb78;
					case AgGridFilterOperators.GREATER_THAN:
						operator = SqlFilterOperators.GREATER_THAN;
						break bb78;
					case AgGridFilterOperators.GREATER_THAN_OR_EQUAL:
						operator = SqlFilterOperators.GREATER_THAN_OR_EQUAL;
						break bb78;
					case AgGridFilterOperators.LESS_THAN:
						operator = SqlFilterOperators.LESS_THAN;
						break bb78;
					case AgGridFilterOperators.LESS_THAN_OR_EQUAL:
						operator = SqlFilterOperators.LESS_THAN_OR_EQUAL;
						break bb78;
					case AgGridFilterOperators.STARTSWITH:
						operator = SqlFilterOperators.STARTSWITH;
						break bb78;
					case AgGridFilterOperators.IN_RANGE:
						operator = SqlFilterOperators.BETWEEN;
						if (item[1]?.filterType === AgGridFilterTypes.NUMBER) value = value + ("," + item[1]?.filterTo);
						else if (item[1]?.filterType === AgGridFilterTypes.DATE) {
							const dateSplit_0 = (item[1]?.dateTo?.substring(0, 10)).split("-");
							const date_0 = new Date(dateSplit_0[0], dateSplit_0[1] - 1, dateSplit_0[2]);
							const dateString_0 = DateTime.fromJSDate(date_0).setZone(zone, { keepLocalTime: true }).toJSDate().toISOString();
							value = value + ("," + dateString_0);
						}
						break bb78;
					case AgGridFilterOperators.ENDSWITH:
						operator = SqlFilterOperators.ENDSWITH;
						break bb78;
					default:
				}
				return {
					searchField: field,
					searchValue: value,
					searchOperator: operator,
					searchValueType: type,
					groupId: null
				};
			});
			return searchFilters;
		};
		$[3] = zone;
		$[4] = t2;
	} else t2 = $[4];
	return t2;
};
const parseSearchParamsIntoFilterObjects = () => {
	const qs = new URLSearchParams(window.location.search);
	const filterObjects = [];
	qs.forEach((value, key) => {
		if (key.startsWith(UdpSearchEnums.UdpQueryParamPrefix)) {
			const keyParts = key.split("-");
			const searchField = keyParts[1] || void 0;
			const searchOperator = SqlFilterOperators[(keyParts[2] || void 0).toUpperCase()] || void 0;
			const searchValue = value || void 0;
			if (searchField && searchOperator && searchValue) filterObjects.push({
				searchField,
				searchOperator,
				searchValue
			});
		}
	});
	return filterObjects;
};
//#endregion
export { getApiMethodIdByPath, getSearchIdentifiers, getSearchObject, parseSearchParamsIntoFilterObjects, useConvertFilterModelToSearchFilters };

//# sourceMappingURL=SearchUtilities.mjs.map