import { ensureNamedValueTag } from "../helpers/tag.js";
import { createGroupHTagFromGroupPointer } from "../helpers/groups.js";
import { includeSingletonTag } from "./tags.js";
/** Sets the "h" tag for NIP-29 group messages or other events */
export function setGroupPointer(group) {
    return includeSingletonTag(createGroupHTagFromGroupPointer(group), true);
}
/** Adds "previous" tags for group messages */
export function addPreviousRefs(previous, count = 6) {
    return (draft) => {
        let tags = Array.from(draft.tags);
        // sort previous events by date and limit to 50
        const sorted = previous.sort((a, b) => b.created_at - a.created_at).slice(0, 50);
        for (let i = 0; i < count; i++) {
            const index = Math.round(Math.random() * (sorted.length - 1));
            const event = sorted.splice(index, 1)[0];
            if (event)
                tags = ensureNamedValueTag(tags, ["previous", event.id.slice(0, 8)]);
        }
        return { ...draft, tags };
    };
}
