export const event = 'messageUpdate'

export async function callback(client, oldMessage, newMessage) {
    try {
        if (newMessage.author.id == client.user.id) return;
        const { log, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = newMessage.channel.client.logger
        newMessage.reference = newMessage.reference || {}
        let newContent = newMessage.cleanContent.replaceAll("`", "‘")
        let oldContent = oldMessage.cleanContent.replaceAll("`", "‘")
        let attachmentJump = "";
        let config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[newMessage.channel.id + newMessage.id];
        if (config) { attachmentJump = ` [[View attachments]](${config})` }
        if (newContent == oldContent) {
            if (!oldMessage.flags.has("CROSSPOSTED") && newMessage.flags.has("CROSSPOSTED")) {
                let data = {
                    meta: {
                        type: 'messageAnnounce',
                        displayName: 'Message Published',
                        calculateType: 'messageAnnounce',
                        color: NucleusColors.yellow,
                        emoji: 'MESSAGE.CREATE',
                        timestamp: newMessage.editedTimestamp
                    },
                    separate: {
                        end: `[[Jump to message]](${newMessage.url})`
                    },
                    list: {
                        messageId: entry(newMessage.id, `\`${newMessage.id}\``),
                        sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
                        sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel)),
                        sent: entry(new Date(newMessage.createdTimestamp), renderDelta(new Date(newMessage.createdTimestamp))),
                        published: entry(new Date(newMessage.editedTimestamp), renderDelta(new Date(newMessage.editedTimestamp))),
                        mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
                        attachments: entry(
                            renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
                            renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
                        )
                    },
                    hidden: {
                        guild: newMessage.channel.guild.id
                    }
                }
                return log(data);
            }
        };
        if (!newMessage.editedTimestamp) { return }
        if (newContent.length > 256) newContent = newContent.substring(0, 253) + '...'
        if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + '...'
        let data = {
            meta: {
                type: 'messageUpdate',
                displayName: 'Message Edited',
                calculateType: 'messageUpdate',
                color: NucleusColors.yellow,
                emoji: 'MESSAGE.EDIT',
                timestamp: newMessage.editedTimestamp
            },
            separate: {
                start: (oldContent ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n` : '**Before:** *Message had no content*\n') +
                    (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : '**After:** *Message had no content*'),
                end: `[[Jump to message]](${newMessage.url})`
            },
            list: {
                messageId: entry(newMessage.id, `\`${newMessage.id}\``),
                sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
                sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel)),
                sent: entry(new Date(newMessage.createdTimestamp), renderDelta(new Date(newMessage.createdTimestamp))),
                edited: entry(new Date(newMessage.editedTimestamp), renderDelta(new Date(newMessage.editedTimestamp))),
                mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
                attachments: entry(
                    renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
                    renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
                ),
                repliedTo: entry(
                    newMessage.reference.messageId || null,
                    newMessage.reference.messageId ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${newMessage.reference.messageId})` : "None"
                )
            },
            hidden: {
                guild: newMessage.channel.guild.id
            }
        }
        log(data);
    } catch {}
}
