/**
 * This code was AUTOGENERATED using the Codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun Codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */

import {
  isProgramError,
  type Address,
  type SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM,
  type SolanaError,
} from "@solana/kit";
import { TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS } from "../programs";

/** InvalidAuthority: Invalid authority */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_AUTHORITY = 0x1770; // 6000
/** InvalidTokenMessengerState: Invalid token messenger state */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MESSENGER_STATE = 0x1771; // 6001
/** InvalidTokenMessenger: Invalid token messenger */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MESSENGER = 0x1772; // 6002
/** InvalidOwner: Invalid owner */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_OWNER = 0x1773; // 6003
/** MalformedMessage: Malformed message */
export const TOKEN_MESSENGER_MINTER_ERROR__MALFORMED_MESSAGE = 0x1774; // 6004
/** InvalidMessageBodyVersion: Invalid message body version */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_MESSAGE_BODY_VERSION = 0x1775; // 6005
/** InvalidAmount: Invalid amount */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_AMOUNT = 0x1776; // 6006
/** InvalidDestinationDomain: Invalid destination domain */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_DESTINATION_DOMAIN = 0x1777; // 6007
/** InvalidDestinationCaller: Invalid destination caller */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_DESTINATION_CALLER = 0x1778; // 6008
/** InvalidMintRecipient: Invalid mint recipient */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_MINT_RECIPIENT = 0x1779; // 6009
/** InvalidSender: Invalid sender */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_SENDER = 0x177a; // 6010
/** InvalidTokenPair: Invalid token pair */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_PAIR = 0x177b; // 6011
/** InvalidTokenMint: Invalid token mint */
export const TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MINT = 0x177c; // 6012

export type TokenMessengerMinterError =
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_AMOUNT
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_AUTHORITY
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_DESTINATION_CALLER
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_DESTINATION_DOMAIN
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_MESSAGE_BODY_VERSION
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_MINT_RECIPIENT
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_OWNER
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_SENDER
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MESSENGER
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MESSENGER_STATE
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MINT
  | typeof TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_PAIR
  | typeof TOKEN_MESSENGER_MINTER_ERROR__MALFORMED_MESSAGE;

let tokenMessengerMinterErrorMessages:
  | Record<TokenMessengerMinterError, string>
  | undefined;
if (process.env.NODE_ENV !== "production") {
  tokenMessengerMinterErrorMessages = {
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_AMOUNT]: `Invalid amount`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_AUTHORITY]: `Invalid authority`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_DESTINATION_CALLER]: `Invalid destination caller`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_DESTINATION_DOMAIN]: `Invalid destination domain`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_MESSAGE_BODY_VERSION]: `Invalid message body version`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_MINT_RECIPIENT]: `Invalid mint recipient`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_OWNER]: `Invalid owner`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_SENDER]: `Invalid sender`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MESSENGER]: `Invalid token messenger`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MESSENGER_STATE]: `Invalid token messenger state`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_MINT]: `Invalid token mint`,
    [TOKEN_MESSENGER_MINTER_ERROR__INVALID_TOKEN_PAIR]: `Invalid token pair`,
    [TOKEN_MESSENGER_MINTER_ERROR__MALFORMED_MESSAGE]: `Malformed message`,
  };
}

export function getTokenMessengerMinterErrorMessage(
  code: TokenMessengerMinterError,
): string {
  if (process.env.NODE_ENV !== "production") {
    return (
      tokenMessengerMinterErrorMessages as Record<
        TokenMessengerMinterError,
        string
      >
    )[code];
  }

  return "Error message not available in production bundles.";
}

export function isTokenMessengerMinterError<
  TProgramErrorCode extends TokenMessengerMinterError,
>(
  error: unknown,
  transactionMessage: {
    instructions: Record<number, { programAddress: Address }>;
  },
  code?: TProgramErrorCode,
): error is SolanaError<typeof SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM> &
  Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> {
  return isProgramError<TProgramErrorCode>(
    error,
    transactionMessage,
    TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
    code,
  );
}
