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

export const ENABLE_ATTESTER_DISCRIMINATOR = new Uint8Array([
  2, 11, 193, 115, 5, 148, 4, 198,
]);

export function getEnableAttesterDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    ENABLE_ATTESTER_DISCRIMINATOR,
  );
}

export type EnableAttesterInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountAttesterManager extends string | AccountMeta<string> = string,
  TAccountMessageTransmitter 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,
      TAccountAttesterManager extends string
        ? ReadonlySignerAccount<TAccountAttesterManager> &
            AccountSignerMeta<TAccountAttesterManager>
        : TAccountAttesterManager,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type EnableAttesterInstructionData = {
  discriminator: ReadonlyUint8Array;
  newAttester: Address;
};

export type EnableAttesterInstructionDataArgs = { newAttester: Address };

export function getEnableAttesterInstructionDataEncoder(): FixedSizeEncoder<EnableAttesterInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["newAttester", getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: ENABLE_ATTESTER_DISCRIMINATOR }),
  );
}

export function getEnableAttesterInstructionDataDecoder(): FixedSizeDecoder<EnableAttesterInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["newAttester", getAddressDecoder()],
  ]);
}

export function getEnableAttesterInstructionDataCodec(): FixedSizeCodec<
  EnableAttesterInstructionDataArgs,
  EnableAttesterInstructionData
> {
  return combineCodec(
    getEnableAttesterInstructionDataEncoder(),
    getEnableAttesterInstructionDataDecoder(),
  );
}

export type EnableAttesterInput<
  TAccountPayer extends string = string,
  TAccountAttesterManager extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  payer: TransactionSigner<TAccountPayer>;
  attesterManager: TransactionSigner<TAccountAttesterManager>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  newAttester: EnableAttesterInstructionDataArgs["newAttester"];
};

export function getEnableAttesterInstruction<
  TAccountPayer extends string,
  TAccountAttesterManager extends string,
  TAccountMessageTransmitter extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
>(
  input: EnableAttesterInput<
    TAccountPayer,
    TAccountAttesterManager,
    TAccountMessageTransmitter,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): EnableAttesterInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountAttesterManager,
  TAccountMessageTransmitter,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MESSAGE_TRANSMITTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    attesterManager: {
      value: input.attesterManager ?? null,
      isWritable: false,
    },
    messageTransmitter: {
      value: input.messageTransmitter ?? 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.attesterManager),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getEnableAttesterInstructionDataEncoder().encode(
      args as EnableAttesterInstructionDataArgs,
    ),
    programAddress,
  } as EnableAttesterInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountAttesterManager,
    TAccountMessageTransmitter,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedEnableAttesterInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    payer: TAccountMetas[0];
    attesterManager: TAccountMetas[1];
    messageTransmitter: TAccountMetas[2];
    systemProgram: TAccountMetas[3];
    eventAuthority: TAccountMetas[4];
    program: TAccountMetas[5];
  };
  data: EnableAttesterInstructionData;
};

export function parseEnableAttesterInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedEnableAttesterInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 6) {
    // 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(),
      attesterManager: getNextAccount(),
      messageTransmitter: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getEnableAttesterInstructionDataDecoder().decode(instruction.data),
  };
}
