import { _serialize, serializeQuery } from './client.js';
export const minilinksQuery = (query, ml) => {
    if (typeof (query) === 'number' || typeof (query) === 'string')
        return [ml.byId[query]];
    else {
        const q = serializeQuery(query);
        const result = minilinksQueryHandle(q.where, ml);
        return q.limit ? result.slice(q.offset || 0, (q.offset || 0) + (q.limit)) : result;
    }
};
export const minilinksQueryIs = (query, link) => {
    if (typeof (query) === 'number')
        return link.id === query;
    else {
        const q = serializeQuery(query);
        return minilinksQueryLevel(q, link, 'links');
    }
};
export const minilinksQueryHandle = (q, ml) => {
    return ml.links.filter((link) => {
        const r = minilinksQueryLevel(q, link, 'links');
        return r;
    });
};
export const minilinksQueryLevel = (q, link, env = 'links') => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const fields = Object.keys(q);
    if (env === 'links') {
        for (const f in fields) {
            const field = fields[f];
            if (field === '_and') {
                for (const a in q[field]) {
                    if (!minilinksQueryLevel(q[field][a], link, env)) {
                        return false;
                    }
                }
            }
            else if (field === '_or') {
                let oneOf = false;
                for (const a in q[field]) {
                    if (minilinksQueryLevel(q[field][a], link, env)) {
                        oneOf = true;
                        break;
                    }
                }
                return oneOf;
            }
            else if (field === '_not') {
                if (minilinksQueryLevel(q[field], link, env)) {
                    return false;
                }
            }
            else if ((_b = (_a = _serialize === null || _serialize === void 0 ? void 0 : _serialize[env]) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b[field]) {
                if (!minilinksQueryComparison(q, link, field, env)) {
                    return false;
                }
            }
            else {
                if (((_d = (_c = _serialize === null || _serialize === void 0 ? void 0 : _serialize[env]) === null || _c === void 0 ? void 0 : _c.relations) === null || _d === void 0 ? void 0 : _d[field]) === 'links') {
                    if (typeof (q[field]) === 'object') {
                        if (typeof (q[field].length) === 'number') {
                            if (!q[field].length)
                                return false;
                            let found = false;
                            for (const sub in link === null || link === void 0 ? void 0 : link[field]) {
                                const sublink = link === null || link === void 0 ? void 0 : link[field][sub];
                                if (minilinksQueryLevel(q[field], sublink, env)) {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                                return false;
                        }
                        else {
                            if (Array.isArray(link === null || link === void 0 ? void 0 : link[field])) {
                                let found = false;
                                for (const el in link === null || link === void 0 ? void 0 : link[field]) {
                                    if (minilinksQueryLevel(q[field], link === null || link === void 0 ? void 0 : link[field][el], env)) {
                                        found = true;
                                        break;
                                    }
                                }
                                if (!found)
                                    return false;
                            }
                            else {
                                if (!minilinksQueryLevel(q[field], link === null || link === void 0 ? void 0 : link[field], env)) {
                                    return false;
                                }
                            }
                        }
                    }
                }
                else if (((_f = (_e = _serialize === null || _serialize === void 0 ? void 0 : _serialize[env]) === null || _e === void 0 ? void 0 : _e.relations) === null || _f === void 0 ? void 0 : _f[field]) === 'value') {
                    if (!(link === null || link === void 0 ? void 0 : link[field]) || !minilinksQueryLevel(q[field], link === null || link === void 0 ? void 0 : link[field], 'value')) {
                        return false;
                    }
                }
                else {
                    return false;
                }
            }
        }
    }
    else if (env === 'value') {
        for (const f in fields) {
            const field = fields[f];
            if ((_h = (_g = _serialize === null || _serialize === void 0 ? void 0 : _serialize[env]) === null || _g === void 0 ? void 0 : _g.fields) === null || _h === void 0 ? void 0 : _h[field]) {
                if (!minilinksQueryComparison(q, link, field, env)) {
                    return false;
                }
            }
            else {
                return false;
            }
        }
    }
    else {
        return false;
    }
    return true;
};
export const minilinksQueryComparison = (q, link, field, env = 'links') => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
    const comp = q === null || q === void 0 ? void 0 : q[field];
    if (typeof (comp) === 'undefined')
        throw new Error(`${field} === undefined`);
    if (typeof (link === null || link === void 0 ? void 0 : link[field]) === 'object' && Array.isArray(link === null || link === void 0 ? void 0 : link[field])) {
        if (comp.hasOwnProperty('_eq')) {
            if (!(link === null || link === void 0 ? void 0 : link[field].includes(comp._eq)))
                return false;
        }
        if (comp.hasOwnProperty('_neq')) {
            if (link === null || link === void 0 ? void 0 : link[field].includes(comp._neq))
                return false;
        }
    }
    else {
        if (typeof (link === null || link === void 0 ? void 0 : link[field]) === 'undefined')
            return false;
        if (comp.hasOwnProperty('_eq')) {
            if ((link === null || link === void 0 ? void 0 : link[field]) !== comp._eq)
                return false;
        }
        if (comp.hasOwnProperty('_neq')) {
            if ((link === null || link === void 0 ? void 0 : link[field]) === comp._neq)
                return false;
        }
        if (comp.hasOwnProperty('_gt')) {
            if (!((link === null || link === void 0 ? void 0 : link[field]) > comp._gt))
                return false;
        }
        if (comp.hasOwnProperty('_gte')) {
            if (!((link === null || link === void 0 ? void 0 : link[field]) >= comp._gte))
                return false;
        }
        if (comp.hasOwnProperty('_lt')) {
            if (!((link === null || link === void 0 ? void 0 : link[field]) < comp._lt))
                return false;
        }
        if (comp.hasOwnProperty('_lte')) {
            if (!((link === null || link === void 0 ? void 0 : link[field]) <= comp._lte))
                return false;
        }
        if (comp.hasOwnProperty('_is_null')) {
            if (((link === null || link === void 0 ? void 0 : link[field]) === null) === comp._is_null)
                return false;
        }
        if (comp.hasOwnProperty('_in')) {
            if (!((_a = comp === null || comp === void 0 ? void 0 : comp._in) === null || _a === void 0 ? void 0 : _a.includes(link === null || link === void 0 ? void 0 : link[field])))
                return false;
        }
        if (comp.hasOwnProperty('_nin')) {
            if ((_b = comp === null || comp === void 0 ? void 0 : comp._nin) === null || _b === void 0 ? void 0 : _b.includes(link === null || link === void 0 ? void 0 : link[field]))
                return false;
        }
        if (comp.hasOwnProperty('_ilike')) {
            if (!((_c = link === null || link === void 0 ? void 0 : link[field]) === null || _c === void 0 ? void 0 : _c.toLowerCase) || !((_e = (_d = link === null || link === void 0 ? void 0 : link[field]) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === null || _e === void 0 ? void 0 : _e.includes((_f = comp === null || comp === void 0 ? void 0 : comp._ilike) === null || _f === void 0 ? void 0 : _f.toLowerCase())))
                return false;
        }
        if (comp.hasOwnProperty('_nilike')) {
            if (!((_g = link === null || link === void 0 ? void 0 : link[field]) === null || _g === void 0 ? void 0 : _g.toLowerCase) || !((_j = (_h = link === null || link === void 0 ? void 0 : link[field]) === null || _h === void 0 ? void 0 : _h.toLowerCase()) === null || _j === void 0 ? void 0 : _j.includes((_k = comp === null || comp === void 0 ? void 0 : comp._nilike) === null || _k === void 0 ? void 0 : _k.toLowerCase())))
                return false;
        }
        if (comp.hasOwnProperty('_like')) {
            if (!((_l = link === null || link === void 0 ? void 0 : link[field]) === null || _l === void 0 ? void 0 : _l.includes(comp === null || comp === void 0 ? void 0 : comp._like)))
                return false;
        }
        if (comp.hasOwnProperty('_nlike')) {
            if (!((_m = link === null || link === void 0 ? void 0 : link[field]) === null || _m === void 0 ? void 0 : _m.includes(comp === null || comp === void 0 ? void 0 : comp._nlike)))
                return false;
        }
        if (comp.hasOwnProperty('_iregex')) {
            if (!((_o = link === null || link === void 0 ? void 0 : link[field]) === null || _o === void 0 ? void 0 : _o.toLowerCase) || !((_p = link === null || link === void 0 ? void 0 : link[field]) === null || _p === void 0 ? void 0 : _p.toLowerCase().match(comp === null || comp === void 0 ? void 0 : comp._iregex)))
                return false;
        }
        if (comp.hasOwnProperty('_regex')) {
            if (!(link === null || link === void 0 ? void 0 : link[field].match(comp === null || comp === void 0 ? void 0 : comp._regex)))
                return false;
        }
        if (comp.hasOwnProperty('_niregex')) {
            if (!((_q = link === null || link === void 0 ? void 0 : link[field]) === null || _q === void 0 ? void 0 : _q.toLowerCase) || !((_r = link === null || link === void 0 ? void 0 : link[field]) === null || _r === void 0 ? void 0 : _r.toLowerCase().match(comp === null || comp === void 0 ? void 0 : comp._niregex)))
                return false;
        }
        if (comp.hasOwnProperty('_nregex')) {
            if (!(link === null || link === void 0 ? void 0 : link[field].match(comp === null || comp === void 0 ? void 0 : comp._nregex)))
                return false;
        }
        return true;
    }
};
//# sourceMappingURL=minilinks-query.js.map