/**
 * 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,
  getU32Decoder,
  getU32Encoder,
  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 GET_NONCE_PDA_DISCRIMINATOR = new Uint8Array([
  114, 70, 229, 212, 223, 50, 33, 39,
]);

export function getGetNoncePdaDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    GET_NONCE_PDA_DISCRIMINATOR,
  );
}

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

export type GetNoncePdaInstructionData = {
  discriminator: ReadonlyUint8Array;
  nonce: bigint;
  sourceDomain: number;
};

export type GetNoncePdaInstructionDataArgs = {
  nonce: number | bigint;
  sourceDomain: number;
};

export function getGetNoncePdaInstructionDataEncoder(): FixedSizeEncoder<GetNoncePdaInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["nonce", getU64Encoder()],
      ["sourceDomain", getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: GET_NONCE_PDA_DISCRIMINATOR }),
  );
}

export function getGetNoncePdaInstructionDataDecoder(): FixedSizeDecoder<GetNoncePdaInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["nonce", getU64Decoder()],
    ["sourceDomain", getU32Decoder()],
  ]);
}

export function getGetNoncePdaInstructionDataCodec(): FixedSizeCodec<
  GetNoncePdaInstructionDataArgs,
  GetNoncePdaInstructionData
> {
  return combineCodec(
    getGetNoncePdaInstructionDataEncoder(),
    getGetNoncePdaInstructionDataDecoder(),
  );
}

export type GetNoncePdaInput<
  TAccountMessageTransmitter extends string = string,
> = {
  messageTransmitter: Address<TAccountMessageTransmitter>;
  nonce: GetNoncePdaInstructionDataArgs["nonce"];
  sourceDomain: GetNoncePdaInstructionDataArgs["sourceDomain"];
};

export function getGetNoncePdaInstruction<
  TAccountMessageTransmitter extends string,
  TProgramAddress extends Address = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
>(
  input: GetNoncePdaInput<TAccountMessageTransmitter>,
  config?: { programAddress?: TProgramAddress },
): GetNoncePdaInstruction<TProgramAddress, TAccountMessageTransmitter> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MESSAGE_TRANSMITTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    messageTransmitter: {
      value: input.messageTransmitter ?? 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.messageTransmitter)],
    data: getGetNoncePdaInstructionDataEncoder().encode(
      args as GetNoncePdaInstructionDataArgs,
    ),
    programAddress,
  } as GetNoncePdaInstruction<TProgramAddress, TAccountMessageTransmitter>);
}

export type ParsedGetNoncePdaInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    messageTransmitter: TAccountMetas[0];
  };
  data: GetNoncePdaInstructionData;
};

export function parseGetNoncePdaInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedGetNoncePdaInstruction<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: { messageTransmitter: getNextAccount() },
    data: getGetNoncePdaInstructionDataDecoder().decode(instruction.data),
  };
}
