/*
 * Vencord, a Discord client mod
 * Copyright (c) 2025 Vendicated and contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

import { addMessageAccessory, removeMessageAccessory } from "@api/MessageAccessories";
import { Button } from "@components/Button";
import { Logger } from "@utils/Logger";
import definePlugin from "@utils/types";
import { Message } from "@vencord/discord-types";

function SealDarkFountainDecoration({
    message
}: {
    message: Message;
}) {
    if (/dark (fountain|world)/gi.test(message.content) && /(delta(rune|goon))|prophecy/gi.test(message.content) || /^(hop|get) on deltarune/i.test(message.content.toLowerCase())) {
        return (
            <div>
                <Button type="button" variant="link" onClick={a => open("steam://rungameid/1671210")}>
                    Seal Fountain
                </Button>
            </div>
        );
    }
}

export default definePlugin({
    name: "DeltaruneProphecy",
    description: "The Legend of Light and Dark. More Kromer!",
    authors: [{
        name: "ocbwoy3",
        id: 486147449703104523n
    }],
    patches: [
        {
            find: "#{intl::LOADING_DID_YOU_KNOW}",
            replacement: [
                {
                    match: /"_loadingText".+?(?=(\i)\[.{0,10}\.random)/,
                    replace: "$&$self.mutateQuotes($1),"
                },
                {
                    match: /"_eventLoadingText".+?(?=(\i)\[.{0,10}\.random)/,
                    replace: "$&$self.mutateQuotes($1),",
                    predicate: () => true
                }
            ]
        },
        {
            find: "data-testid\":\"app-spinner\"",
            replacement: {
                match: /children:\s*\w+/,
                replace: `
children:[
  (0,r.jsx)("source",{alt:"",src:"https://files.catbox.moe/a5xk7y.webm"},"webm")
]
`
            }
        },
        {
            find: "let a=JSON.parse('{\"",
            replacement: [
                {
                    match: /Find or start a conversation/gi,
                    replace: "Find or open a Dark Fountain"
                },
                {
                    match: /"Active Now"/gi,
                    replace: "\"Sealing the Fountain right now...\""
                },
                {
                    match: /Discord Orbs Terms/gi,
                    replace: "Kromer Terms"
                },
                {
                    match: /Discord Orbs/gi,
                    replace: "Kromer"
                },
                {
                    match: /Earn through Quests/gi,
                    replace: "Earn by opening Dark Fountains"
                },
                {
                    match: /Explore Orbs Exclusives/gi,
                    replace: "Explore Dark World Exclusives"
                },
                {
                    match: /Orbs/gi,
                    replace: "Kromer"
                },
                {
                    match: /Mutual Servers/gi,
                    replace: "Mutual Dark Worlds"
                },
                {
                    match: /\b(Member)(s)?\b/g,
                    replace: "Darkner$2"
                },
                {
                    match: /"Mutual Friends"/g,
                    replace: "\"Mutual Darkners\""
                }
            ]
        }
    ],

    mutateQuotes(quotes: string[]) {
        try {
            quotes.length = 0;
            quotes.push("THE LEGEND OF THIS WORLD.\n< DELTARUNE. >");
        } catch (e) {
            new Logger("DeltaruneProphecy").error("Failed to mutate quotes", e);
        }
    },

    start() {
        addMessageAccessory(
            "sealFountainButton",
            (props: Record<string, any>) => {
                return (
                    <SealDarkFountainDecoration
                        message={props.message as Message}
                    />
                );
            },
            4
        );
    },

    stop() {
        removeMessageAccessory("sealFountainButton");
    }
});
