import type { EventSubChannelChatMessageEvent } from "@twurple/eventsub-base";
import type User from "user";

export type cheers =
	| "execute"
	| "grenade"
	| "tnt"
	| "timeout"
	| "superloot"
	| "realsilverbullet";

type cheerOptions = {
	name: cheers;
	amount: number;
	isItem: boolean;
	execute: (
		msg: EventSubChannelChatMessageEvent,
		sender: User,
	) => Promise<void>;
};

export class Cheer {
	public readonly name: cheers;
	public readonly amount: number;
	public readonly execute: (
		msg: EventSubChannelChatMessageEvent,
		sender: User,
	) => Promise<void>;
	public readonly isItem: boolean;
	constructor(options: cheerOptions) {
		this.name = options.name;
		this.amount = options.amount;
		this.execute = options.execute;
		this.isItem = options.isItem;
	}
}

import { readdir } from "node:fs/promises";

const cheers = new Map<number, Cheer>();
const namedcheers = new Map<string, Cheer>();

const files = await readdir(import.meta.dir);
for (const file of files) {
	if (!file.endsWith(".ts")) continue;
	if (file === import.meta.file) continue;
	const cheer: Cheer = await import(
		`${import.meta.dir}/${file.slice(0, -3)}`
	).then((a) => a.default);
	cheers.set(cheer.amount, cheer);
	namedcheers.set(cheer.name, cheer);
}

export default cheers;
export { namedcheers };

import { getUserRecord } from "db/dbUser";
import { changeItemCount, type items } from "items";
import { sendMessage } from "lib/commandUtils";

export async function handleNoTarget(
	msg: EventSubChannelChatMessageEvent,
	user: User,
	itemname: items,
	silent = true,
) {
	if (await user.itemLock()) {
		await sendMessage(
			`Cannot give ${user.displayName} a ${itemname} (itemlock)`,
			msg.messageId,
		);
		return;
	}
	await user.setLock();
	const userRecord = await getUserRecord(user);
	if (!silent)
		await sendMessage(
			`No (valid) target specified. You got a ${itemname}!`,
			msg.messageId,
		);
	await changeItemCount(user, userRecord, itemname, 1);
	await user.clearLock();
}
