/**
 * 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 { MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS } from "../programs";

/** InvalidAuthority: Invalid authority */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_AUTHORITY = 0x1770; // 6000
/** ProgramPaused: Instruction is not allowed at this time */
export const MESSAGE_TRANSMITTER_V2_ERROR__PROGRAM_PAUSED = 0x1771; // 6001
/** InvalidMessageTransmitterState: Invalid message transmitter state */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_TRANSMITTER_STATE = 0x1772; // 6002
/** InvalidSignatureThreshold: Invalid signature threshold */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_THRESHOLD = 0x1773; // 6003
/** SignatureThresholdAlreadySet: Signature threshold already set */
export const MESSAGE_TRANSMITTER_V2_ERROR__SIGNATURE_THRESHOLD_ALREADY_SET = 0x1774; // 6004
/** InvalidOwner: Invalid owner */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_OWNER = 0x1775; // 6005
/** InvalidPauser: Invalid pauser */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_PAUSER = 0x1776; // 6006
/** InvalidAttesterManager: Invalid attester manager */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER_MANAGER = 0x1777; // 6007
/** InvalidAttester: Invalid attester */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER = 0x1778; // 6008
/** AttesterAlreadyEnabled: Attester already enabled */
export const MESSAGE_TRANSMITTER_V2_ERROR__ATTESTER_ALREADY_ENABLED = 0x1779; // 6009
/** TooFewEnabledAttesters: Too few enabled attesters */
export const MESSAGE_TRANSMITTER_V2_ERROR__TOO_FEW_ENABLED_ATTESTERS = 0x177a; // 6010
/** SignatureThresholdTooLow: Signature threshold is too low */
export const MESSAGE_TRANSMITTER_V2_ERROR__SIGNATURE_THRESHOLD_TOO_LOW = 0x177b; // 6011
/** AttesterAlreadyDisabled: Attester already disabled */
export const MESSAGE_TRANSMITTER_V2_ERROR__ATTESTER_ALREADY_DISABLED = 0x177c; // 6012
/** MessageBodyLimitExceeded: Message body exceeds max size */
export const MESSAGE_TRANSMITTER_V2_ERROR__MESSAGE_BODY_LIMIT_EXCEEDED = 0x177d; // 6013
/** InvalidDestinationCaller: Invalid destination caller */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_CALLER = 0x177e; // 6014
/** InvalidRecipient: Invalid message recipient */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_RECIPIENT = 0x177f; // 6015
/** SenderNotPermitted: Sender is not permitted */
export const MESSAGE_TRANSMITTER_V2_ERROR__SENDER_NOT_PERMITTED = 0x1780; // 6016
/** InvalidSourceDomain: Invalid source domain */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SOURCE_DOMAIN = 0x1781; // 6017
/** InvalidDestinationDomain: Invalid destination domain */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_DOMAIN = 0x1782; // 6018
/** InvalidMessageVersion: Invalid message version */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_VERSION = 0x1783; // 6019
/** InvalidUsedNoncesAccount: Invalid used nonces account */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_USED_NONCES_ACCOUNT = 0x1784; // 6020
/** InvalidRecipientProgram: Invalid recipient program */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_RECIPIENT_PROGRAM = 0x1785; // 6021
/** InvalidNonce: Invalid nonce */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_NONCE = 0x1786; // 6022
/** NonceAlreadyUsed: Nonce already used */
export const MESSAGE_TRANSMITTER_V2_ERROR__NONCE_ALREADY_USED = 0x1787; // 6023
/** MessageTooShort: Message is too short */
export const MESSAGE_TRANSMITTER_V2_ERROR__MESSAGE_TOO_SHORT = 0x1788; // 6024
/** MalformedMessage: Malformed message */
export const MESSAGE_TRANSMITTER_V2_ERROR__MALFORMED_MESSAGE = 0x1789; // 6025
/** InvalidSignatureOrderOrDupe: Invalid signature order or dupe */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_ORDER_OR_DUPE = 0x178a; // 6026
/** InvalidAttesterSignature: Invalid attester signature */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER_SIGNATURE = 0x178b; // 6027
/** InvalidAttestationLength: Invalid attestation length */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTATION_LENGTH = 0x178c; // 6028
/** InvalidSignatureRecoveryId: Invalid signature recovery ID */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_RECOVERY_ID = 0x178d; // 6029
/** InvalidSignatureSValue: Invalid signature S value */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_S_VALUE = 0x178e; // 6030
/** InvalidMessageHash: Invalid message hash */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_HASH = 0x178f; // 6031
/** InvalidDestinationMessage: Invalid destination message */
export const MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_MESSAGE = 0x1790; // 6032
/** EventAccountWindowNotExpired: Event account window not expired */
export const MESSAGE_TRANSMITTER_V2_ERROR__EVENT_ACCOUNT_WINDOW_NOT_EXPIRED = 0x1791; // 6033
/** DestinationDomainIsLocalDomain: Destination domain is local domain */
export const MESSAGE_TRANSMITTER_V2_ERROR__DESTINATION_DOMAIN_IS_LOCAL_DOMAIN = 0x1792; // 6034

export type MessageTransmitterV2Error =
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__ATTESTER_ALREADY_DISABLED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__ATTESTER_ALREADY_ENABLED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__DESTINATION_DOMAIN_IS_LOCAL_DOMAIN
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__EVENT_ACCOUNT_WINDOW_NOT_EXPIRED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTATION_LENGTH
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER_MANAGER
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER_SIGNATURE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_AUTHORITY
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_CALLER
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_DOMAIN
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_MESSAGE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_HASH
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_TRANSMITTER_STATE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_VERSION
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_NONCE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_OWNER
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_PAUSER
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_RECIPIENT
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_RECIPIENT_PROGRAM
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_ORDER_OR_DUPE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_RECOVERY_ID
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_S_VALUE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_THRESHOLD
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SOURCE_DOMAIN
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__INVALID_USED_NONCES_ACCOUNT
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__MALFORMED_MESSAGE
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__MESSAGE_BODY_LIMIT_EXCEEDED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__MESSAGE_TOO_SHORT
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__NONCE_ALREADY_USED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__PROGRAM_PAUSED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__SENDER_NOT_PERMITTED
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__SIGNATURE_THRESHOLD_ALREADY_SET
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__SIGNATURE_THRESHOLD_TOO_LOW
  | typeof MESSAGE_TRANSMITTER_V2_ERROR__TOO_FEW_ENABLED_ATTESTERS;

let messageTransmitterV2ErrorMessages:
  | Record<MessageTransmitterV2Error, string>
  | undefined;
if (process.env.NODE_ENV !== "production") {
  messageTransmitterV2ErrorMessages = {
    [MESSAGE_TRANSMITTER_V2_ERROR__ATTESTER_ALREADY_DISABLED]: `Attester already disabled`,
    [MESSAGE_TRANSMITTER_V2_ERROR__ATTESTER_ALREADY_ENABLED]: `Attester already enabled`,
    [MESSAGE_TRANSMITTER_V2_ERROR__DESTINATION_DOMAIN_IS_LOCAL_DOMAIN]: `Destination domain is local domain`,
    [MESSAGE_TRANSMITTER_V2_ERROR__EVENT_ACCOUNT_WINDOW_NOT_EXPIRED]: `Event account window not expired`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTATION_LENGTH]: `Invalid attestation length`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER]: `Invalid attester`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER_MANAGER]: `Invalid attester manager`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_ATTESTER_SIGNATURE]: `Invalid attester signature`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_AUTHORITY]: `Invalid authority`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_CALLER]: `Invalid destination caller`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_DOMAIN]: `Invalid destination domain`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_DESTINATION_MESSAGE]: `Invalid destination message`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_HASH]: `Invalid message hash`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_TRANSMITTER_STATE]: `Invalid message transmitter state`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_MESSAGE_VERSION]: `Invalid message version`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_NONCE]: `Invalid nonce`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_OWNER]: `Invalid owner`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_PAUSER]: `Invalid pauser`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_RECIPIENT]: `Invalid message recipient`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_RECIPIENT_PROGRAM]: `Invalid recipient program`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_ORDER_OR_DUPE]: `Invalid signature order or dupe`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_RECOVERY_ID]: `Invalid signature recovery ID`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_S_VALUE]: `Invalid signature S value`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SIGNATURE_THRESHOLD]: `Invalid signature threshold`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_SOURCE_DOMAIN]: `Invalid source domain`,
    [MESSAGE_TRANSMITTER_V2_ERROR__INVALID_USED_NONCES_ACCOUNT]: `Invalid used nonces account`,
    [MESSAGE_TRANSMITTER_V2_ERROR__MALFORMED_MESSAGE]: `Malformed message`,
    [MESSAGE_TRANSMITTER_V2_ERROR__MESSAGE_BODY_LIMIT_EXCEEDED]: `Message body exceeds max size`,
    [MESSAGE_TRANSMITTER_V2_ERROR__MESSAGE_TOO_SHORT]: `Message is too short`,
    [MESSAGE_TRANSMITTER_V2_ERROR__NONCE_ALREADY_USED]: `Nonce already used`,
    [MESSAGE_TRANSMITTER_V2_ERROR__PROGRAM_PAUSED]: `Instruction is not allowed at this time`,
    [MESSAGE_TRANSMITTER_V2_ERROR__SENDER_NOT_PERMITTED]: `Sender is not permitted`,
    [MESSAGE_TRANSMITTER_V2_ERROR__SIGNATURE_THRESHOLD_ALREADY_SET]: `Signature threshold already set`,
    [MESSAGE_TRANSMITTER_V2_ERROR__SIGNATURE_THRESHOLD_TOO_LOW]: `Signature threshold is too low`,
    [MESSAGE_TRANSMITTER_V2_ERROR__TOO_FEW_ENABLED_ATTESTERS]: `Too few enabled attesters`,
  };
}

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

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

export function isMessageTransmitterV2Error<
  TProgramErrorCode extends MessageTransmitterV2Error,
>(
  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,
    MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
    code,
  );
}
