/**
 * 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 AccountSignerMeta,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import { MESSAGE_TRANSMITTER_V2_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 MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
  TAccountPayee extends string | AccountMeta<string> = string,
  TAccountMessageTransmitter extends string | AccountMeta<string> = string,
  TAccountMessageSentEventData extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountPayee extends string
        ? WritableSignerAccount<TAccountPayee> &
            AccountSignerMeta<TAccountPayee>
        : TAccountPayee,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountMessageSentEventData extends string
        ? WritableAccount<TAccountMessageSentEventData>
        : TAccountMessageSentEventData,
      ...TRemainingAccounts,
    ]
  >;

export type ReclaimEventAccountInstructionData = {
  discriminator: ReadonlyUint8Array;
  attestation: ReadonlyUint8Array;
  destinationMessage: ReadonlyUint8Array;
};

export type ReclaimEventAccountInstructionDataArgs = {
  attestation: ReadonlyUint8Array;
  destinationMessage: ReadonlyUint8Array;
};

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

export function getReclaimEventAccountInstructionDataDecoder(): Decoder<ReclaimEventAccountInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["attestation", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
    [
      "destinationMessage",
      addDecoderSizePrefix(getBytesDecoder(), getU32Decoder()),
    ],
  ]);
}

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

export type ReclaimEventAccountInput<
  TAccountPayee extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountMessageSentEventData extends string = string,
> = {
  /** rent SOL receiver, should match original rent payer */
  payee: TransactionSigner<TAccountPayee>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  messageSentEventData: Address<TAccountMessageSentEventData>;
  attestation: ReclaimEventAccountInstructionDataArgs["attestation"];
  destinationMessage: ReclaimEventAccountInstructionDataArgs["destinationMessage"];
};

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

  // Original accounts.
  const originalAccounts = {
    payee: { value: input.payee ?? null, isWritable: true },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    messageSentEventData: {
      value: input.messageSentEventData ?? null,
      isWritable: true,
    },
  };
  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.payee),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.messageSentEventData),
    ],
    data: getReclaimEventAccountInstructionDataEncoder().encode(
      args as ReclaimEventAccountInstructionDataArgs,
    ),
    programAddress,
  } as ReclaimEventAccountInstruction<
    TProgramAddress,
    TAccountPayee,
    TAccountMessageTransmitter,
    TAccountMessageSentEventData
  >);
}

export type ParsedReclaimEventAccountInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    /** rent SOL receiver, should match original rent payer */
    payee: TAccountMetas[0];
    messageTransmitter: TAccountMetas[1];
    messageSentEventData: TAccountMetas[2];
  };
  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 < 3) {
    // 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: {
      payee: getNextAccount(),
      messageTransmitter: getNextAccount(),
      messageSentEventData: getNextAccount(),
    },
    data: getReclaimEventAccountInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
