import { useCallback } from 'react';
import { UdpKeywordEnums } from '../enums/unitySystemEnums';
import { FilterElementParamType } from '../enums/searchEnums';
import { useUser } from '../utilities/auth/useUser';
import i18n from 'i18next';
import { ConfigService } from '../configService';
export const useSearchFilterParamsMap = () => {
    const user = useUser();
    const getKeywordValue = useCallback((keyword) => {
        let keywordValue = '';
        switch (keyword) {
            case UdpKeywordEnums.Udp_Tenant_Id:
                keywordValue = (user === null || user === void 0 ? void 0 : 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 = []) => {
        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);
                    }
                    break;
                default:
                    break;
            }
        });
        return paramsMap;
    }, [getFunctionValue, getKeywordValue]);
    const getQueryFilterElements = useCallback((apiMethodInstance, params = []) => {
        var _a;
        if (!apiMethodInstance)
            return [];
        let filterElements = [];
        try {
            const bodyStr = apiMethodInstance.body;
            const body = JSON.parse(bodyStr);
            // Parameters for getParamsMap needs to be an array of params and an array of anonymous functions
            // Example is at the top called FILTER_PARAMS and FUNCTION_LIST
            const paramsMap = getParamsMap(params);
            // Checks for any filter elements that have been configured with params. and
            // will use paramsMap to grab filter element
            (_a = body === null || body === void 0 ? void 0 : body.filterElements) === null || _a === void 0 ? void 0 : _a.forEach((filter) => {
                if (filter.searchValue.startsWith('params.')) {
                    const key = filter.searchValue.replace('params.', '');
                    if (paramsMap[key]) {
                        filter.searchValue = paramsMap[key];
                        filterElements.push(filter);
                    }
                }
                else {
                    filterElements.push(filter);
                }
            });
        }
        catch (_b) {
            console.warn('Error parsing apiMethodInstance body');
        }
        return filterElements;
    }, [getParamsMap]);
    const getQueryFilterGroups = useCallback((apiMethodInstance, params = []) => {
        if (!apiMethodInstance)
            return [];
        let filterGroups = [];
        try {
            const bodyStr = apiMethodInstance.body;
            const body = JSON.parse(bodyStr);
            filterGroups = [...body.filterGroups];
        }
        catch (_a) {
            console.warn('Error parsing apiMethodInstance body');
        }
        return filterGroups;
    }, []);
    const getQuerySearchOperator = useCallback((apiMethodInstance, params = []) => {
        if (!apiMethodInstance)
            return [];
        let operator = 0;
        try {
            const bodyStr = apiMethodInstance.body;
            const body = JSON.parse(bodyStr);
            operator = body.logicalSearchOperator;
        }
        catch (_a) {
            console.warn('Error parsing apiMethodInstance body');
        }
        return operator;
    }, []);
    return { getParamsMap, getQueryFilterElements, getQueryFilterGroups, getQuerySearchOperator };
};
//# sourceMappingURL=useSearchFilterParamsMap.js.map