const positiveInt = (value, fallback) => {
    const parsed = Number(value);
    return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
};
export function decodeCrudQuery(params, fallback) {
    const sorts = params.getAll('sort').flatMap(value => {
        const parts = value.split(':');
        const field = parts[0];
        const direction = parts[1];
        if (field && (direction === 'asc' || direction === 'desc')) {
            return [{ field, direction: direction }];
        }
        return [];
    });
    const filters = params.getAll('filter').flatMap(value => {
        const first = value.indexOf(':');
        const second = value.indexOf(':', first + 1);
        if (first <= 0 || second <= first)
            return [];
        return [{ field: value.slice(0, first), operator: value.slice(first + 1, second), value: value.slice(second + 1) }];
    });
    return {
        page: positiveInt(params.get('page'), fallback.page),
        pageSize: positiveInt(params.get('pageSize'), fallback.pageSize),
        search: params.get('search')?.trim() || undefined,
        sorts: sorts.length ? sorts : fallback.sorts,
        filters: filters.length ? filters : fallback.filters
    };
}
export function encodeCrudQuery(query) {
    const params = new URLSearchParams();
    params.set('page', String(query.page));
    params.set('pageSize', String(query.pageSize));
    if (query.search)
        params.set('search', query.search);
    for (const sort of query.sorts)
        params.append('sort', `${sort.field}:${sort.direction}`);
    for (const filter of query.filters)
        params.append('filter', `${filter.field}:${filter.operator}:${String(filter.value ?? '')}`);
    return params;
}
