import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import type { Message } from "discord-types/general";

const settings = definePluginSettings({}).withPrivateSettings<{
    users?: Record<string, boolean>;
}>();

export default definePlugin({
    name: "RememberReplyMention",
    description: "Remember whether to ping on reply for each user",
    authors: [{ name: "slonkazoid", id: 276363003270791168n }],
    settings,

    shouldMention(message: Message, isHoldingShift: boolean) {
        if (settings.store.users === undefined) settings.store.users = {};
        let preference = settings.store.users[message.author.id];
        if (typeof preference !== "boolean") {
            preference = !isHoldingShift;
            settings.store.users[message.author.id] = preference;
        }
        console.log(message.author.id, preference);
        return preference;
    },

    togglePing(
        {
            channelId,
            shouldMention,
        }: {
            channelId: string;
            shouldMention: boolean;
        },
        replies: Record<string, { message: Message }>
    ) {
        console.log(arguments);
        let reply = replies[channelId];
        if (reply === undefined) return;
        let id = reply.message.author.id;
        if (settings.store.users === undefined) settings.store.users = {};
        console.log("updated state for", id, shouldMention);
        settings.store.users[id] = shouldMention;
    },

    patches: [
        {
            find: ',"Message")}function',
            replacement: {
                match: /:(\i),shouldMention:!(\i)\.shiftKey/,
                replace:
                    ":$1,shouldMention:$self.shouldMention($1,$2.shiftKey)",
            },
        },
        {
            find: "SET_PENDING_REPLY_SHOULD_MENTION:",
            replacement: {
                match: /(let{channelId:\i,shouldMention:\i}=(\i);\i in (\i)&&)/,
                replace: "$self.togglePing($2, $3);$1",
            },
        },
    ],
});
