import { ConfigService } from "../configService/index.mjs";
import { UdpKeywordEnums } from "../enums/unitySystemEnums.mjs";
import { useUser } from "../utilities/auth/useUser.mjs";
import { FilterElementParamType } from "../enums/searchEnums.mjs";
import { useCallback } from "react";
import i18n from "i18next";
//#region src/hooks/useSearchFilterParamsMap.js
const useSearchFilterParamsMap = () => {
	const user = useUser();
	const getKeywordValue = useCallback((keyword) => {
		let keywordValue = "";
		switch (keyword) {
			case UdpKeywordEnums.Udp_Tenant_Id:
				keywordValue = user?.currentTenantId || "";
				break;
			case UdpKeywordEnums.Udp_Product_Id:
				keywordValue = ConfigService.config.UNITY_PRODUCT_ID || "";
				break;
			case UdpKeywordEnums.Udp_Language_Id:
				keywordValue = i18n.language || "";
				break;
			default: break;
		}
		return keywordValue;
	}, [user]);
	const isFunction = (functionToCheck) => {
		return functionToCheck && {}.toString.call(functionToCheck) === "[object Function]";
	};
	const getFunctionValue = useCallback((functionIndex, functionList) => {
		const functionToExecute = functionList[functionIndex];
		if (isFunction(functionToExecute)) return functionToExecute() || "";
		return "";
	}, []);
	/**
	* Helper function that creates a map that will be used to determine
	* the filter elements for a data browser query configuration using params.
	* as a searchValue
	*
	* @param {Array<Object>} params - list of param objects to determine the map, example:
	* [
	{
	name: 'tenantName',
	value: 'ABC Company',
	type: 'string'
	},
	{
	name: 'tenantId',
	value: 'UDP_TenantId_ID',
	type: 'keyword'
	},
	{
	name: 'productId',
	value: 0,
	type: 'function'
	}
	]
	* @param {Array<function>} functionList - list of anonymous functions for the function param to use
	*/
	const getParamsMap = useCallback((params = [], functionList_0 = []) => {
		const paramsMap = {};
		params.forEach((param) => {
			switch (param.type) {
				case FilterElementParamType.String:
					if (param.name) paramsMap[param.name] = param.value;
					break;
				case FilterElementParamType.Keyword:
					if (param.name) paramsMap[param.name] = getKeywordValue(param.value);
					break;
				case FilterElementParamType.Function:
					if (param.name) paramsMap[param.name] = getFunctionValue(param.value, functionList_0);
					break;
				default: break;
			}
		});
		return paramsMap;
	}, [getFunctionValue, getKeywordValue]);
	return {
		getParamsMap,
		getQueryFilterElements: useCallback((apiMethodInstance, params_0 = []) => {
			if (!apiMethodInstance) return [];
			let filterElements = [];
			try {
				const bodyStr = apiMethodInstance.body;
				const body = JSON.parse(bodyStr);
				const paramsMap_0 = getParamsMap(params_0);
				body?.filterElements?.forEach((filter) => {
					if (filter.searchValue.startsWith("params.")) {
						const key = filter.searchValue.replace("params.", "");
						if (paramsMap_0[key]) {
							filter.searchValue = paramsMap_0[key];
							filterElements.push(filter);
						}
					} else filterElements.push(filter);
				});
			} catch {
				console.warn("Error parsing apiMethodInstance body");
			}
			return filterElements;
		}, [getParamsMap]),
		getQueryFilterGroups: useCallback((apiMethodInstance_0, params_1 = []) => {
			if (!apiMethodInstance_0) return [];
			let filterGroups = [];
			try {
				const bodyStr_0 = apiMethodInstance_0.body;
				filterGroups = [...JSON.parse(bodyStr_0).filterGroups];
			} catch {
				console.warn("Error parsing apiMethodInstance body");
			}
			return filterGroups;
		}, []),
		getQuerySearchOperator: useCallback((apiMethodInstance_1, params_2 = []) => {
			if (!apiMethodInstance_1) return [];
			let operator = 0;
			try {
				const bodyStr_1 = apiMethodInstance_1.body;
				operator = JSON.parse(bodyStr_1).logicalSearchOperator;
			} catch {
				console.warn("Error parsing apiMethodInstance body");
			}
			return operator;
		}, [])
	};
};
//#endregion
export { useSearchFilterParamsMap };

//# sourceMappingURL=useSearchFilterParamsMap.mjs.map