/**
 * 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 {
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  transformEncoder,
  type AccountMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
} from "@solana/kit";
import { findStatePda } from "../pdas";
import { SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";
import {
  getUsedNonceAccountParamsDecoder,
  getUsedNonceAccountParamsEncoder,
  type UsedNonceAccountParams,
  type UsedNonceAccountParamsArgs,
} from "../types";

export const GET_USED_NONCE_CLOSE_INFO_DISCRIMINATOR = new Uint8Array([
  19, 183, 42, 151, 118, 234, 57, 92,
]);

export function getGetUsedNonceCloseInfoDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    GET_USED_NONCE_CLOSE_INFO_DISCRIMINATOR,
  );
}

export type GetUsedNonceCloseInfoInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountUsedNonce extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountState extends string
        ? ReadonlyAccount<TAccountState>
        : TAccountState,
      TAccountUsedNonce extends string
        ? ReadonlyAccount<TAccountUsedNonce>
        : TAccountUsedNonce,
      ...TRemainingAccounts,
    ]
  >;

export type GetUsedNonceCloseInfoInstructionData = {
  discriminator: ReadonlyUint8Array;
  params: UsedNonceAccountParams;
};

export type GetUsedNonceCloseInfoInstructionDataArgs = {
  params: UsedNonceAccountParamsArgs;
};

export function getGetUsedNonceCloseInfoInstructionDataEncoder(): FixedSizeEncoder<GetUsedNonceCloseInfoInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["params", getUsedNonceAccountParamsEncoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: GET_USED_NONCE_CLOSE_INFO_DISCRIMINATOR,
    }),
  );
}

export function getGetUsedNonceCloseInfoInstructionDataDecoder(): FixedSizeDecoder<GetUsedNonceCloseInfoInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["params", getUsedNonceAccountParamsDecoder()],
  ]);
}

export function getGetUsedNonceCloseInfoInstructionDataCodec(): FixedSizeCodec<
  GetUsedNonceCloseInfoInstructionDataArgs,
  GetUsedNonceCloseInfoInstructionData
> {
  return combineCodec(
    getGetUsedNonceCloseInfoInstructionDataEncoder(),
    getGetUsedNonceCloseInfoInstructionDataDecoder(),
  );
}

export type GetUsedNonceCloseInfoAsyncInput<
  TAccountState extends string = string,
  TAccountUsedNonce extends string = string,
> = {
  state?: Address<TAccountState>;
  usedNonce: Address<TAccountUsedNonce>;
  params: GetUsedNonceCloseInfoInstructionDataArgs["params"];
};

export async function getGetUsedNonceCloseInfoInstructionAsync<
  TAccountState extends string,
  TAccountUsedNonce extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: GetUsedNonceCloseInfoAsyncInput<TAccountState, TAccountUsedNonce>,
  config?: { programAddress?: TProgramAddress },
): Promise<
  GetUsedNonceCloseInfoInstruction<
    TProgramAddress,
    TAccountState,
    TAccountUsedNonce
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    state: { value: input.state ?? null, isWritable: false },
    usedNonce: { value: input.usedNonce ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.state.value) {
    accounts.state.value = await findStatePda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.usedNonce),
    ],
    data: getGetUsedNonceCloseInfoInstructionDataEncoder().encode(
      args as GetUsedNonceCloseInfoInstructionDataArgs,
    ),
    programAddress,
  } as GetUsedNonceCloseInfoInstruction<
    TProgramAddress,
    TAccountState,
    TAccountUsedNonce
  >);
}

export type GetUsedNonceCloseInfoInput<
  TAccountState extends string = string,
  TAccountUsedNonce extends string = string,
> = {
  state: Address<TAccountState>;
  usedNonce: Address<TAccountUsedNonce>;
  params: GetUsedNonceCloseInfoInstructionDataArgs["params"];
};

export function getGetUsedNonceCloseInfoInstruction<
  TAccountState extends string,
  TAccountUsedNonce extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: GetUsedNonceCloseInfoInput<TAccountState, TAccountUsedNonce>,
  config?: { programAddress?: TProgramAddress },
): GetUsedNonceCloseInfoInstruction<
  TProgramAddress,
  TAccountState,
  TAccountUsedNonce
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    state: { value: input.state ?? null, isWritable: false },
    usedNonce: { value: input.usedNonce ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Original args.
  const args = { ...input };

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.usedNonce),
    ],
    data: getGetUsedNonceCloseInfoInstructionDataEncoder().encode(
      args as GetUsedNonceCloseInfoInstructionDataArgs,
    ),
    programAddress,
  } as GetUsedNonceCloseInfoInstruction<
    TProgramAddress,
    TAccountState,
    TAccountUsedNonce
  >);
}

export type ParsedGetUsedNonceCloseInfoInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    state: TAccountMetas[0];
    usedNonce: TAccountMetas[1];
  };
  data: GetUsedNonceCloseInfoInstructionData;
};

export function parseGetUsedNonceCloseInfoInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedGetUsedNonceCloseInfoInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 2) {
    // TODO: Coded error.
    throw new Error("Not enough accounts");
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: { state: getNextAccount(), usedNonce: getNextAccount() },
    data: getGetUsedNonceCloseInfoInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
