/**
 * 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,
  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 DENYLIST_ACCOUNT_DISCRIMINATOR = new Uint8Array([
  101, 116, 197, 112, 81, 249, 75, 194,
]);

export function getDenylistAccountDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    DENYLIST_ACCOUNT_DISCRIMINATOR,
  );
}

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

export type DenylistAccountInstructionData = {
  discriminator: ReadonlyUint8Array;
  account: Address;
};

export type DenylistAccountInstructionDataArgs = { account: Address };

export function getDenylistAccountInstructionDataEncoder(): FixedSizeEncoder<DenylistAccountInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["account", getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: DENYLIST_ACCOUNT_DISCRIMINATOR }),
  );
}

export function getDenylistAccountInstructionDataDecoder(): FixedSizeDecoder<DenylistAccountInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["account", getAddressDecoder()],
  ]);
}

export function getDenylistAccountInstructionDataCodec(): FixedSizeCodec<
  DenylistAccountInstructionDataArgs,
  DenylistAccountInstructionData
> {
  return combineCodec(
    getDenylistAccountInstructionDataEncoder(),
    getDenylistAccountInstructionDataDecoder(),
  );
}

export type DenylistAccountAsyncInput<
  TAccountPayer extends string = string,
  TAccountDenylister extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountDenylistAccount extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  denylister: TransactionSigner<TAccountDenylister>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  denylistAccount: Address<TAccountDenylistAccount>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  account: DenylistAccountInstructionDataArgs["account"];
};

export async function getDenylistAccountInstructionAsync<
  TAccountPayer extends string,
  TAccountDenylister extends string,
  TAccountTokenMessenger extends string,
  TAccountDenylistAccount extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: DenylistAccountAsyncInput<
    TAccountPayer,
    TAccountDenylister,
    TAccountTokenMessenger,
    TAccountDenylistAccount,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  DenylistAccountInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountDenylister,
    TAccountTokenMessenger,
    TAccountDenylistAccount,
    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 },
    denylister: { value: input.denylister ?? null, isWritable: false },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    denylistAccount: { value: input.denylistAccount ?? 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.denylister),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.denylistAccount),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getDenylistAccountInstructionDataEncoder().encode(
      args as DenylistAccountInstructionDataArgs,
    ),
    programAddress,
  } as DenylistAccountInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountDenylister,
    TAccountTokenMessenger,
    TAccountDenylistAccount,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type DenylistAccountInput<
  TAccountPayer extends string = string,
  TAccountDenylister extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountDenylistAccount extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  denylister: TransactionSigner<TAccountDenylister>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  denylistAccount: Address<TAccountDenylistAccount>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  account: DenylistAccountInstructionDataArgs["account"];
};

export function getDenylistAccountInstruction<
  TAccountPayer extends string,
  TAccountDenylister extends string,
  TAccountTokenMessenger extends string,
  TAccountDenylistAccount extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: DenylistAccountInput<
    TAccountPayer,
    TAccountDenylister,
    TAccountTokenMessenger,
    TAccountDenylistAccount,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): DenylistAccountInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountDenylister,
  TAccountTokenMessenger,
  TAccountDenylistAccount,
  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 },
    denylister: { value: input.denylister ?? null, isWritable: false },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    denylistAccount: { value: input.denylistAccount ?? 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.denylister),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.denylistAccount),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getDenylistAccountInstructionDataEncoder().encode(
      args as DenylistAccountInstructionDataArgs,
    ),
    programAddress,
  } as DenylistAccountInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountDenylister,
    TAccountTokenMessenger,
    TAccountDenylistAccount,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

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

export function parseDenylistAccountInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedDenylistAccountInstruction<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(),
      denylister: getNextAccount(),
      tokenMessenger: getNextAccount(),
      denylistAccount: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getDenylistAccountInstructionDataDecoder().decode(instruction.data),
  };
}
