/**
 * 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,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  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 REQUEST_SLOW_FILL_PARAMS_DISCRIMINATOR = new Uint8Array([
  5, 54, 214, 89, 197, 37, 118, 28,
]);

export function getRequestSlowFillParamsDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    REQUEST_SLOW_FILL_PARAMS_DISCRIMINATOR,
  );
}

export type RequestSlowFillParams = {
  discriminator: ReadonlyUint8Array;
  relayData: RelayData;
};

export type RequestSlowFillParamsArgs = { relayData: RelayDataArgs };

/** Gets the encoder for {@link RequestSlowFillParamsArgs} account data. */
export function getRequestSlowFillParamsEncoder(): Encoder<RequestSlowFillParamsArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["relayData", getRelayDataEncoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: REQUEST_SLOW_FILL_PARAMS_DISCRIMINATOR,
    }),
  );
}

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

/** Gets the codec for {@link RequestSlowFillParams} account data. */
export function getRequestSlowFillParamsCodec(): Codec<
  RequestSlowFillParamsArgs,
  RequestSlowFillParams
> {
  return combineCodec(
    getRequestSlowFillParamsEncoder(),
    getRequestSlowFillParamsDecoder(),
  );
}

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

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

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

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

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