import { activeRedeems } from "pointRedeems";
import { eventSub } from "index";
import { sendMessage } from "lib/commandUtils";
import logger from "lib/logger";
import { streamerId } from "main";
import User from "user";

eventSub.onChannelRedemptionAdd(streamerId, async (msg) => {
	const selection = activeRedeems.get(msg.rewardId);
	if (!selection) {
		logger.warn(`Can't find the ${msg.rewardTitle} redeem`);
		return;
	}
	const user = await User.initUsername(msg.userName);
	try {
		await selection.execute(msg, user!);
	} catch (err) {
		await sendMessage(
			`[ERROR]: Something went wrong with ${user?.displayName}'s redeem!`,
		);
		logger.err(err as string);
	}
});
