/**
 * 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,
  getBooleanDecoder,
  getBooleanEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getU64Decoder,
  getU64Encoder,
  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 STATE_DISCRIMINATOR = new Uint8Array([
  216, 146, 107, 94, 104, 75, 182, 177,
]);

export function getStateDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(STATE_DISCRIMINATOR);
}

export type State = {
  discriminator: ReadonlyUint8Array;
  pausedDeposits: boolean;
  pausedFills: boolean;
  owner: Address;
  seed: bigint;
  numberOfDeposits: number;
  chainId: bigint;
  currentTime: number;
  remoteDomain: number;
  crossDomainAdmin: Address;
  rootBundleId: number;
  depositQuoteTimeBuffer: number;
  fillDeadlineBuffer: number;
};

export type StateArgs = {
  pausedDeposits: boolean;
  pausedFills: boolean;
  owner: Address;
  seed: number | bigint;
  numberOfDeposits: number;
  chainId: number | bigint;
  currentTime: number;
  remoteDomain: number;
  crossDomainAdmin: Address;
  rootBundleId: number;
  depositQuoteTimeBuffer: number;
  fillDeadlineBuffer: number;
};

/** Gets the encoder for {@link StateArgs} account data. */
export function getStateEncoder(): FixedSizeEncoder<StateArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["pausedDeposits", getBooleanEncoder()],
      ["pausedFills", getBooleanEncoder()],
      ["owner", getAddressEncoder()],
      ["seed", getU64Encoder()],
      ["numberOfDeposits", getU32Encoder()],
      ["chainId", getU64Encoder()],
      ["currentTime", getU32Encoder()],
      ["remoteDomain", getU32Encoder()],
      ["crossDomainAdmin", getAddressEncoder()],
      ["rootBundleId", getU32Encoder()],
      ["depositQuoteTimeBuffer", getU32Encoder()],
      ["fillDeadlineBuffer", getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: STATE_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link State} account data. */
export function getStateDecoder(): FixedSizeDecoder<State> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["pausedDeposits", getBooleanDecoder()],
    ["pausedFills", getBooleanDecoder()],
    ["owner", getAddressDecoder()],
    ["seed", getU64Decoder()],
    ["numberOfDeposits", getU32Decoder()],
    ["chainId", getU64Decoder()],
    ["currentTime", getU32Decoder()],
    ["remoteDomain", getU32Decoder()],
    ["crossDomainAdmin", getAddressDecoder()],
    ["rootBundleId", getU32Decoder()],
    ["depositQuoteTimeBuffer", getU32Decoder()],
    ["fillDeadlineBuffer", getU32Decoder()],
  ]);
}

/** Gets the codec for {@link State} account data. */
export function getStateCodec(): FixedSizeCodec<StateArgs, State> {
  return combineCodec(getStateEncoder(), getStateDecoder());
}

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

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

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

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

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

export function getStateSize(): number {
  return 114;
}
