import { readdir } from "node:fs/promises";
import type { Command } from "lib/commandUtils";

const commands = new Map<string, Command>(); // This map has all command/item aliases mapped to commands/items (many-to-one)
const specialAliasCommands = new Map<string, Command>(); // This map has all special command/item aliases mapped to commands/items (just like commands map)
const basecommands = new Map<string, Command>(); // This map has all command names mapped to commands (one-to-one) (no items)

const files = await readdir(import.meta.dir);
for (const file of files) {
	if (!file.endsWith(".ts")) continue;
	if (file === import.meta.file) continue;
	const command: Command = await import(
		`${import.meta.dir}/${file.slice(0, -3)}`
	).then((a) => a.default);
	basecommands.set(command.name, command);
	for (const alias of command.aliases) {
		commands.set(alias, command); // Since it's not a primitive type the map is filled with references to the command, not the actual object
	}
	for (const alias of command.specialaliases) {
		specialAliasCommands.set(alias, command);
	}
}

import items, { specialAliasItems } from "items";

for (const [name, item] of Array.from(items)) {
	commands.set(name, item); // As Item is basically just Command but with more parameters, this should work fine
}
for (const [alias, item] of Array.from(specialAliasItems)) {
	specialAliasCommands.set(alias, item);
}

export default commands;
export { specialAliasCommands, basecommands };
