"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.explainFilter = explainFilter;
const util_1 = require("../../util");
const explainPredicate_1 = require("./explainPredicate");
/**
 * Generate an explanation if a specific result passes the filter's criteria
 * @param filter
 * @param result
 * @param requestOptions
 * @returns
 */
async function explainFilter(filter, result, requestOptions) {
    // setup return value
    const explanation = {
        filter: (0, util_1.cloneObject)(filter),
        matched: false,
        reasons: [],
    };
    // for each predicate, explain the match and return the explanation
    for (const predicate of filter.predicates) {
        const r = await (0, explainPredicate_1.explainPredicate)(predicate, result, requestOptions);
        explanation.reasons.push(r);
    }
    // depending on the operation, we combine the predicate results differently
    if (filter.operation === "OR") {
        // if any of the predicates match, then the filter matches
        explanation.matched = explanation.reasons.some((r) => r.matched);
    }
    else {
        // filter.operation defaults to AND
        explanation.matched = explanation.reasons.every((r) => r.matched);
    }
    // return
    return explanation;
}
//# sourceMappingURL=explainFilter.js.map