/**
 * 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,
  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";
import {
  getFillStatusDecoder,
  getFillStatusEncoder,
  type FillStatus,
  type FillStatusArgs,
} from "../types";

export const FILL_STATUS_ACCOUNT_DISCRIMINATOR = new Uint8Array([
  105, 89, 88, 35, 24, 147, 178, 137,
]);

export function getFillStatusAccountDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    FILL_STATUS_ACCOUNT_DISCRIMINATOR,
  );
}

export type FillStatusAccount = {
  discriminator: ReadonlyUint8Array;
  status: FillStatus;
  relayer: Address;
  fillDeadline: number;
};

export type FillStatusAccountArgs = {
  status: FillStatusArgs;
  relayer: Address;
  fillDeadline: number;
};

/** Gets the encoder for {@link FillStatusAccountArgs} account data. */
export function getFillStatusAccountEncoder(): FixedSizeEncoder<FillStatusAccountArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["status", getFillStatusEncoder()],
      ["relayer", getAddressEncoder()],
      ["fillDeadline", getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: FILL_STATUS_ACCOUNT_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link FillStatusAccount} account data. */
export function getFillStatusAccountDecoder(): FixedSizeDecoder<FillStatusAccount> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["status", getFillStatusDecoder()],
    ["relayer", getAddressDecoder()],
    ["fillDeadline", getU32Decoder()],
  ]);
}

/** Gets the codec for {@link FillStatusAccount} account data. */
export function getFillStatusAccountCodec(): FixedSizeCodec<
  FillStatusAccountArgs,
  FillStatusAccount
> {
  return combineCodec(
    getFillStatusAccountEncoder(),
    getFillStatusAccountDecoder(),
  );
}

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

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

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

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

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

export function getFillStatusAccountSize(): number {
  return 45;
}
