import getloot from "commands/getloot";
import items from "items";
import { Command, sendMessage } from "lib/commandUtils";
import { isInvuln, removeInvuln } from "lib/invuln";
import { redis } from "lib/redis";
import { streamerUsers } from "main";

export default new Command({
	name: "use",
	aliases: ["use"],
	usertype: "chatter",
	disableable: false,
	specialaliases: ["i"],
	execution: async (msg, user, specialargs) => {
		const messagequery = msg.messageText.trim().split(" ").slice(1); // This selects the item, so on "i blast mrockstar20" it would pick ["blast", "mrockstar20"]
		const silent = msg.messageText.toLowerCase().startsWith("i");
		if (!messagequery[0]) {
			if (!silent) {
				await sendMessage(
					"Please specify an item you would like to use",
					msg.messageId,
				);
			}
			return;
		}
		const selection = items.get(messagequery[0].toLowerCase());
		if (messagequery[0].toLowerCase() === "lootbox") {
			await getloot.execute(msg, user);
			return;
		}
		if (!selection) {
			if (!silent) {
				await sendMessage(`'${messagequery[0]}' is not an item`, msg.messageId);
			}
			return;
		}
		if (await redis.sismember("disabledcommands", selection.name)) {
			await sendMessage(
				`The ${selection.prettyName} item is disabled`,
				msg.messageId,
			);
			return;
		}
		if (
			(await isInvuln(msg.chatterId)) &&
			!streamerUsers.includes(msg.chatterId)
		) {
			await sendMessage(
				`You're no longer an invuln because you used an item.`,
				msg.messageId,
			);
			await removeInvuln(msg.chatterId);
		}
		await selection.execute(msg, user, specialargs);
	},
});
