"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeQueryParams = exports.applyGenericQueryParams = exports.applySearchFilters = void 0;
const applySearchFilters = (qb, searchParams, columns) => {
    const hasSearchParams = searchParams?.length;
    if (hasSearchParams) {
        const sqlParameters = searchParams.map((item) => `%${item}%`);
        const sqlQueryParameters = sqlParameters.map(() => '?').join(',');
        qb.where((builder) => {
            columns.forEach((column) => {
                builder.orWhereRaw(`(${column}) ILIKE ANY (ARRAY[${sqlQueryParameters}])`, sqlParameters);
            });
        });
    }
};
exports.applySearchFilters = applySearchFilters;
const applyGenericQueryParams = (query, queryParams) => {
    queryParams.forEach((param) => {
        switch (param.operator) {
            case 'IS':
            case 'IS_ANY_OF':
                query.whereIn(param.field, param.values);
                break;
            case 'IS_NOT':
            case 'IS_NONE_OF':
                query.whereNotIn(param.field, param.values);
                break;
            case 'IS_BEFORE':
                query.where(param.field, '<', param.values[0]);
                break;
            case 'IS_ON_OR_AFTER':
                query.where(param.field, '>=', param.values[0]);
                break;
        }
    });
};
exports.applyGenericQueryParams = applyGenericQueryParams;
const normalizeQueryParams = (params, defaults) => {
    const { query, offset, limit = defaults.limitDefault, sortOrder, sortBy = defaults.sortByDefault, } = params;
    const normalizedQuery = query
        ?.split(',')
        .map((query) => query.trim())
        .filter((query) => query);
    const maxLimit = defaults.maxLimit || 1000;
    const normalizedLimit = Number(limit) > 0 && Number(limit) <= maxLimit
        ? Number(limit)
        : defaults.limitDefault;
    const normalizedOffset = Number(offset) > 0 ? Number(offset) : 0;
    const normalizedSortBy = sortBy;
    const normalizedSortOrder = sortOrder === 'asc' || sortOrder === 'desc' ? sortOrder : 'asc';
    return {
        normalizedQuery,
        normalizedLimit,
        normalizedOffset,
        normalizedSortBy,
        normalizedSortOrder,
    };
};
exports.normalizeQueryParams = normalizeQueryParams;
//# sourceMappingURL=search-utils.js.map