/**
 * 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 {
  addDecoderSizePrefix,
  addEncoderSizePrefix,
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  transformEncoder,
  type AccountMeta,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
  type WritableAccount,
} from "@solana/kit";
import { findEventAuthorityPda, findRentFundPda } from "../pdas";
import { SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const RECLAIM_EVENT_ACCOUNT_DISCRIMINATOR = new Uint8Array([
  94, 198, 180, 159, 131, 236, 15, 174,
]);

export function getReclaimEventAccountDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    RECLAIM_EVENT_ACCOUNT_DISCRIMINATOR,
  );
}

export type ReclaimEventAccountInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountRentFund extends string | AccountMeta<string> = string,
  TAccountMessageTransmitter extends string | AccountMeta<string> = string,
  TAccountMessageSentEventData extends string | AccountMeta<string> = string,
  TAccountMessageTransmitterProgram extends string | AccountMeta<string> =
    "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountRentFund extends string
        ? WritableAccount<TAccountRentFund>
        : TAccountRentFund,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountMessageSentEventData extends string
        ? WritableAccount<TAccountMessageSentEventData>
        : TAccountMessageSentEventData,
      TAccountMessageTransmitterProgram extends string
        ? ReadonlyAccount<TAccountMessageTransmitterProgram>
        : TAccountMessageTransmitterProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type ReclaimEventAccountInstructionData = {
  discriminator: ReadonlyUint8Array;
  attestation: ReadonlyUint8Array;
  nonce: ReadonlyUint8Array;
  finalityThresholdExecuted: ReadonlyUint8Array;
  feeExecuted: ReadonlyUint8Array;
  expirationBlock: ReadonlyUint8Array;
};

export type ReclaimEventAccountInstructionDataArgs = {
  attestation: ReadonlyUint8Array;
  nonce: ReadonlyUint8Array;
  finalityThresholdExecuted: ReadonlyUint8Array;
  feeExecuted: ReadonlyUint8Array;
  expirationBlock: ReadonlyUint8Array;
};

export function getReclaimEventAccountInstructionDataEncoder(): Encoder<ReclaimEventAccountInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["attestation", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
      ["nonce", fixEncoderSize(getBytesEncoder(), 32)],
      ["finalityThresholdExecuted", fixEncoderSize(getBytesEncoder(), 4)],
      ["feeExecuted", fixEncoderSize(getBytesEncoder(), 32)],
      ["expirationBlock", fixEncoderSize(getBytesEncoder(), 32)],
    ]),
    (value) => ({
      ...value,
      discriminator: RECLAIM_EVENT_ACCOUNT_DISCRIMINATOR,
    }),
  );
}

export function getReclaimEventAccountInstructionDataDecoder(): Decoder<ReclaimEventAccountInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["attestation", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
    ["nonce", fixDecoderSize(getBytesDecoder(), 32)],
    ["finalityThresholdExecuted", fixDecoderSize(getBytesDecoder(), 4)],
    ["feeExecuted", fixDecoderSize(getBytesDecoder(), 32)],
    ["expirationBlock", fixDecoderSize(getBytesDecoder(), 32)],
  ]);
}

export function getReclaimEventAccountInstructionDataCodec(): Codec<
  ReclaimEventAccountInstructionDataArgs,
  ReclaimEventAccountInstructionData
> {
  return combineCodec(
    getReclaimEventAccountInstructionDataEncoder(),
    getReclaimEventAccountInstructionDataDecoder(),
  );
}

export type ReclaimEventAccountAsyncInput<
  TAccountRentFund extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountMessageSentEventData extends string = string,
  TAccountMessageTransmitterProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  rentFund?: Address<TAccountRentFund>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  messageSentEventData: Address<TAccountMessageSentEventData>;
  messageTransmitterProgram?: Address<TAccountMessageTransmitterProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  attestation: ReclaimEventAccountInstructionDataArgs["attestation"];
  nonce: ReclaimEventAccountInstructionDataArgs["nonce"];
  finalityThresholdExecuted: ReclaimEventAccountInstructionDataArgs["finalityThresholdExecuted"];
  feeExecuted: ReclaimEventAccountInstructionDataArgs["feeExecuted"];
  expirationBlock: ReclaimEventAccountInstructionDataArgs["expirationBlock"];
};

export async function getReclaimEventAccountInstructionAsync<
  TAccountRentFund extends string,
  TAccountMessageTransmitter extends string,
  TAccountMessageSentEventData extends string,
  TAccountMessageTransmitterProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: ReclaimEventAccountAsyncInput<
    TAccountRentFund,
    TAccountMessageTransmitter,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  ReclaimEventAccountInstruction<
    TProgramAddress,
    TAccountRentFund,
    TAccountMessageTransmitter,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    messageSentEventData: {
      value: input.messageSentEventData ?? null,
      isWritable: true,
    },
    messageTransmitterProgram: {
      value: input.messageTransmitterProgram ?? null,
      isWritable: false,
    },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    program: { value: input.program ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

  // Resolve default values.
  if (!accounts.rentFund.value) {
    accounts.rentFund.value = await findRentFundPda();
  }
  if (!accounts.messageTransmitterProgram.value) {
    accounts.messageTransmitterProgram.value =
      "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC" as Address<"CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC">;
  }
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.rentFund),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.messageSentEventData),
      getAccountMeta(accounts.messageTransmitterProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getReclaimEventAccountInstructionDataEncoder().encode(
      args as ReclaimEventAccountInstructionDataArgs,
    ),
    programAddress,
  } as ReclaimEventAccountInstruction<
    TProgramAddress,
    TAccountRentFund,
    TAccountMessageTransmitter,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ReclaimEventAccountInput<
  TAccountRentFund extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountMessageSentEventData extends string = string,
  TAccountMessageTransmitterProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  rentFund: Address<TAccountRentFund>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  messageSentEventData: Address<TAccountMessageSentEventData>;
  messageTransmitterProgram?: Address<TAccountMessageTransmitterProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  attestation: ReclaimEventAccountInstructionDataArgs["attestation"];
  nonce: ReclaimEventAccountInstructionDataArgs["nonce"];
  finalityThresholdExecuted: ReclaimEventAccountInstructionDataArgs["finalityThresholdExecuted"];
  feeExecuted: ReclaimEventAccountInstructionDataArgs["feeExecuted"];
  expirationBlock: ReclaimEventAccountInstructionDataArgs["expirationBlock"];
};

export function getReclaimEventAccountInstruction<
  TAccountRentFund extends string,
  TAccountMessageTransmitter extends string,
  TAccountMessageSentEventData extends string,
  TAccountMessageTransmitterProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: ReclaimEventAccountInput<
    TAccountRentFund,
    TAccountMessageTransmitter,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): ReclaimEventAccountInstruction<
  TProgramAddress,
  TAccountRentFund,
  TAccountMessageTransmitter,
  TAccountMessageSentEventData,
  TAccountMessageTransmitterProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    messageSentEventData: {
      value: input.messageSentEventData ?? null,
      isWritable: true,
    },
    messageTransmitterProgram: {
      value: input.messageTransmitterProgram ?? null,
      isWritable: false,
    },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    program: { value: input.program ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

  // Resolve default values.
  if (!accounts.messageTransmitterProgram.value) {
    accounts.messageTransmitterProgram.value =
      "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC" as Address<"CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.rentFund),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.messageSentEventData),
      getAccountMeta(accounts.messageTransmitterProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getReclaimEventAccountInstructionDataEncoder().encode(
      args as ReclaimEventAccountInstructionDataArgs,
    ),
    programAddress,
  } as ReclaimEventAccountInstruction<
    TProgramAddress,
    TAccountRentFund,
    TAccountMessageTransmitter,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedReclaimEventAccountInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    rentFund: TAccountMetas[0];
    messageTransmitter: TAccountMetas[1];
    messageSentEventData: TAccountMetas[2];
    messageTransmitterProgram: TAccountMetas[3];
    eventAuthority: TAccountMetas[4];
    program: TAccountMetas[5];
  };
  data: ReclaimEventAccountInstructionData;
};

export function parseReclaimEventAccountInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedReclaimEventAccountInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 6) {
    // 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: {
      rentFund: getNextAccount(),
      messageTransmitter: getNextAccount(),
      messageSentEventData: getNextAccount(),
      messageTransmitterProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getReclaimEventAccountInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
