/**
 * @copyright nhcarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */
import { commandList } from "../../commands/_commandList.js";
import type { WeatherCache } from "../../interfaces/weatherCache.js";
import type { Interaction } from "discord.js";

/**
 * Handles the INTERACTION_CREATE event from Discord. Checks if the interaction is
 * an application command, and if there is a matching command, and runs it.
 * @param interaction - The interaction payload from Discord.
 * @param cache - The cache of weather data.
 */
export const onInteraction = async(
  interaction: Interaction,
  cache: WeatherCache,
): Promise<void> => {
  if (!interaction.isChatInputCommand()) {
    return;
  }

  const target = commandList.find(
    (command) => {
      return command.data.name === interaction.commandName;
    },
  );

  if (!target) {
    await interaction.reply(`Command ${interaction.commandName} not found.`);
    return;
  }

  await target.run(interaction, cache);
};
