import { useLocation } from 'react-router-dom';
import { useMemo } from 'react';
import { UdpSearchEnums } from '../enums/unitySystemEnums';
export const useUdpGridFilterModel = () => {
    const { search } = useLocation();
    const searchParams = useMemo(() => new URLSearchParams(search), [search]);
    const filterModel = useMemo(() => {
        const constructedFilterModel = {};
        searchParams.forEach((value, key) => {
            if (key.startsWith(`${UdpSearchEnums.UdpQueryParamPrefix}-`)) {
                const paramBody = key.replace(`${UdpSearchEnums.UdpQueryParamPrefix}-`, '');
                const lastHyphenIndex = paramBody.lastIndexOf('-');
                if (lastHyphenIndex !== -1) {
                    const field = paramBody.substring(0, lastHyphenIndex);
                    const rawOp = paramBody.substring(lastHyphenIndex + 1);
                    const operator = rawOp.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
                    if (operator.toLowerCase() === 'in') {
                        constructedFilterModel[field] = {
                            filterType: 'set',
                            values: value.split(',')
                        };
                    }
                    else {
                        // Determine Filter Type & Parse Value
                        const { filterType, filterKey, parsedValue } = parseFilterValue(value);
                        const agGridOperator = operator === 'isNull'
                            ? value === 'true'
                                ? 'blank'
                                : 'notBlank'
                            : operator;
                        const condition = Object.assign({ filterType, type: agGridOperator }, (filterKey !== null && { [filterKey]: parsedValue }));
                        if (constructedFilterModel[field]) {
                            constructedFilterModel[field].conditions.push(condition);
                        }
                        else {
                            constructedFilterModel[field] = {
                                filterType: filterType,
                                operator: 'OR',
                                conditions: [condition]
                            };
                        }
                    }
                }
            }
        });
        return constructedFilterModel;
    }, [searchParams]);
    return filterModel;
};
const parseFilterValue = (value) => {
    if (value === 'true' || value === 'false') {
        return { filterType: 'text', filterKey: null, parsedValue: null };
    }
    const isDate = !isNaN(Date.parse(value)) && (value.includes('-') || value.includes('/'));
    if (isDate) {
        return {
            filterType: 'date',
            filterKey: 'dateFrom',
            parsedValue: value
        };
    }
    if (!isNaN(value) && !isNaN(parseFloat(value))) {
        return {
            filterType: 'number',
            filterKey: 'filter',
            parsedValue: Number(value)
        };
    }
    return {
        filterType: 'text',
        filterKey: 'filter',
        parsedValue: value
    };
};
//# sourceMappingURL=useUdpGridFilterModel.js.map