import type { NucleusClient } from "../utils/client.js";
import { AuditLogEvent, GuildAuditLogsEntry, Sticker } from "discord.js";

export const event = "stickerUpdate";

export async function callback(client: NucleusClient, oldSticker: Sticker, newSticker: Sticker) {
    const { getAuditLog, isLogging, log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
    if (!await isLogging(newSticker.guild!.id, "stickerUpdate")) return;
    if (oldSticker.name === newSticker.name) return;
    const auditLog = (await getAuditLog(newSticker.guild!, AuditLogEvent.StickerUpdate))
        .filter((entry: GuildAuditLogsEntry) => (entry.target as Sticker)!.id === newSticker.id)[0]!;
    if (auditLog.executor!.id === client.user!.id) return;

    const changes = {
        stickerId: entry(newSticker.id, `\`${newSticker.id}\``),
        edited: entry(newSticker.createdTimestamp, renderDelta(newSticker.createdTimestamp)),
        editedBy: entry(auditLog.executor!.id, renderUser((await newSticker.guild!.members.fetch(auditLog.executor!.id)).user)),
        name: entry([oldSticker.name, newSticker.name], `\`:${oldSticker.name}:\` -> \`:${newSticker.name}:\``)
    };
    const data = {
        meta: {
            type: "stickerUpdate",
            displayName: "Sticker Edited",
            calculateType: "stickerUpdate",
            color: NucleusColors.yellow,
            emoji: "GUILD.EMOJI.EDIT",
            timestamp: auditLog.createdTimestamp
        },
        list: changes,
        hidden: {
            guild: newSticker.guild!.id
        }
    };
    log(data);
}
