function parseJSON(str, fallback) {
    try {
        return JSON.parse(str ?? '{}');
    } catch (e) {
        console.error('Error parsing JSON:', e);
        return fallback;
    }
}
export default function (route, param) {
    // catch route query params
    const params = parseJSON(route?.query?.table, {});
    // extract and parse filter from params
    const { filter: filterStr = '{}' } = params;

    const where = parseJSON(filterStr, {})?.where;

    if (where && !param) {
        return where;
    } else if (where && where.and) {
        const foundParam = where.and.find((p) => p[param]);
        if (foundParam) {
            return foundParam[param];
        }
    } else if (where && where[param]) {
        return where[param];
    }
    return null;
}
