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

export const BURN_TOKEN_CUSTODY_DISCRIMINATOR = new Uint8Array([
  233, 136, 180, 175, 112, 41, 62, 71,
]);

export function getBurnTokenCustodyDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    BURN_TOKEN_CUSTODY_DISCRIMINATOR,
  );
}

export type BurnTokenCustodyInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountPayee 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,
  TAccountCustodyTokenMint extends string | AccountMeta<string> = string,
  TAccountTokenProgram extends string | AccountMeta<string> =
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountPayee extends string
        ? WritableSignerAccount<TAccountPayee> &
            AccountSignerMeta<TAccountPayee>
        : TAccountPayee,
      TAccountTokenController extends string
        ? ReadonlySignerAccount<TAccountTokenController> &
            AccountSignerMeta<TAccountTokenController>
        : TAccountTokenController,
      TAccountTokenMinter extends string
        ? ReadonlyAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountLocalToken extends string
        ? ReadonlyAccount<TAccountLocalToken>
        : TAccountLocalToken,
      TAccountCustodyTokenAccount extends string
        ? WritableAccount<TAccountCustodyTokenAccount>
        : TAccountCustodyTokenAccount,
      TAccountCustodyTokenMint extends string
        ? WritableAccount<TAccountCustodyTokenMint>
        : TAccountCustodyTokenMint,
      TAccountTokenProgram extends string
        ? ReadonlyAccount<TAccountTokenProgram>
        : TAccountTokenProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type BurnTokenCustodyInstructionData = {
  discriminator: ReadonlyUint8Array;
  amount: bigint;
};

export type BurnTokenCustodyInstructionDataArgs = { amount: number | bigint };

export function getBurnTokenCustodyInstructionDataEncoder(): FixedSizeEncoder<BurnTokenCustodyInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["amount", getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: BURN_TOKEN_CUSTODY_DISCRIMINATOR }),
  );
}

export function getBurnTokenCustodyInstructionDataDecoder(): FixedSizeDecoder<BurnTokenCustodyInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["amount", getU64Decoder()],
  ]);
}

export function getBurnTokenCustodyInstructionDataCodec(): FixedSizeCodec<
  BurnTokenCustodyInstructionDataArgs,
  BurnTokenCustodyInstructionData
> {
  return combineCodec(
    getBurnTokenCustodyInstructionDataEncoder(),
    getBurnTokenCustodyInstructionDataDecoder(),
  );
}

export type BurnTokenCustodyAsyncInput<
  TAccountPayee extends string = string,
  TAccountTokenController extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountCustodyTokenAccount extends string = string,
  TAccountCustodyTokenMint extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payee: TransactionSigner<TAccountPayee>;
  tokenController: TransactionSigner<TAccountTokenController>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  custodyTokenAccount: Address<TAccountCustodyTokenAccount>;
  custodyTokenMint: Address<TAccountCustodyTokenMint>;
  tokenProgram?: Address<TAccountTokenProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  amount: BurnTokenCustodyInstructionDataArgs["amount"];
};

export async function getBurnTokenCustodyInstructionAsync<
  TAccountPayee extends string,
  TAccountTokenController extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountCustodyTokenAccount extends string,
  TAccountCustodyTokenMint extends string,
  TAccountTokenProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: BurnTokenCustodyAsyncInput<
    TAccountPayee,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountCustodyTokenMint,
    TAccountTokenProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  BurnTokenCustodyInstruction<
    TProgramAddress,
    TAccountPayee,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountCustodyTokenMint,
    TAccountTokenProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payee: { value: input.payee ?? null, isWritable: true },
    tokenController: {
      value: input.tokenController ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    localToken: { value: input.localToken ?? null, isWritable: false },
    custodyTokenAccount: {
      value: input.custodyTokenAccount ?? null,
      isWritable: true,
    },
    custodyTokenMint: {
      value: input.custodyTokenMint ?? null,
      isWritable: true,
    },
    tokenProgram: { value: input.tokenProgram ?? 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.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.payee),
      getAccountMeta(accounts.tokenController),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.custodyTokenAccount),
      getAccountMeta(accounts.custodyTokenMint),
      getAccountMeta(accounts.tokenProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getBurnTokenCustodyInstructionDataEncoder().encode(
      args as BurnTokenCustodyInstructionDataArgs,
    ),
    programAddress,
  } as BurnTokenCustodyInstruction<
    TProgramAddress,
    TAccountPayee,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountCustodyTokenMint,
    TAccountTokenProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type BurnTokenCustodyInput<
  TAccountPayee extends string = string,
  TAccountTokenController extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountCustodyTokenAccount extends string = string,
  TAccountCustodyTokenMint extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payee: TransactionSigner<TAccountPayee>;
  tokenController: TransactionSigner<TAccountTokenController>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  custodyTokenAccount: Address<TAccountCustodyTokenAccount>;
  custodyTokenMint: Address<TAccountCustodyTokenMint>;
  tokenProgram?: Address<TAccountTokenProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  amount: BurnTokenCustodyInstructionDataArgs["amount"];
};

export function getBurnTokenCustodyInstruction<
  TAccountPayee extends string,
  TAccountTokenController extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountCustodyTokenAccount extends string,
  TAccountCustodyTokenMint extends string,
  TAccountTokenProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: BurnTokenCustodyInput<
    TAccountPayee,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountCustodyTokenMint,
    TAccountTokenProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): BurnTokenCustodyInstruction<
  TProgramAddress,
  TAccountPayee,
  TAccountTokenController,
  TAccountTokenMinter,
  TAccountLocalToken,
  TAccountCustodyTokenAccount,
  TAccountCustodyTokenMint,
  TAccountTokenProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payee: { value: input.payee ?? null, isWritable: true },
    tokenController: {
      value: input.tokenController ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    localToken: { value: input.localToken ?? null, isWritable: false },
    custodyTokenAccount: {
      value: input.custodyTokenAccount ?? null,
      isWritable: true,
    },
    custodyTokenMint: {
      value: input.custodyTokenMint ?? null,
      isWritable: true,
    },
    tokenProgram: { value: input.tokenProgram ?? 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">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.payee),
      getAccountMeta(accounts.tokenController),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.custodyTokenAccount),
      getAccountMeta(accounts.custodyTokenMint),
      getAccountMeta(accounts.tokenProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getBurnTokenCustodyInstructionDataEncoder().encode(
      args as BurnTokenCustodyInstructionDataArgs,
    ),
    programAddress,
  } as BurnTokenCustodyInstruction<
    TProgramAddress,
    TAccountPayee,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCustodyTokenAccount,
    TAccountCustodyTokenMint,
    TAccountTokenProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedBurnTokenCustodyInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    payee: TAccountMetas[0];
    tokenController: TAccountMetas[1];
    tokenMinter: TAccountMetas[2];
    localToken: TAccountMetas[3];
    custodyTokenAccount: TAccountMetas[4];
    custodyTokenMint: TAccountMetas[5];
    tokenProgram: TAccountMetas[6];
    eventAuthority: TAccountMetas[7];
    program: TAccountMetas[8];
  };
  data: BurnTokenCustodyInstructionData;
};

export function parseBurnTokenCustodyInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedBurnTokenCustodyInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 9) {
    // 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(),
      tokenController: getNextAccount(),
      tokenMinter: getNextAccount(),
      localToken: getNextAccount(),
      custodyTokenAccount: getNextAccount(),
      custodyTokenMint: getNextAccount(),
      tokenProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getBurnTokenCustodyInstructionDataDecoder().decode(instruction.data),
  };
}
