const Dict = require('./dict').Dict;
const FoldDict = require('./fold_dict').FoldDict;

const topic_senders = new Dict(); // key is stream-id, value is Dict
const stream_senders = new Dict(); // key is stream-id, value is Dict

exports.process_message_for_senders = function (message) {
    const stream_id = message.stream_id.toString();
    const topic = util.get_message_topic(message);

    // Process most recent sender to topic
    const topic_dict = topic_senders.get(stream_id) || new FoldDict();
    let sender_message_ids = topic_dict.get(topic) || new Dict();
    let old_message_id = sender_message_ids.get(message.sender_id);

    if (old_message_id === undefined || old_message_id < message.id) {
        sender_message_ids.set(message.sender_id, message.id);
    }

    topic_dict.set(topic, sender_message_ids);
    topic_senders.set(stream_id, topic_dict);

    // Process most recent sender to whole stream
    sender_message_ids = stream_senders.get(stream_id) || new Dict();
    old_message_id = sender_message_ids.get(message.sender_id);

    if (old_message_id === undefined || old_message_id < message.id) {
        sender_message_ids.set(message.sender_id, message.id);
    }

    stream_senders.set(stream_id, sender_message_ids);
};

exports.compare_by_recency = function (user_a, user_b, stream_id, topic) {
    stream_id = stream_id.toString();

    let a_message_id;
    let b_message_id;

    const topic_dict = topic_senders.get(stream_id);
    if (topic !== undefined && topic_dict !== undefined) {
        const sender_message_ids = topic_dict.get(topic);
        if (sender_message_ids !== undefined) {
            b_message_id = sender_message_ids.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
            a_message_id = sender_message_ids.get(user_a.user_id) || Number.NEGATIVE_INFINITY;

            if (a_message_id !== b_message_id) {
                return b_message_id - a_message_id;
            }
        }
    }

    // Check recency for whole stream as tiebreaker
    const stream_dict = stream_senders.get(stream_id);
    if (stream_dict !== undefined) {
        b_message_id = stream_dict.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
        a_message_id = stream_dict.get(user_a.user_id) || Number.NEGATIVE_INFINITY;

        if (a_message_id !== b_message_id) {
            return b_message_id - a_message_id;
        }
    }

    return 0;
};

window.recent_senders = exports;

@@ -1,14 +1,18 @@
 const 
+Fold
 Dict = r
@@ -21,23 +21,32 @@
 uire('./
+fold_
 dict').
+Fold
 Dict;%0Aco
@@ -45,28 +45,27 @@
 Dict;%0Aconst 
-Fold
+Int
 Dict = requi
@@ -66,36 +66,35 @@
  require('./
-fold
+int
 _dict').
 FoldDict;%0A%0Ac
@@ -77,28 +77,27 @@
 /int_dict').
-Fold
+Int
 Dict;%0A%0Aconst
@@ -109,32 +109,35 @@
 c_senders = new 
+Int
 Dict(); // key i
@@ -186,24 +186,27 @@
 nders = new 
+Int
 Dict(); // k
@@ -335,27 +335,16 @@
 tream_id
-.toString()
 ;%0A    co
@@ -553,32 +553,35 @@
 t(topic) %7C%7C new 
+Int
 Dict();%0A    let 
@@ -988,16 +988,19 @@
  %7C%7C new 
+Int
 Dict();%0A
@@ -1343,47 +1343,8 @@
 ) %7B%0A
-    stream_id = stream_id.toString();%0A%0A
     

Replace topic_senders and stream_senders with IntDict.