/**
 * 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,
  getAddressDecoder,
  getAddressEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getU64Decoder,
  getU64Encoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlySignerAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import { TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const DEPOSIT_FOR_BURN_WITH_CALLER_DISCRIMINATOR = new Uint8Array([
  167, 222, 19, 114, 85, 21, 14, 118,
]);

export function getDepositForBurnWithCallerDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    DEPOSIT_FOR_BURN_WITH_CALLER_DISCRIMINATOR,
  );
}

export type DepositForBurnWithCallerInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountOwner extends string | AccountMeta<string> = string,
  TAccountEventRentPayer extends string | AccountMeta<string> = string,
  TAccountSenderAuthorityPda extends string | AccountMeta<string> = string,
  TAccountBurnTokenAccount extends string | AccountMeta<string> = string,
  TAccountMessageTransmitter extends string | AccountMeta<string> = string,
  TAccountTokenMessenger extends string | AccountMeta<string> = string,
  TAccountRemoteTokenMessenger extends string | AccountMeta<string> = string,
  TAccountTokenMinter extends string | AccountMeta<string> = string,
  TAccountLocalToken extends string | AccountMeta<string> = string,
  TAccountBurnTokenMint extends string | AccountMeta<string> = string,
  TAccountMessageSentEventData extends string | AccountMeta<string> = string,
  TAccountMessageTransmitterProgram extends string | AccountMeta<string> =
    string,
  TAccountTokenMessengerMinterProgram extends string | AccountMeta<string> =
    string,
  TAccountTokenProgram extends string | AccountMeta<string> =
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountOwner extends string
        ? ReadonlySignerAccount<TAccountOwner> &
            AccountSignerMeta<TAccountOwner>
        : TAccountOwner,
      TAccountEventRentPayer extends string
        ? WritableSignerAccount<TAccountEventRentPayer> &
            AccountSignerMeta<TAccountEventRentPayer>
        : TAccountEventRentPayer,
      TAccountSenderAuthorityPda extends string
        ? ReadonlyAccount<TAccountSenderAuthorityPda>
        : TAccountSenderAuthorityPda,
      TAccountBurnTokenAccount extends string
        ? WritableAccount<TAccountBurnTokenAccount>
        : TAccountBurnTokenAccount,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountTokenMessenger extends string
        ? ReadonlyAccount<TAccountTokenMessenger>
        : TAccountTokenMessenger,
      TAccountRemoteTokenMessenger extends string
        ? ReadonlyAccount<TAccountRemoteTokenMessenger>
        : TAccountRemoteTokenMessenger,
      TAccountTokenMinter extends string
        ? ReadonlyAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountLocalToken extends string
        ? WritableAccount<TAccountLocalToken>
        : TAccountLocalToken,
      TAccountBurnTokenMint extends string
        ? WritableAccount<TAccountBurnTokenMint>
        : TAccountBurnTokenMint,
      TAccountMessageSentEventData extends string
        ? WritableSignerAccount<TAccountMessageSentEventData> &
            AccountSignerMeta<TAccountMessageSentEventData>
        : TAccountMessageSentEventData,
      TAccountMessageTransmitterProgram extends string
        ? ReadonlyAccount<TAccountMessageTransmitterProgram>
        : TAccountMessageTransmitterProgram,
      TAccountTokenMessengerMinterProgram extends string
        ? ReadonlyAccount<TAccountTokenMessengerMinterProgram>
        : TAccountTokenMessengerMinterProgram,
      TAccountTokenProgram extends string
        ? ReadonlyAccount<TAccountTokenProgram>
        : TAccountTokenProgram,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type DepositForBurnWithCallerInstructionData = {
  discriminator: ReadonlyUint8Array;
  amount: bigint;
  destinationDomain: number;
  mintRecipient: Address;
  destinationCaller: Address;
};

export type DepositForBurnWithCallerInstructionDataArgs = {
  amount: number | bigint;
  destinationDomain: number;
  mintRecipient: Address;
  destinationCaller: Address;
};

export function getDepositForBurnWithCallerInstructionDataEncoder(): FixedSizeEncoder<DepositForBurnWithCallerInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["amount", getU64Encoder()],
      ["destinationDomain", getU32Encoder()],
      ["mintRecipient", getAddressEncoder()],
      ["destinationCaller", getAddressEncoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: DEPOSIT_FOR_BURN_WITH_CALLER_DISCRIMINATOR,
    }),
  );
}

export function getDepositForBurnWithCallerInstructionDataDecoder(): FixedSizeDecoder<DepositForBurnWithCallerInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["amount", getU64Decoder()],
    ["destinationDomain", getU32Decoder()],
    ["mintRecipient", getAddressDecoder()],
    ["destinationCaller", getAddressDecoder()],
  ]);
}

export function getDepositForBurnWithCallerInstructionDataCodec(): FixedSizeCodec<
  DepositForBurnWithCallerInstructionDataArgs,
  DepositForBurnWithCallerInstructionData
> {
  return combineCodec(
    getDepositForBurnWithCallerInstructionDataEncoder(),
    getDepositForBurnWithCallerInstructionDataDecoder(),
  );
}

export type DepositForBurnWithCallerInput<
  TAccountOwner extends string = string,
  TAccountEventRentPayer extends string = string,
  TAccountSenderAuthorityPda extends string = string,
  TAccountBurnTokenAccount extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountRemoteTokenMessenger extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountBurnTokenMint extends string = string,
  TAccountMessageSentEventData extends string = string,
  TAccountMessageTransmitterProgram extends string = string,
  TAccountTokenMessengerMinterProgram extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  owner: TransactionSigner<TAccountOwner>;
  eventRentPayer: TransactionSigner<TAccountEventRentPayer>;
  senderAuthorityPda: Address<TAccountSenderAuthorityPda>;
  burnTokenAccount: Address<TAccountBurnTokenAccount>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  remoteTokenMessenger: Address<TAccountRemoteTokenMessenger>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  burnTokenMint: Address<TAccountBurnTokenMint>;
  messageSentEventData: TransactionSigner<TAccountMessageSentEventData>;
  messageTransmitterProgram: Address<TAccountMessageTransmitterProgram>;
  tokenMessengerMinterProgram: Address<TAccountTokenMessengerMinterProgram>;
  tokenProgram?: Address<TAccountTokenProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  amount: DepositForBurnWithCallerInstructionDataArgs["amount"];
  destinationDomain: DepositForBurnWithCallerInstructionDataArgs["destinationDomain"];
  mintRecipient: DepositForBurnWithCallerInstructionDataArgs["mintRecipient"];
  destinationCaller: DepositForBurnWithCallerInstructionDataArgs["destinationCaller"];
};

export function getDepositForBurnWithCallerInstruction<
  TAccountOwner extends string,
  TAccountEventRentPayer extends string,
  TAccountSenderAuthorityPda extends string,
  TAccountBurnTokenAccount extends string,
  TAccountMessageTransmitter extends string,
  TAccountTokenMessenger extends string,
  TAccountRemoteTokenMessenger extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountBurnTokenMint extends string,
  TAccountMessageSentEventData extends string,
  TAccountMessageTransmitterProgram extends string,
  TAccountTokenMessengerMinterProgram extends string,
  TAccountTokenProgram extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
>(
  input: DepositForBurnWithCallerInput<
    TAccountOwner,
    TAccountEventRentPayer,
    TAccountSenderAuthorityPda,
    TAccountBurnTokenAccount,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountBurnTokenMint,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): DepositForBurnWithCallerInstruction<
  TProgramAddress,
  TAccountOwner,
  TAccountEventRentPayer,
  TAccountSenderAuthorityPda,
  TAccountBurnTokenAccount,
  TAccountMessageTransmitter,
  TAccountTokenMessenger,
  TAccountRemoteTokenMessenger,
  TAccountTokenMinter,
  TAccountLocalToken,
  TAccountBurnTokenMint,
  TAccountMessageSentEventData,
  TAccountMessageTransmitterProgram,
  TAccountTokenMessengerMinterProgram,
  TAccountTokenProgram,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    owner: { value: input.owner ?? null, isWritable: false },
    eventRentPayer: { value: input.eventRentPayer ?? null, isWritable: true },
    senderAuthorityPda: {
      value: input.senderAuthorityPda ?? null,
      isWritable: false,
    },
    burnTokenAccount: {
      value: input.burnTokenAccount ?? null,
      isWritable: true,
    },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    remoteTokenMessenger: {
      value: input.remoteTokenMessenger ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    localToken: { value: input.localToken ?? null, isWritable: true },
    burnTokenMint: { value: input.burnTokenMint ?? null, isWritable: true },
    messageSentEventData: {
      value: input.messageSentEventData ?? null,
      isWritable: true,
    },
    messageTransmitterProgram: {
      value: input.messageTransmitterProgram ?? null,
      isWritable: false,
    },
    tokenMessengerMinterProgram: {
      value: input.tokenMessengerMinterProgram ?? null,
      isWritable: false,
    },
    tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
    systemProgram: { value: input.systemProgram ?? 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.tokenProgram.value) {
    accounts.tokenProgram.value =
      "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.owner),
      getAccountMeta(accounts.eventRentPayer),
      getAccountMeta(accounts.senderAuthorityPda),
      getAccountMeta(accounts.burnTokenAccount),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.remoteTokenMessenger),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.burnTokenMint),
      getAccountMeta(accounts.messageSentEventData),
      getAccountMeta(accounts.messageTransmitterProgram),
      getAccountMeta(accounts.tokenMessengerMinterProgram),
      getAccountMeta(accounts.tokenProgram),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getDepositForBurnWithCallerInstructionDataEncoder().encode(
      args as DepositForBurnWithCallerInstructionDataArgs,
    ),
    programAddress,
  } as DepositForBurnWithCallerInstruction<
    TProgramAddress,
    TAccountOwner,
    TAccountEventRentPayer,
    TAccountSenderAuthorityPda,
    TAccountBurnTokenAccount,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountBurnTokenMint,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedDepositForBurnWithCallerInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    owner: TAccountMetas[0];
    eventRentPayer: TAccountMetas[1];
    senderAuthorityPda: TAccountMetas[2];
    burnTokenAccount: TAccountMetas[3];
    messageTransmitter: TAccountMetas[4];
    tokenMessenger: TAccountMetas[5];
    remoteTokenMessenger: TAccountMetas[6];
    tokenMinter: TAccountMetas[7];
    localToken: TAccountMetas[8];
    burnTokenMint: TAccountMetas[9];
    messageSentEventData: TAccountMetas[10];
    messageTransmitterProgram: TAccountMetas[11];
    tokenMessengerMinterProgram: TAccountMetas[12];
    tokenProgram: TAccountMetas[13];
    systemProgram: TAccountMetas[14];
    eventAuthority: TAccountMetas[15];
    program: TAccountMetas[16];
  };
  data: DepositForBurnWithCallerInstructionData;
};

export function parseDepositForBurnWithCallerInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedDepositForBurnWithCallerInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 17) {
    // 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: {
      owner: getNextAccount(),
      eventRentPayer: getNextAccount(),
      senderAuthorityPda: getNextAccount(),
      burnTokenAccount: getNextAccount(),
      messageTransmitter: getNextAccount(),
      tokenMessenger: getNextAccount(),
      remoteTokenMessenger: getNextAccount(),
      tokenMinter: getNextAccount(),
      localToken: getNextAccount(),
      burnTokenMint: getNextAccount(),
      messageSentEventData: getNextAccount(),
      messageTransmitterProgram: getNextAccount(),
      tokenMessengerMinterProgram: getNextAccount(),
      tokenProgram: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getDepositForBurnWithCallerInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
