/**
 * 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, 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 ORACLE_DISCRIMINATOR = new Uint8Array([139, 194, 131, 179, 140, 179, 229, 244]);

export function getOracleDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(ORACLE_DISCRIMINATOR); }

export type Oracle = { discriminator: ReadonlyUint8Array; 
/** Index of latest observation slot */
idx: bigint; 
/** Size of active sample. Active sample is initialized observation. */
activeSize: bigint; 
/** Number of observations */
length: bigint;  };

export type OracleArgs = { 
/** Index of latest observation slot */
idx: number | bigint; 
/** Size of active sample. Active sample is initialized observation. */
activeSize: number | bigint; 
/** Number of observations */
length: number | bigint;  };

/** Gets the encoder for {@link OracleArgs} account data. */
export function getOracleEncoder(): FixedSizeEncoder<OracleArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['idx', getU64Encoder()], ['activeSize', getU64Encoder()], ['length', getU64Encoder()]]), (value) => ({ ...value, discriminator: ORACLE_DISCRIMINATOR }));
}

/** Gets the decoder for {@link Oracle} account data. */
export function getOracleDecoder(): FixedSizeDecoder<Oracle> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['idx', getU64Decoder()], ['activeSize', getU64Decoder()], ['length', getU64Decoder()]]);
}

/** Gets the codec for {@link Oracle} account data. */
export function getOracleCodec(): FixedSizeCodec<OracleArgs, Oracle> {
    return combineCodec(getOracleEncoder(), getOracleDecoder());
}

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

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

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

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

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

export function getOracleSize(): number {
  return 32;
}