"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWhereClause = buildWhereClause;
function buildWhereClause(query, allowedFilters, globalSearchColumns) {
    const where = {};
    if (query.search && globalSearchColumns.length > 0) {
        where.OR = globalSearchColumns.map((column) => {
            if (column === 'items.itemCode') {
                return {
                    items: {
                        some: {
                            itemCode: {
                                contains: query.search,
                                mode: 'insensitive',
                            },
                        },
                    },
                };
            }
            return {
                [column]: {
                    contains: query.search,
                    mode: 'insensitive',
                },
            };
        });
    }
    if (query.filter) {
        Object.entries(query.filter).forEach(([key, value]) => {
            if (!allowedFilters[key])
                return;
            if (value === undefined || value === null || value === '')
                return;
            const type = allowedFilters[key];
            if (key.endsWith('_gte') || key.endsWith('_lte')) {
                const [field, operator] = key.split('_');
                if (!where[field]) {
                    where[field] = {};
                }
                if (operator === 'gte') {
                    where[field].gte = new Date(value);
                }
                if (operator === 'lte') {
                    where[field].lte = new Date(value);
                }
                return;
            }
            switch (type) {
                case 'string':
                    where[key] = {
                        contains: value,
                        mode: 'insensitive',
                    };
                    break;
                case 'enum':
                    if (Array.isArray(value)) {
                        where[key] = { in: value };
                    }
                    else {
                        where[key] = value;
                    }
                    break;
                case 'date':
                    where[key] = new Date(value);
                    break;
                case 'exact':
                    where[key] = value;
                    break;
                case 'dateRange':
                    if (typeof value === 'object') {
                        const range = {};
                        if (value.gte) {
                            range.gte = new Date(value.gte);
                        }
                        if (value.lte) {
                            range.lte = new Date(value.lte);
                        }
                        if (Object.keys(range).length > 0) {
                            where[key] = range;
                        }
                    }
                    break;
            }
        });
    }
    return where;
}
//# sourceMappingURL=prisma-query-builder.js.map