import { enableRedeem, idMap, namedRedeems, sfxRedeems } from "pointRedeems";
import { Command, sendMessage } from "lib/commandUtils";
import logger from "lib/logger";
import parseCommandArgs from "lib/parseCommandArgs";

export default new Command({
	name: "enableRedeem",
	aliases: ["enableredeem"],
	usertype: "moderator",
	disableable: false,
	execution: async (msg) => {
		const args = parseCommandArgs(msg.messageText);
		if (!args[0]) {
			await sendMessage(
				"Please specify a point redemption to enable",
				msg.messageId,
			);
			return;
		}
		if (args[0] === "sfx" || args[0] === "sound") {
			sfxRedeems.forEach(async (redeem) => {
				const id = idMap.get(redeem.name);
				if (!id) {
					await sendMessage(
						`Failed to find the ID for redeem ${redeem.name}`,
						msg.messageId,
					);
					logger.err(`Failed to find the ID for ${redeem.name} while enabling`);
					return;
				}
				await enableRedeem(redeem, id);
			});
			await sendMessage(
				`Enabled all sound (sfx) channel point redemptions`,
				msg.messageId,
			);
			return;
		}
		const selection = namedRedeems.get(args[0]);
		if (!selection) {
			await sendMessage(
				`Redeem ${args[0]} doesn't exist. The internal names for redeems are here: https://gitlab.com/qwerinope/qweribot#point-redeems`,
				msg.messageId,
			);
			return;
		}
		const id = idMap.get(selection.name);
		await enableRedeem(selection, id!);
		await sendMessage(
			`The ${selection.name} point redeem is now enabled`,
			msg.messageId,
		);
	},
});
