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

export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_EVENT_AUTHORITY = 0x0; // 0
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ATA = 0x1; // 1
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_MINT = 0x2; // 2
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_INSTANCE_ID = 0x3; // 3
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_INSTANCE = 0x4; // 4
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ADMIN = 0x5; // 5
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__TRANSFER_HOOK_NOT_ALLOWED = 0x6; // 6
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_OPERATOR_PDA = 0x7; // 7
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_TOKEN_ACCOUNT = 0x8; // 8
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ESCROW_BALANCE = 0x9; // 9
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ALLOWED_MINT = 0xa; // 10
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_SMT_PROOF = 0xb; // 11
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_TRANSACTION_NONCE_FOR_CURRENT_TREE_INDEX = 0xc; // 12
export const PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__UNEXPECTED_TREE_INDEX = 0xd; // 13

export type PrivateChannelEscrowProgramError =
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ADMIN
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ALLOWED_MINT
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ATA
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ESCROW_BALANCE
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_EVENT_AUTHORITY
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_INSTANCE
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_INSTANCE_ID
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_MINT
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_OPERATOR_PDA
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_SMT_PROOF
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_TOKEN_ACCOUNT
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_TRANSACTION_NONCE_FOR_CURRENT_TREE_INDEX
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__TRANSFER_HOOK_NOT_ALLOWED
  | typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__UNEXPECTED_TREE_INDEX;

let privateChannelEscrowProgramErrorMessages:
  | Record<PrivateChannelEscrowProgramError, string>
  | undefined;
if (process.env["NODE_ENV"] !== "production") {
  privateChannelEscrowProgramErrorMessages = {
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ADMIN]: `Invalid admin provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ALLOWED_MINT]: `Invalid allowed mint`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ATA]: `Invalid ATA provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_ESCROW_BALANCE]: `Invalid escrow balance`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_EVENT_AUTHORITY]: `Invalid event authority provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_INSTANCE]: `Invalid instance provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_INSTANCE_ID]: `Instance ID invalid or does not respect rules`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_MINT]: `Invalid mint provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_OPERATOR_PDA]: `Invalid operator PDA provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_SMT_PROOF]: `Invalid SMT proof provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_TOKEN_ACCOUNT]: `Invalid token account provided`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__INVALID_TRANSACTION_NONCE_FOR_CURRENT_TREE_INDEX]: `Invalid transaction nonce for current tree index`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__TRANSFER_HOOK_NOT_ALLOWED]: `Transfer hook extension not allowed`,
    [PRIVATE_CHANNEL_ESCROW_PROGRAM_ERROR__UNEXPECTED_TREE_INDEX]: `Unexpected current tree index for SMT root reset`,
  };
}

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

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

export function isPrivateChannelEscrowProgramError<
  TProgramErrorCode extends PrivateChannelEscrowProgramError,
>(
  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,
    PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
    code,
  );
}
