/**
 * 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,
  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 { findEventAuthorityPda } from "../pdas";
import { TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const ADD_REMOTE_TOKEN_MESSENGER_DISCRIMINATOR = new Uint8Array([
  12, 149, 172, 165, 111, 202, 24, 33,
]);

export function getAddRemoteTokenMessengerDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    ADD_REMOTE_TOKEN_MESSENGER_DISCRIMINATOR,
  );
}

export type AddRemoteTokenMessengerInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountOwner extends string | AccountMeta<string> = string,
  TAccountTokenMessenger extends string | AccountMeta<string> = string,
  TAccountRemoteTokenMessenger extends string | AccountMeta<string> = string,
  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<
    [
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountOwner extends string
        ? ReadonlySignerAccount<TAccountOwner> &
            AccountSignerMeta<TAccountOwner>
        : TAccountOwner,
      TAccountTokenMessenger extends string
        ? ReadonlyAccount<TAccountTokenMessenger>
        : TAccountTokenMessenger,
      TAccountRemoteTokenMessenger extends string
        ? WritableAccount<TAccountRemoteTokenMessenger>
        : TAccountRemoteTokenMessenger,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type AddRemoteTokenMessengerInstructionData = {
  discriminator: ReadonlyUint8Array;
  domain: number;
  tokenMessenger: Address;
};

export type AddRemoteTokenMessengerInstructionDataArgs = {
  domain: number;
  tokenMessenger: Address;
};

export function getAddRemoteTokenMessengerInstructionDataEncoder(): FixedSizeEncoder<AddRemoteTokenMessengerInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["domain", getU32Encoder()],
      ["tokenMessenger", getAddressEncoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: ADD_REMOTE_TOKEN_MESSENGER_DISCRIMINATOR,
    }),
  );
}

export function getAddRemoteTokenMessengerInstructionDataDecoder(): FixedSizeDecoder<AddRemoteTokenMessengerInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["domain", getU32Decoder()],
    ["tokenMessenger", getAddressDecoder()],
  ]);
}

export function getAddRemoteTokenMessengerInstructionDataCodec(): FixedSizeCodec<
  AddRemoteTokenMessengerInstructionDataArgs,
  AddRemoteTokenMessengerInstructionData
> {
  return combineCodec(
    getAddRemoteTokenMessengerInstructionDataEncoder(),
    getAddRemoteTokenMessengerInstructionDataDecoder(),
  );
}

export type AddRemoteTokenMessengerAsyncInput<
  TAccountPayer extends string = string,
  TAccountOwner extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountRemoteTokenMessenger extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  owner: TransactionSigner<TAccountOwner>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  remoteTokenMessenger: Address<TAccountRemoteTokenMessenger>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  domain: AddRemoteTokenMessengerInstructionDataArgs["domain"];
  tokenMessengerArg: AddRemoteTokenMessengerInstructionDataArgs["tokenMessenger"];
};

export async function getAddRemoteTokenMessengerInstructionAsync<
  TAccountPayer extends string,
  TAccountOwner extends string,
  TAccountTokenMessenger extends string,
  TAccountRemoteTokenMessenger extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: AddRemoteTokenMessengerAsyncInput<
    TAccountPayer,
    TAccountOwner,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  AddRemoteTokenMessengerInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountOwner,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    owner: { value: input.owner ?? null, isWritable: false },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    remoteTokenMessenger: {
      value: input.remoteTokenMessenger ?? null,
      isWritable: true,
    },
    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, tokenMessenger: input.tokenMessengerArg };

  // Resolve default values.
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.owner),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.remoteTokenMessenger),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getAddRemoteTokenMessengerInstructionDataEncoder().encode(
      args as AddRemoteTokenMessengerInstructionDataArgs,
    ),
    programAddress,
  } as AddRemoteTokenMessengerInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountOwner,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type AddRemoteTokenMessengerInput<
  TAccountPayer extends string = string,
  TAccountOwner extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountRemoteTokenMessenger extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  owner: TransactionSigner<TAccountOwner>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  remoteTokenMessenger: Address<TAccountRemoteTokenMessenger>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  domain: AddRemoteTokenMessengerInstructionDataArgs["domain"];
  tokenMessengerArg: AddRemoteTokenMessengerInstructionDataArgs["tokenMessenger"];
};

export function getAddRemoteTokenMessengerInstruction<
  TAccountPayer extends string,
  TAccountOwner extends string,
  TAccountTokenMessenger extends string,
  TAccountRemoteTokenMessenger extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: AddRemoteTokenMessengerInput<
    TAccountPayer,
    TAccountOwner,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): AddRemoteTokenMessengerInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountOwner,
  TAccountTokenMessenger,
  TAccountRemoteTokenMessenger,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    owner: { value: input.owner ?? null, isWritable: false },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    remoteTokenMessenger: {
      value: input.remoteTokenMessenger ?? null,
      isWritable: true,
    },
    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, tokenMessenger: input.tokenMessengerArg };

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.owner),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.remoteTokenMessenger),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getAddRemoteTokenMessengerInstructionDataEncoder().encode(
      args as AddRemoteTokenMessengerInstructionDataArgs,
    ),
    programAddress,
  } as AddRemoteTokenMessengerInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountOwner,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedAddRemoteTokenMessengerInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    payer: TAccountMetas[0];
    owner: TAccountMetas[1];
    tokenMessenger: TAccountMetas[2];
    remoteTokenMessenger: TAccountMetas[3];
    systemProgram: TAccountMetas[4];
    eventAuthority: TAccountMetas[5];
    program: TAccountMetas[6];
  };
  data: AddRemoteTokenMessengerInstructionData;
};

export function parseAddRemoteTokenMessengerInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedAddRemoteTokenMessengerInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 7) {
    // 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: {
      payer: getNextAccount(),
      owner: getNextAccount(),
      tokenMessenger: getNextAccount(),
      remoteTokenMessenger: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getAddRemoteTokenMessengerInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
