"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterQueryPropagateMessages = exports.filterQueryResponseMessages = exports.filterQueryRequestMessages = void 0;
const rxjs_1 = require("rxjs");
/**
 * This function encapsulates the common filtering logic.
 * The order of the filters is important as it reduces the workload by filtering the most specific conditions first.
 */
const filterSystemMessageBasedOnCriteria = ({ systemMessage$, queryNodeAddress$, impossibleRequestIds$, httpResponseRequestId$, getRequestPublisherId, }) => (0, rxjs_1.combineLatest)({
    message: systemMessage$,
    queryNodeAddress: queryNodeAddress$,
    impossibleRequestIds: impossibleRequestIds$,
    httpResponseRequestId: httpResponseRequestId$,
}).pipe(
// select messages that match requestId from http response, if we already have it, otherwise filter impossible requests
(0, rxjs_1.filter)(({ httpResponseRequestId, message, impossibleRequestIds }) => httpResponseRequestId === null
    ? !impossibleRequestIds.has(message.message.requestId)
    : message.message.requestId === httpResponseRequestId), 
// select messages published from query node address
(0, rxjs_1.filter)(({ queryNodeAddress, message }) => queryNodeAddress === getRequestPublisherId(message)));
const filterQueryRequestMessages = (request$, queryNodeAddress$, impossibleRequestIds$, httpResponseRequestId$) => filterSystemMessageBasedOnCriteria({
    systemMessage$: request$,
    queryNodeAddress$,
    impossibleRequestIds$,
    httpResponseRequestId$,
    getRequestPublisherId: (v) => v.metadata.publisherId,
});
exports.filterQueryRequestMessages = filterQueryRequestMessages;
const filterQueryResponseMessages = (response$, queryNodeAddress$, impossibleRequestIds$, httpResponseRequestId$) => filterSystemMessageBasedOnCriteria({
    systemMessage$: response$,
    queryNodeAddress$,
    impossibleRequestIds$,
    httpResponseRequestId$,
    getRequestPublisherId: (v) => v.message.requestPublisherId,
});
exports.filterQueryResponseMessages = filterQueryResponseMessages;
const filterQueryPropagateMessages = (propagate$, queryNodeAddress$, impossibleRequestIds$, httpResponseRequestId$) => filterSystemMessageBasedOnCriteria({
    systemMessage$: propagate$,
    queryNodeAddress$,
    impossibleRequestIds$,
    httpResponseRequestId$,
    getRequestPublisherId: (v) => v.message.requestPublisherId,
});
exports.filterQueryPropagateMessages = filterQueryPropagateMessages;
//# sourceMappingURL=filterSystemMessageOperations.js.map