/**
 * 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,
  getArrayDecoder,
  getArrayEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  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 {
  getSlowFillDecoder,
  getSlowFillEncoder,
  type SlowFill,
  type SlowFillArgs,
} from "../types";

export const EXECUTE_SLOW_RELAY_LEAF_PARAMS_DISCRIMINATOR = new Uint8Array([
  135, 208, 119, 251, 14, 222, 66, 155,
]);

export function getExecuteSlowRelayLeafParamsDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    EXECUTE_SLOW_RELAY_LEAF_PARAMS_DISCRIMINATOR,
  );
}

export type ExecuteSlowRelayLeafParams = {
  discriminator: ReadonlyUint8Array;
  slowFillLeaf: SlowFill;
  rootBundleId: number;
  proof: Array<ReadonlyUint8Array>;
};

export type ExecuteSlowRelayLeafParamsArgs = {
  slowFillLeaf: SlowFillArgs;
  rootBundleId: number;
  proof: Array<ReadonlyUint8Array>;
};

/** Gets the encoder for {@link ExecuteSlowRelayLeafParamsArgs} account data. */
export function getExecuteSlowRelayLeafParamsEncoder(): Encoder<ExecuteSlowRelayLeafParamsArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["slowFillLeaf", getSlowFillEncoder()],
      ["rootBundleId", getU32Encoder()],
      ["proof", getArrayEncoder(fixEncoderSize(getBytesEncoder(), 32))],
    ]),
    (value) => ({
      ...value,
      discriminator: EXECUTE_SLOW_RELAY_LEAF_PARAMS_DISCRIMINATOR,
    }),
  );
}

/** Gets the decoder for {@link ExecuteSlowRelayLeafParams} account data. */
export function getExecuteSlowRelayLeafParamsDecoder(): Decoder<ExecuteSlowRelayLeafParams> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["slowFillLeaf", getSlowFillDecoder()],
    ["rootBundleId", getU32Decoder()],
    ["proof", getArrayDecoder(fixDecoderSize(getBytesDecoder(), 32))],
  ]);
}

/** Gets the codec for {@link ExecuteSlowRelayLeafParams} account data. */
export function getExecuteSlowRelayLeafParamsCodec(): Codec<
  ExecuteSlowRelayLeafParamsArgs,
  ExecuteSlowRelayLeafParams
> {
  return combineCodec(
    getExecuteSlowRelayLeafParamsEncoder(),
    getExecuteSlowRelayLeafParamsDecoder(),
  );
}

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

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

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

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

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