import { callback as roleMenu } from "../actions/roleMenu.js";
import verify from "../reflex/verify.js";
import create from "../actions/tickets/create.js";
import close from "../actions/tickets/delete.js";
import createTranscript from "../premium/createTranscript.js";
import Fuse from "fuse.js";
import { autocomplete as tagAutocomplete } from "../commands/tag.js";

export const event = "interactionCreate";

function getAutocomplete(typed: string, options: string[]): object[] {
    options = options.filter((option) => option.length <= 100); // thanks discord. 6000 character limit on slash command inputs but only 100 for autocomplete.
    if (!typed)
        return options
            .slice(0, 25)
            .sort()
            .map((option) => ({ name: option, value: option }));
    const fuse = new Fuse(options, {
        useExtendedSearch: true,
        findAllMatches: true,
        minMatchCharLength: 0
    }).search(typed);
    return fuse.slice(0, 25).map((option) => ({ name: option.item, value: option.item }));
}

function generateStatsChannelAutocomplete(typed) {
    const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"];
    const autocompletions = [];
    const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
    if (beforeLastOpenBracket !== null) {
        for (const replacement of validReplacements) {
            autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`);
        }
    } else {
        for (const replacement of validReplacements) {
            autocompletions.push(`${typed} {${replacement}}`);
        }
    }
    return getAutocomplete(typed, autocompletions);
}
function generateWelcomeMessageAutocomplete(typed) {
    const validReplacements = [
        "serverName",
        "memberCount",
        "memberCount:bots",
        "memberCount:humans",
        "member:mention",
        "member:name"
    ];
    const autocompletions = [];
    const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
    if (beforeLastOpenBracket !== null) {
        for (const replacement of validReplacements) {
            autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`);
        }
    } else {
        for (const replacement of validReplacements) {
            autocompletions.push(`${typed} {${replacement}}`);
        }
    }
    return getAutocomplete(typed, autocompletions);
}

async function interactionCreate(interaction) {
    if (interaction.componentType === "BUTTON") {
        switch (interaction.customId) {
            case "rolemenu": {
                return await roleMenu(interaction);
            }
            case "verifybutton": {
                return verify(interaction);
            }
            case "createticket": {
                return create(interaction);
            }
            case "closeticket": {
                return close(interaction);
            }
            case "createtranscript": {
                return createTranscript(interaction);
            }
        }
    } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") {
        switch (
            `${interaction.commandName} ${interaction.options.getSubcommandGroup(
                false
            )} ${interaction.options.getSubcommand(false)}`
        ) {
            case "tag null null": {
                return interaction.respond(
                    getAutocomplete(interaction.options.getString("tag"), await tagAutocomplete(interaction))
                );
            }
            case "settings null stats": {
                return interaction.respond(generateStatsChannelAutocomplete(interaction.options.getString("name")));
            }
            case "settings null welcome": {
                return interaction.respond(
                    generateWelcomeMessageAutocomplete(interaction.options.getString("message"))
                );
            }
        }
    }
}

export async function callback(client, interaction) {
    await interactionCreate(interaction);
}
