/**
 * 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,
  getArrayDecoder,
  getArrayEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  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 {
  getDatedPriceDecoder,
  getDatedPriceEncoder,
  type DatedPrice,
  type DatedPriceArgs,
} from "../types";

export const ORACLE_PRICES_DISCRIMINATOR = new Uint8Array([
  89, 128, 118, 221, 6, 72, 180, 146,
]);

export function getOraclePricesDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    ORACLE_PRICES_DISCRIMINATOR,
  );
}

export type OraclePrices = {
  discriminator: ReadonlyUint8Array;
  oracleMappings: Address;
  prices: Array<DatedPrice>;
};

export type OraclePricesArgs = {
  oracleMappings: Address;
  prices: Array<DatedPriceArgs>;
};

/** Gets the encoder for {@link OraclePricesArgs} account data. */
export function getOraclePricesEncoder(): FixedSizeEncoder<OraclePricesArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["oracleMappings", getAddressEncoder()],
      ["prices", getArrayEncoder(getDatedPriceEncoder(), { size: 512 })],
    ]),
    (value) => ({ ...value, discriminator: ORACLE_PRICES_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link OraclePrices} account data. */
export function getOraclePricesDecoder(): FixedSizeDecoder<OraclePrices> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["oracleMappings", getAddressDecoder()],
    ["prices", getArrayDecoder(getDatedPriceDecoder(), { size: 512 })],
  ]);
}

/** Gets the codec for {@link OraclePrices} account data. */
export function getOraclePricesCodec(): FixedSizeCodec<
  OraclePricesArgs,
  OraclePrices
> {
  return combineCodec(getOraclePricesEncoder(), getOraclePricesDecoder());
}

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

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

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

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

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

export function getOraclePricesSize(): number {
  return 28712;
}
