import { kinds } from "nostr-tools";
import { getLegacyMessageCorrespondent, getLegacyMessageParent } from "../helpers/legacy-messages.js";
import { map } from "rxjs";
import { getConversationIdentifierFromMessage, getConversationParticipants } from "../helpers/messages.js";
import { hasNameValueTag } from "../helpers/event-tags.js";
/** A model that returns all legacy message groups (1-1) that a pubkey is participating in */
export function LegacyMessagesGroups(self) {
    return (store) => store.timeline({ kinds: [kinds.EncryptedDirectMessage], "#p": [self] }).pipe(map((messages) => {
        const groups = {};
        for (const message of messages) {
            const id = getConversationIdentifierFromMessage(message);
            if (!groups[id] || groups[id].created_at < message.created_at)
                groups[id] = message;
        }
        return Object.values(groups).map((message) => ({
            id: getConversationIdentifierFromMessage(message),
            participants: getConversationParticipants(message),
            lastMessage: message,
        }));
    }));
}
/** Returns all legacy direct messages in a group */
export function LegacyMessagesGroup(self, correspondent) {
    return (store) => store.timeline([
        {
            kinds: [kinds.EncryptedDirectMessage],
            "#p": [self],
            authors: [correspondent],
        },
        {
            kinds: [kinds.EncryptedDirectMessage],
            "#p": [correspondent],
            authors: [self],
        },
    ]);
}
/** Returns an array of legacy messages that have replies */
export function LegacyMessageThreads(self, correspondent) {
    return (store) => store.model(LegacyMessagesGroup, self, correspondent).pipe(map((messages) => messages.filter((message) => 
    // Only select messages that are not replies
    !getLegacyMessageParent(message) &&
        // Check if message has any replies
        messages.some((m) => hasNameValueTag(m, "e", message.id)))));
}
/** Returns all the legacy direct messages that are replies to a given message */
export function LegacyMessageReplies(self, message) {
    const correspondent = getLegacyMessageCorrespondent(message, self);
    if (!correspondent)
        throw new Error("Legacy message has no correspondent");
    return (store) => store.timeline([
        {
            kinds: [kinds.EncryptedDirectMessage],
            "#p": [self],
            authors: [correspondent],
            "#e": [message.id],
        },
        {
            kinds: [kinds.EncryptedDirectMessage],
            "#p": [correspondent],
            authors: [self],
            "#e": [message.id],
        },
    ]);
}
