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

/** NotOwner: Only the owner can call this function! */
export const SVM_SPOKE_ERROR__NOT_OWNER = 0x1770; // 6000
/** InvalidRelayHash: Invalid relay hash! */
export const SVM_SPOKE_ERROR__INVALID_RELAY_HASH = 0x1771; // 6001
/** CanOnlyCloseFillStatusPdaIfFillDeadlinePassed: The fill deadline has not passed! */
export const SVM_SPOKE_ERROR__CAN_ONLY_CLOSE_FILL_STATUS_PDA_IF_FILL_DEADLINE_PASSED = 0x1772; // 6002
/** NotRelayer: The caller is not the relayer! */
export const SVM_SPOKE_ERROR__NOT_RELAYER = 0x1773; // 6003
/** CannotSetCurrentTime: Cannot set time if not in test mode! */
export const SVM_SPOKE_ERROR__CANNOT_SET_CURRENT_TIME = 0x1774; // 6004
/** InvalidRemoteDomain: Invalid remote domain! */
export const SVM_SPOKE_ERROR__INVALID_REMOTE_DOMAIN = 0x1775; // 6005
/** InvalidRemoteSender: Invalid remote sender! */
export const SVM_SPOKE_ERROR__INVALID_REMOTE_SENDER = 0x1776; // 6006
/** InvalidMint: Invalid mint! */
export const SVM_SPOKE_ERROR__INVALID_MINT = 0x1777; // 6007
/** ExceededPendingBridgeAmount: Exceeded pending bridge amount to HubPool! */
export const SVM_SPOKE_ERROR__EXCEEDED_PENDING_BRIDGE_AMOUNT = 0x1778; // 6008
/** ParamsWriteOverflow: Overflow writing to parameters account! */
export const SVM_SPOKE_ERROR__PARAMS_WRITE_OVERFLOW = 0x1779; // 6009
/** InvalidRefund: Invalid refund address! */
export const SVM_SPOKE_ERROR__INVALID_REFUND = 0x177a; // 6010
/** ZeroRefundClaim: Zero relayer refund claim! */
export const SVM_SPOKE_ERROR__ZERO_REFUND_CLAIM = 0x177b; // 6011
/** NonZeroRefundClaim: Cannot close non-zero relayer refund claim! */
export const SVM_SPOKE_ERROR__NON_ZERO_REFUND_CLAIM = 0x177c; // 6012
/** InvalidClaimInitializer: Invalid claim initializer! */
export const SVM_SPOKE_ERROR__INVALID_CLAIM_INITIALIZER = 0x177d; // 6013
/** InvalidRefundTokenAccount: Invalid refund token account! */
export const SVM_SPOKE_ERROR__INVALID_REFUND_TOKEN_ACCOUNT = 0x177e; // 6014
/** InvalidProductionSeed: Seed must be 0 in production! */
export const SVM_SPOKE_ERROR__INVALID_PRODUCTION_SEED = 0x177f; // 6015
/** InvalidATACreationAccounts: Invalid remaining accounts for ATA creation! */
export const SVM_SPOKE_ERROR__INVALID_A_T_A_CREATION_ACCOUNTS = 0x1780; // 6016
/** InvalidDelegatePda: Invalid delegate PDA! */
export const SVM_SPOKE_ERROR__INVALID_DELEGATE_PDA = 0x1781; // 6017

export type SvmSpokeError =
  | typeof SVM_SPOKE_ERROR__CANNOT_SET_CURRENT_TIME
  | typeof SVM_SPOKE_ERROR__CAN_ONLY_CLOSE_FILL_STATUS_PDA_IF_FILL_DEADLINE_PASSED
  | typeof SVM_SPOKE_ERROR__EXCEEDED_PENDING_BRIDGE_AMOUNT
  | typeof SVM_SPOKE_ERROR__INVALID_A_T_A_CREATION_ACCOUNTS
  | typeof SVM_SPOKE_ERROR__INVALID_CLAIM_INITIALIZER
  | typeof SVM_SPOKE_ERROR__INVALID_DELEGATE_PDA
  | typeof SVM_SPOKE_ERROR__INVALID_MINT
  | typeof SVM_SPOKE_ERROR__INVALID_PRODUCTION_SEED
  | typeof SVM_SPOKE_ERROR__INVALID_REFUND
  | typeof SVM_SPOKE_ERROR__INVALID_REFUND_TOKEN_ACCOUNT
  | typeof SVM_SPOKE_ERROR__INVALID_RELAY_HASH
  | typeof SVM_SPOKE_ERROR__INVALID_REMOTE_DOMAIN
  | typeof SVM_SPOKE_ERROR__INVALID_REMOTE_SENDER
  | typeof SVM_SPOKE_ERROR__NON_ZERO_REFUND_CLAIM
  | typeof SVM_SPOKE_ERROR__NOT_OWNER
  | typeof SVM_SPOKE_ERROR__NOT_RELAYER
  | typeof SVM_SPOKE_ERROR__PARAMS_WRITE_OVERFLOW
  | typeof SVM_SPOKE_ERROR__ZERO_REFUND_CLAIM;

let svmSpokeErrorMessages: Record<SvmSpokeError, string> | undefined;
if (process.env.NODE_ENV !== "production") {
  svmSpokeErrorMessages = {
    [SVM_SPOKE_ERROR__CANNOT_SET_CURRENT_TIME]: `Cannot set time if not in test mode!`,
    [SVM_SPOKE_ERROR__CAN_ONLY_CLOSE_FILL_STATUS_PDA_IF_FILL_DEADLINE_PASSED]: `The fill deadline has not passed!`,
    [SVM_SPOKE_ERROR__EXCEEDED_PENDING_BRIDGE_AMOUNT]: `Exceeded pending bridge amount to HubPool!`,
    [SVM_SPOKE_ERROR__INVALID_A_T_A_CREATION_ACCOUNTS]: `Invalid remaining accounts for ATA creation!`,
    [SVM_SPOKE_ERROR__INVALID_CLAIM_INITIALIZER]: `Invalid claim initializer!`,
    [SVM_SPOKE_ERROR__INVALID_DELEGATE_PDA]: `Invalid delegate PDA!`,
    [SVM_SPOKE_ERROR__INVALID_MINT]: `Invalid mint!`,
    [SVM_SPOKE_ERROR__INVALID_PRODUCTION_SEED]: `Seed must be 0 in production!`,
    [SVM_SPOKE_ERROR__INVALID_REFUND]: `Invalid refund address!`,
    [SVM_SPOKE_ERROR__INVALID_REFUND_TOKEN_ACCOUNT]: `Invalid refund token account!`,
    [SVM_SPOKE_ERROR__INVALID_RELAY_HASH]: `Invalid relay hash!`,
    [SVM_SPOKE_ERROR__INVALID_REMOTE_DOMAIN]: `Invalid remote domain!`,
    [SVM_SPOKE_ERROR__INVALID_REMOTE_SENDER]: `Invalid remote sender!`,
    [SVM_SPOKE_ERROR__NON_ZERO_REFUND_CLAIM]: `Cannot close non-zero relayer refund claim!`,
    [SVM_SPOKE_ERROR__NOT_OWNER]: `Only the owner can call this function!`,
    [SVM_SPOKE_ERROR__NOT_RELAYER]: `The caller is not the relayer!`,
    [SVM_SPOKE_ERROR__PARAMS_WRITE_OVERFLOW]: `Overflow writing to parameters account!`,
    [SVM_SPOKE_ERROR__ZERO_REFUND_CLAIM]: `Zero relayer refund claim!`,
  };
}

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

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

export function isSvmSpokeError<TProgramErrorCode extends SvmSpokeError>(
  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,
    SVM_SPOKE_PROGRAM_ADDRESS,
    code,
  );
}
