import { getWordHardMute } from "./check-word-mute.js";
import { Cache } from "./cache.js";
import { unique } from "../prelude/array.js";
import config from "../config/index.js";
import { UserProfiles } from "../models/index.js";
const filteredNoteCache = new Cache("filteredNote", config.wordMuteCache?.ttlSeconds ?? 60 * 60 * 24);
const mutedWordsCache = new Cache("mutedWords", 60 * 5);
export async function isFiltered(note, user, profile) {
    if (!user) return false;
    if (profile === undefined) profile = {
        mutedWords: await mutedWordsCache.fetch(user.id, async ()=>UserProfiles.findOneBy({
                userId: user.id
            }).then((p)=>p?.mutedWords ?? []))
    };
    if (!profile || profile.mutedWords.length < 1) return false;
    const ts = note.updatedAt ?? note.createdAt;
    const identifier = (typeof ts === "string" ? new Date(ts) : ts)?.getTime() ?? '0';
    return filteredNoteCache.fetch(`${note.id}:${identifier}:${user.id}`, ()=>getWordHardMute(note, user, unique(profile.mutedWords)));
}
