/**
 * 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,
  getU64Decoder,
  getU64Encoder,
  transformEncoder,
  type Account,
  type Address,
  type Codec,
  type Decoder,
  type EncodedAccount,
  type Encoder,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type MaybeAccount,
  type MaybeEncodedAccount,
  type ReadonlyUint8Array,
} from "@solana/kit";
import {
  getRelayDataDecoder,
  getRelayDataEncoder,
  type RelayData,
  type RelayDataArgs,
} from "../types";

export const FILL_RELAY_PARAMS_DISCRIMINATOR = new Uint8Array([
  50, 243, 51, 185, 89, 60, 43, 202,
]);

export function getFillRelayParamsDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    FILL_RELAY_PARAMS_DISCRIMINATOR,
  );
}

export type FillRelayParams = {
  discriminator: ReadonlyUint8Array;
  relayData: RelayData;
  repaymentChainId: bigint;
  repaymentAddress: Address;
};

export type FillRelayParamsArgs = {
  relayData: RelayDataArgs;
  repaymentChainId: number | bigint;
  repaymentAddress: Address;
};

/** Gets the encoder for {@link FillRelayParamsArgs} account data. */
export function getFillRelayParamsEncoder(): Encoder<FillRelayParamsArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["relayData", getRelayDataEncoder()],
      ["repaymentChainId", getU64Encoder()],
      ["repaymentAddress", getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: FILL_RELAY_PARAMS_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link FillRelayParams} account data. */
export function getFillRelayParamsDecoder(): Decoder<FillRelayParams> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["relayData", getRelayDataDecoder()],
    ["repaymentChainId", getU64Decoder()],
    ["repaymentAddress", getAddressDecoder()],
  ]);
}

/** Gets the codec for {@link FillRelayParams} account data. */
export function getFillRelayParamsCodec(): Codec<
  FillRelayParamsArgs,
  FillRelayParams
> {
  return combineCodec(getFillRelayParamsEncoder(), getFillRelayParamsDecoder());
}

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

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

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

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

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