export const event = "messageDelete";

export async function callback(client, message) {
    try {
        if (message.author.id === client.user.id) return;
        if (
            client.noLog.includes(
                `${message.guild.id}/${message.channel.id}/${message.id}`
            )
        )
            return;
        const {
            getAuditLog,
            log,
            NucleusColors,
            entry,
            renderUser,
            renderDelta,
            renderChannel
        } = message.channel.client.logger;
        const auditLog = await getAuditLog(message.guild, "MEMBER_BAN_ADD");
        const audit = auditLog.entries
            .filter((entry) => entry.target.id === message.author.id)
            .first();
        if (audit) {
            if (audit.createdAt - 100 < new Date().getTime()) return;
        }
        message.reference = message.reference || {};
        let content = message.cleanContent;
        content.replace("`", "\\`");
        if (content.length > 256) content = content.substring(0, 253) + "...";
        const attachments =
            message.attachments.size +
            (
                message.content.match(
                    /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
                ) ?? []
            ).length;
        let attachmentJump = "";
        const config = (await client.database.guilds.read(message.guild.id))
            .logging.attachments.saved[message.channel.id + message.id];
        if (config) {
            attachmentJump = ` [[View attachments]](${config})`;
        }
        const data = {
            meta: {
                type: "messageDelete",
                displayName: "Message Deleted",
                calculateType: "messageDelete",
                color: NucleusColors.red,
                emoji: "MESSAGE.DELETE",
                timestamp: new Date().getTime()
            },
            separate: {
                start: content
                    ? `**Message:**\n\`\`\`${content}\`\`\``
                    : "**Message:** *Message had no content*"
            },
            list: {
                messageId: entry(message.id, `\`${message.id}\``),
                sentBy: entry(message.author.id, renderUser(message.author)),
                sentIn: entry(
                    message.channel.id,
                    renderChannel(message.channel)
                ),
                deleted: entry(
                    new Date().getTime(),
                    renderDelta(new Date().getTime())
                ),
                mentions: message.mentions.users.size,
                attachments: entry(attachments, attachments + attachmentJump),
                repliedTo: entry(
                    message.reference.messageId || null,
                    message.reference.messageId
                        ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})`
                        : "None"
                )
            },
            hidden: {
                guild: message.channel.guild.id
            }
        };
        log(data);
    } catch (e) {
        console.log(e);
    }
}
