/**
 * 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 LINK_TOKEN_PAIR_DISCRIMINATOR = new Uint8Array([
  68, 162, 24, 104, 125, 46, 130, 12,
]);

export function getLinkTokenPairDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    LINK_TOKEN_PAIR_DISCRIMINATOR,
  );
}

export type LinkTokenPairInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountTokenController extends string | AccountMeta<string> = string,
  TAccountTokenMinter extends string | AccountMeta<string> = string,
  TAccountTokenPair 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,
      TAccountTokenController extends string
        ? ReadonlySignerAccount<TAccountTokenController> &
            AccountSignerMeta<TAccountTokenController>
        : TAccountTokenController,
      TAccountTokenMinter extends string
        ? ReadonlyAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountTokenPair extends string
        ? WritableAccount<TAccountTokenPair>
        : TAccountTokenPair,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type LinkTokenPairInstructionData = {
  discriminator: ReadonlyUint8Array;
  localToken: Address;
  remoteDomain: number;
  remoteToken: Address;
};

export type LinkTokenPairInstructionDataArgs = {
  localToken: Address;
  remoteDomain: number;
  remoteToken: Address;
};

export function getLinkTokenPairInstructionDataEncoder(): FixedSizeEncoder<LinkTokenPairInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["localToken", getAddressEncoder()],
      ["remoteDomain", getU32Encoder()],
      ["remoteToken", getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: LINK_TOKEN_PAIR_DISCRIMINATOR }),
  );
}

export function getLinkTokenPairInstructionDataDecoder(): FixedSizeDecoder<LinkTokenPairInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["localToken", getAddressDecoder()],
    ["remoteDomain", getU32Decoder()],
    ["remoteToken", getAddressDecoder()],
  ]);
}

export function getLinkTokenPairInstructionDataCodec(): FixedSizeCodec<
  LinkTokenPairInstructionDataArgs,
  LinkTokenPairInstructionData
> {
  return combineCodec(
    getLinkTokenPairInstructionDataEncoder(),
    getLinkTokenPairInstructionDataDecoder(),
  );
}

export type LinkTokenPairAsyncInput<
  TAccountPayer extends string = string,
  TAccountTokenController extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountTokenPair extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  tokenController: TransactionSigner<TAccountTokenController>;
  tokenMinter: Address<TAccountTokenMinter>;
  tokenPair: Address<TAccountTokenPair>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  localToken: LinkTokenPairInstructionDataArgs["localToken"];
  remoteDomain: LinkTokenPairInstructionDataArgs["remoteDomain"];
  remoteToken: LinkTokenPairInstructionDataArgs["remoteToken"];
};

export async function getLinkTokenPairInstructionAsync<
  TAccountPayer extends string,
  TAccountTokenController extends string,
  TAccountTokenMinter extends string,
  TAccountTokenPair extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: LinkTokenPairAsyncInput<
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountTokenPair,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  LinkTokenPairInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountTokenPair,
    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 },
    tokenController: {
      value: input.tokenController ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    tokenPair: { value: input.tokenPair ?? 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 };

  // 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.tokenController),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.tokenPair),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getLinkTokenPairInstructionDataEncoder().encode(
      args as LinkTokenPairInstructionDataArgs,
    ),
    programAddress,
  } as LinkTokenPairInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountTokenPair,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type LinkTokenPairInput<
  TAccountPayer extends string = string,
  TAccountTokenController extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountTokenPair extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  tokenController: TransactionSigner<TAccountTokenController>;
  tokenMinter: Address<TAccountTokenMinter>;
  tokenPair: Address<TAccountTokenPair>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  localToken: LinkTokenPairInstructionDataArgs["localToken"];
  remoteDomain: LinkTokenPairInstructionDataArgs["remoteDomain"];
  remoteToken: LinkTokenPairInstructionDataArgs["remoteToken"];
};

export function getLinkTokenPairInstruction<
  TAccountPayer extends string,
  TAccountTokenController extends string,
  TAccountTokenMinter extends string,
  TAccountTokenPair extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: LinkTokenPairInput<
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountTokenPair,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): LinkTokenPairInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountTokenController,
  TAccountTokenMinter,
  TAccountTokenPair,
  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 },
    tokenController: {
      value: input.tokenController ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    tokenPair: { value: input.tokenPair ?? 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 };

  // 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.tokenController),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.tokenPair),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getLinkTokenPairInstructionDataEncoder().encode(
      args as LinkTokenPairInstructionDataArgs,
    ),
    programAddress,
  } as LinkTokenPairInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountTokenPair,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedLinkTokenPairInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    payer: TAccountMetas[0];
    tokenController: TAccountMetas[1];
    tokenMinter: TAccountMetas[2];
    tokenPair: TAccountMetas[3];
    systemProgram: TAccountMetas[4];
    eventAuthority: TAccountMetas[5];
    program: TAccountMetas[6];
  };
  data: LinkTokenPairInstructionData;
};

export function parseLinkTokenPairInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedLinkTokenPairInstruction<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(),
      tokenController: getNextAccount(),
      tokenMinter: getNextAccount(),
      tokenPair: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getLinkTokenPairInstructionDataDecoder().decode(instruction.data),
  };
}
