import {
  Interaction,
  CommandInteraction,
  Constants,
  ModalSubmitInteraction,
  ComponentInteraction,
  AutocompleteInteraction,
  PingInteraction,
} from '@projectdysnomia/dysnomia';
import type { ExecutableInteraction } from './command-handler';

export function isApplicationCommand(interaction: Interaction): interaction is CommandInteraction {
  return interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND;
}

export function isModalSubmit(interaction: Interaction): interaction is ModalSubmitInteraction {
  return interaction.type === Constants.InteractionTypes.MODAL_SUBMIT;
}

export function isMessageComponent(interaction: Interaction): interaction is ComponentInteraction {
  return interaction.type === Constants.InteractionTypes.MESSAGE_COMPONENT;
}

export function isAutocomplete(interaction: Interaction): interaction is AutocompleteInteraction {
  return interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE;
}

export function isPing(interaction: Interaction): interaction is PingInteraction {
  return interaction.type === Constants.InteractionTypes.PING;
}

export function commandHasName(interaction: Interaction, name: string): boolean {
  return isApplicationCommand(interaction) && interaction.data.name === name;
}

export function commandHasIdPrefix(interaction: Interaction, prefix: string): boolean {
  return (isModalSubmit(interaction) || isMessageComponent(interaction)) && interaction.data.custom_id.startsWith(prefix);
}

export function getCommandName(interaction: ExecutableInteraction): string | undefined {
  if (isApplicationCommand(interaction) || isAutocomplete(interaction)) {
    return interaction.data.name;
  }
  return undefined;
}
