"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExpectedPropagatedMessages = exports.getMissingPropagatedMessages = void 0;
const utils_1 = require("@streamr/utils");
/**
 * This function gets the messages that are missing from the primary node.
 * It is necessary because every secondary node must warn the primary node about what messages are missing.
 */
function getMissingPropagatedMessages({ expectedPropagatedMessages, propagates, }) {
    const expectedClone = new Map(expectedPropagatedMessages);
    for (const propagate of propagates) {
        const propagatedMessages = expectedClone.get((0, utils_1.toEthereumAddress)(propagate.metadata.publisherId));
        if (propagatedMessages) {
            for (const [messageId] of propagate.message.payload) {
                propagatedMessages.delete(messageId);
            }
        }
    }
    expectedClone.forEach((propagatedMessages, nodeAddress) => {
        if (propagatedMessages.size === 0) {
            expectedClone.delete(nodeAddress);
        }
    });
    return Array.from(expectedClone.entries());
}
exports.getMissingPropagatedMessages = getMissingPropagatedMessages;
/**
 * every secondary node must warn the primary node about what messages are missing
 * so if they have more messages than primary's, we flag it as expected from this address
 */
function getExpectedPropagatedMessages({ secondaryNodeResponses, primaryNodeResponse, }) {
    const primaryNodeMessages = primaryNodeResponse.message.hashMap;
    const propagatedMap = new Map();
    for (const secondaryNodeResponse of secondaryNodeResponses) {
        const secondaryNodeMessages = secondaryNodeResponse.message.hashMap;
        const propagatedMessages = Array.from(secondaryNodeMessages.keys()).filter((messageId) => !primaryNodeMessages.has(messageId));
        propagatedMap.set((0, utils_1.toEthereumAddress)(secondaryNodeResponse.metadata.publisherId), new Set(propagatedMessages));
    }
    return propagatedMap;
}
exports.getExpectedPropagatedMessages = getExpectedPropagatedMessages;
//# sourceMappingURL=handlePropagatedMessages.js.map