/**
 * 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 {
  assertAccountExists,
  assertAccountsExist,
  combineCodec,
  decodeAccount,
  fetchEncodedAccount,
  fetchEncodedAccounts,
  fixDecoderSize,
  fixEncoderSize,
  getAddressDecoder,
  getAddressEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getU8Decoder,
  getU8Encoder,
  transformEncoder,
  type Account,
  type Address,
  type EncodedAccount,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type MaybeAccount,
  type MaybeEncodedAccount,
  type ReadonlyUint8Array,
} from "@solana/kit";

export const TOKEN_PAIR_DISCRIMINATOR = new Uint8Array([
  17, 214, 45, 176, 229, 149, 197, 71,
]);

export function getTokenPairDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(TOKEN_PAIR_DISCRIMINATOR);
}

export type TokenPair = {
  discriminator: ReadonlyUint8Array;
  remoteDomain: number;
  remoteToken: Address;
  localToken: Address;
  bump: number;
};

export type TokenPairArgs = {
  remoteDomain: number;
  remoteToken: Address;
  localToken: Address;
  bump: number;
};

/** Gets the encoder for {@link TokenPairArgs} account data. */
export function getTokenPairEncoder(): FixedSizeEncoder<TokenPairArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["remoteDomain", getU32Encoder()],
      ["remoteToken", getAddressEncoder()],
      ["localToken", getAddressEncoder()],
      ["bump", getU8Encoder()],
    ]),
    (value) => ({ ...value, discriminator: TOKEN_PAIR_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link TokenPair} account data. */
export function getTokenPairDecoder(): FixedSizeDecoder<TokenPair> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["remoteDomain", getU32Decoder()],
    ["remoteToken", getAddressDecoder()],
    ["localToken", getAddressDecoder()],
    ["bump", getU8Decoder()],
  ]);
}

/** Gets the codec for {@link TokenPair} account data. */
export function getTokenPairCodec(): FixedSizeCodec<TokenPairArgs, TokenPair> {
  return combineCodec(getTokenPairEncoder(), getTokenPairDecoder());
}

export function decodeTokenPair<TAddress extends string = string>(
  encodedAccount: EncodedAccount<TAddress>,
): Account<TokenPair, TAddress>;
export function decodeTokenPair<TAddress extends string = string>(
  encodedAccount: MaybeEncodedAccount<TAddress>,
): MaybeAccount<TokenPair, TAddress>;
export function decodeTokenPair<TAddress extends string = string>(
  encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>,
): Account<TokenPair, TAddress> | MaybeAccount<TokenPair, TAddress> {
  return decodeAccount(
    encodedAccount as MaybeEncodedAccount<TAddress>,
    getTokenPairDecoder(),
  );
}

export async function fetchTokenPair<TAddress extends string = string>(
  rpc: Parameters<typeof fetchEncodedAccount>[0],
  address: Address<TAddress>,
  config?: FetchAccountConfig,
): Promise<Account<TokenPair, TAddress>> {
  const maybeAccount = await fetchMaybeTokenPair(rpc, address, config);
  assertAccountExists(maybeAccount);
  return maybeAccount;
}

export async function fetchMaybeTokenPair<TAddress extends string = string>(
  rpc: Parameters<typeof fetchEncodedAccount>[0],
  address: Address<TAddress>,
  config?: FetchAccountConfig,
): Promise<MaybeAccount<TokenPair, TAddress>> {
  const maybeAccount = await fetchEncodedAccount(rpc, address, config);
  return decodeTokenPair(maybeAccount);
}

export async function fetchAllTokenPair(
  rpc: Parameters<typeof fetchEncodedAccounts>[0],
  addresses: Array<Address>,
  config?: FetchAccountsConfig,
): Promise<Account<TokenPair>[]> {
  const maybeAccounts = await fetchAllMaybeTokenPair(rpc, addresses, config);
  assertAccountsExist(maybeAccounts);
  return maybeAccounts;
}

export async function fetchAllMaybeTokenPair(
  rpc: Parameters<typeof fetchEncodedAccounts>[0],
  addresses: Array<Address>,
  config?: FetchAccountsConfig,
): Promise<MaybeAccount<TokenPair>[]> {
  const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
  return maybeAccounts.map((maybeAccount) => decodeTokenPair(maybeAccount));
}

export function getTokenPairSize(): number {
  return 77;
}
