/**
 * 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,
  decodeAccount,
  fetchEncodedAccount,
  fetchEncodedAccounts,
  type Account,
  type EncodedAccount,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type MaybeAccount,
  type MaybeEncodedAccount,
} from '@solana/kit';
import { getAddressDecoder, getAddressEncoder, type Address } from '@solana/kit';
import {
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getArrayDecoder,
  getArrayEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getU64Decoder,
  getU64Encoder,
  transformEncoder,
  type Codec,
  type Decoder,
  type Encoder,
  type ReadonlyUint8Array,
} from '@solana/kit';

export const LIMIT_ORDER_DISCRIMINATOR = new Uint8Array([137, 183, 212, 91, 115, 29, 141, 227]);

export function getLimitOrderDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(LIMIT_ORDER_DISCRIMINATOR);
}

export type LimitOrder = {
  discriminator: ReadonlyUint8Array;
  /** User address, that created this order */
  user: Address;
  /** Guard address, that will protect this order execution */
  guard: Address;
  /** Token IN that user wants to spend */
  tokenInMint: Address;
  /** Amount of tokens In locked */
  amountIn: bigint;
  /** Minimum amount of tokens OUT to receive */
  amountOutMin: bigint;
  /**
   * Keccak256 hash of (token_out,receiver,extra_transfers_tokens_and_receivers,secret_number)
   * Since we want to hide tokens OUT right until execution moment to protect user from
   * any sort of MEV attack
   */
  secretHash: ReadonlyUint8Array;
  /**
   * Array of requested extra transfers amounts
   * Since we want to hide tokens OUT and receivers right until execution moment
   * to protect user from any sort of MEV attack
   * Tokens and receivers will be collected from `remaining_accounts`
   * during fulfillment and checked against `secret_hash` value
   */
  extraTransfersAmounts: Array<bigint>;
  /** Deadline for the operation, in Unix timestamp format, in SECONDS */
  deadline: number;
};

export type LimitOrderArgs = {
  /** User address, that created this order */
  user: Address;
  /** Guard address, that will protect this order execution */
  guard: Address;
  /** Token IN that user wants to spend */
  tokenInMint: Address;
  /** Amount of tokens In locked */
  amountIn: number | bigint;
  /** Minimum amount of tokens OUT to receive */
  amountOutMin: number | bigint;
  /**
   * Keccak256 hash of (token_out,receiver,extra_transfers_tokens_and_receivers,secret_number)
   * Since we want to hide tokens OUT right until execution moment to protect user from
   * any sort of MEV attack
   */
  secretHash: ReadonlyUint8Array;
  /**
   * Array of requested extra transfers amounts
   * Since we want to hide tokens OUT and receivers right until execution moment
   * to protect user from any sort of MEV attack
   * Tokens and receivers will be collected from `remaining_accounts`
   * during fulfillment and checked against `secret_hash` value
   */
  extraTransfersAmounts: Array<number | bigint>;
  /** Deadline for the operation, in Unix timestamp format, in SECONDS */
  deadline: number;
};

export function getLimitOrderEncoder(): Encoder<LimitOrderArgs> {
  return transformEncoder(
    getStructEncoder([
      ['discriminator', fixEncoderSize(getBytesEncoder(), 8)],
      ['user', getAddressEncoder()],
      ['guard', getAddressEncoder()],
      ['tokenInMint', getAddressEncoder()],
      ['amountIn', getU64Encoder()],
      ['amountOutMin', getU64Encoder()],
      ['secretHash', fixEncoderSize(getBytesEncoder(), 32)],
      ['extraTransfersAmounts', getArrayEncoder(getU64Encoder())],
      ['deadline', getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: LIMIT_ORDER_DISCRIMINATOR }),
  );
}

export function getLimitOrderDecoder(): Decoder<LimitOrder> {
  return getStructDecoder([
    ['discriminator', fixDecoderSize(getBytesDecoder(), 8)],
    ['user', getAddressDecoder()],
    ['guard', getAddressDecoder()],
    ['tokenInMint', getAddressDecoder()],
    ['amountIn', getU64Decoder()],
    ['amountOutMin', getU64Decoder()],
    ['secretHash', fixDecoderSize(getBytesDecoder(), 32)],
    ['extraTransfersAmounts', getArrayDecoder(getU64Decoder())],
    ['deadline', getU32Decoder()],
  ]);
}

export function getLimitOrderCodec(): Codec<LimitOrderArgs, LimitOrder> {
  return combineCodec(getLimitOrderEncoder(), getLimitOrderDecoder());
}

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

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

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

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

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