/**
 * 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,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  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 ADD_LOCAL_TOKEN_DISCRIMINATOR = new Uint8Array([
  213, 199, 205, 18, 98, 124, 73, 198,
]);

export function getAddLocalTokenDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    ADD_LOCAL_TOKEN_DISCRIMINATOR,
  );
}

export type AddLocalTokenInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountTokenController extends string | AccountMeta<string> = string,
  TAccountTokenMinter extends string | AccountMeta<string> = string,
  TAccountLocalToken extends string | AccountMeta<string> = string,
  TAccountCustodyTokenAccount extends string | AccountMeta<string> = string,
  TAccountLocalTokenMint 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<
    [
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountTokenController extends string
        ? ReadonlySignerAccount<TAccountTokenController> &
            AccountSignerMeta<TAccountTokenController>
        : TAccountTokenController,
      TAccountTokenMinter extends string
        ? ReadonlyAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountLocalToken extends string
        ? WritableAccount<TAccountLocalToken>
        : TAccountLocalToken,
      TAccountCustodyTokenAccount extends string
        ? WritableAccount<TAccountCustodyTokenAccount>
        : TAccountCustodyTokenAccount,
      TAccountLocalTokenMint extends string
        ? ReadonlyAccount<TAccountLocalTokenMint>
        : TAccountLocalTokenMint,
      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 AddLocalTokenInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type AddLocalTokenInstructionDataArgs = {};

export function getAddLocalTokenInstructionDataEncoder(): FixedSizeEncoder<AddLocalTokenInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]),
    (value) => ({ ...value, discriminator: ADD_LOCAL_TOKEN_DISCRIMINATOR }),
  );
}

export function getAddLocalTokenInstructionDataDecoder(): FixedSizeDecoder<AddLocalTokenInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
  ]);
}

export function getAddLocalTokenInstructionDataCodec(): FixedSizeCodec<
  AddLocalTokenInstructionDataArgs,
  AddLocalTokenInstructionData
> {
  return combineCodec(
    getAddLocalTokenInstructionDataEncoder(),
    getAddLocalTokenInstructionDataDecoder(),
  );
}

export type AddLocalTokenInput<
  TAccountPayer extends string = string,
  TAccountTokenController extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountCustodyTokenAccount extends string = string,
  TAccountLocalTokenMint extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  tokenController: TransactionSigner<TAccountTokenController>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  custodyTokenAccount: Address<TAccountCustodyTokenAccount>;
  localTokenMint: Address<TAccountLocalTokenMint>;
  tokenProgram?: Address<TAccountTokenProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
};

export function getAddLocalTokenInstruction<
  TAccountPayer extends string,
  TAccountTokenController extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountCustodyTokenAccount extends string,
  TAccountLocalTokenMint extends string,
  TAccountTokenProgram extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
>(
  input: AddLocalTokenInput<
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountLocalTokenMint,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): AddLocalTokenInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountTokenController,
  TAccountTokenMinter,
  TAccountLocalToken,
  TAccountCustodyTokenAccount,
  TAccountLocalTokenMint,
  TAccountTokenProgram,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_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 },
    localToken: { value: input.localToken ?? null, isWritable: true },
    custodyTokenAccount: {
      value: input.custodyTokenAccount ?? null,
      isWritable: true,
    },
    localTokenMint: { value: input.localTokenMint ?? 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
  >;

  // 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.payer),
      getAccountMeta(accounts.tokenController),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.custodyTokenAccount),
      getAccountMeta(accounts.localTokenMint),
      getAccountMeta(accounts.tokenProgram),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getAddLocalTokenInstructionDataEncoder().encode({}),
    programAddress,
  } as AddLocalTokenInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountLocalTokenMint,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedAddLocalTokenInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    payer: TAccountMetas[0];
    tokenController: TAccountMetas[1];
    tokenMinter: TAccountMetas[2];
    localToken: TAccountMetas[3];
    custodyTokenAccount: TAccountMetas[4];
    localTokenMint: TAccountMetas[5];
    tokenProgram: TAccountMetas[6];
    systemProgram: TAccountMetas[7];
    eventAuthority: TAccountMetas[8];
    program: TAccountMetas[9];
  };
  data: AddLocalTokenInstructionData;
};

export function parseAddLocalTokenInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedAddLocalTokenInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 10) {
    // 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(),
      localToken: getNextAccount(),
      custodyTokenAccount: getNextAccount(),
      localTokenMint: getNextAccount(),
      tokenProgram: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getAddLocalTokenInstructionDataDecoder().decode(instruction.data),
  };
}
