/**
 * 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,
  getU64Decoder,
  getU64Encoder,
  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 { MESSAGE_TRANSMITTER_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const IS_NONCE_USED_DISCRIMINATOR = new Uint8Array([
  144, 72, 107, 148, 35, 218, 31, 187,
]);

export function getIsNonceUsedDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    IS_NONCE_USED_DISCRIMINATOR,
  );
}

export type IsNonceUsedInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountUsedNonces extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountUsedNonces extends string
        ? ReadonlyAccount<TAccountUsedNonces>
        : TAccountUsedNonces,
      ...TRemainingAccounts,
    ]
  >;

export type IsNonceUsedInstructionData = {
  discriminator: ReadonlyUint8Array;
  nonce: bigint;
};

export type IsNonceUsedInstructionDataArgs = { nonce: number | bigint };

export function getIsNonceUsedInstructionDataEncoder(): FixedSizeEncoder<IsNonceUsedInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["nonce", getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: IS_NONCE_USED_DISCRIMINATOR }),
  );
}

export function getIsNonceUsedInstructionDataDecoder(): FixedSizeDecoder<IsNonceUsedInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["nonce", getU64Decoder()],
  ]);
}

export function getIsNonceUsedInstructionDataCodec(): FixedSizeCodec<
  IsNonceUsedInstructionDataArgs,
  IsNonceUsedInstructionData
> {
  return combineCodec(
    getIsNonceUsedInstructionDataEncoder(),
    getIsNonceUsedInstructionDataDecoder(),
  );
}

export type IsNonceUsedInput<TAccountUsedNonces extends string = string> = {
  /** Account will be explicitly loaded to avoid error when it's not initialized */
  usedNonces: Address<TAccountUsedNonces>;
  nonce: IsNonceUsedInstructionDataArgs["nonce"];
};

export function getIsNonceUsedInstruction<
  TAccountUsedNonces extends string,
  TProgramAddress extends Address = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
>(
  input: IsNonceUsedInput<TAccountUsedNonces>,
  config?: { programAddress?: TProgramAddress },
): IsNonceUsedInstruction<TProgramAddress, TAccountUsedNonces> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MESSAGE_TRANSMITTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    usedNonces: { value: input.usedNonces ?? 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.usedNonces)],
    data: getIsNonceUsedInstructionDataEncoder().encode(
      args as IsNonceUsedInstructionDataArgs,
    ),
    programAddress,
  } as IsNonceUsedInstruction<TProgramAddress, TAccountUsedNonces>);
}

export type ParsedIsNonceUsedInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    /** Account will be explicitly loaded to avoid error when it's not initialized */
    usedNonces: TAccountMetas[0];
  };
  data: IsNonceUsedInstructionData;
};

export function parseIsNonceUsedInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedIsNonceUsedInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 1) {
    // 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: { usedNonces: getNextAccount() },
    data: getIsNonceUsedInstructionDataDecoder().decode(instruction.data),
  };
}
