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

export const UPDATE_ATTESTER_MANAGER_DISCRIMINATOR = new Uint8Array([
  175, 245, 178, 104, 85, 179, 71, 16,
]);

export function getUpdateAttesterManagerDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    UPDATE_ATTESTER_MANAGER_DISCRIMINATOR,
  );
}

export type UpdateAttesterManagerInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountOwner extends string | AccountMeta<string> = string,
  TAccountMessageTransmitter extends string | AccountMeta<string> = string,
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountOwner extends string
        ? ReadonlySignerAccount<TAccountOwner> &
            AccountSignerMeta<TAccountOwner>
        : TAccountOwner,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type UpdateAttesterManagerInstructionData = {
  discriminator: ReadonlyUint8Array;
  newAttesterManager: Address;
};

export type UpdateAttesterManagerInstructionDataArgs = {
  newAttesterManager: Address;
};

export function getUpdateAttesterManagerInstructionDataEncoder(): FixedSizeEncoder<UpdateAttesterManagerInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["newAttesterManager", getAddressEncoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: UPDATE_ATTESTER_MANAGER_DISCRIMINATOR,
    }),
  );
}

export function getUpdateAttesterManagerInstructionDataDecoder(): FixedSizeDecoder<UpdateAttesterManagerInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["newAttesterManager", getAddressDecoder()],
  ]);
}

export function getUpdateAttesterManagerInstructionDataCodec(): FixedSizeCodec<
  UpdateAttesterManagerInstructionDataArgs,
  UpdateAttesterManagerInstructionData
> {
  return combineCodec(
    getUpdateAttesterManagerInstructionDataEncoder(),
    getUpdateAttesterManagerInstructionDataDecoder(),
  );
}

export type UpdateAttesterManagerInput<
  TAccountOwner extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  owner: TransactionSigner<TAccountOwner>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  newAttesterManager: UpdateAttesterManagerInstructionDataArgs["newAttesterManager"];
};

export function getUpdateAttesterManagerInstruction<
  TAccountOwner extends string,
  TAccountMessageTransmitter extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
>(
  input: UpdateAttesterManagerInput<
    TAccountOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): UpdateAttesterManagerInstruction<
  TProgramAddress,
  TAccountOwner,
  TAccountMessageTransmitter,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MESSAGE_TRANSMITTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    owner: { value: input.owner ?? null, isWritable: false },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    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 };

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.owner),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getUpdateAttesterManagerInstructionDataEncoder().encode(
      args as UpdateAttesterManagerInstructionDataArgs,
    ),
    programAddress,
  } as UpdateAttesterManagerInstruction<
    TProgramAddress,
    TAccountOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedUpdateAttesterManagerInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    owner: TAccountMetas[0];
    messageTransmitter: TAccountMetas[1];
    eventAuthority: TAccountMetas[2];
    program: TAccountMetas[3];
  };
  data: UpdateAttesterManagerInstructionData;
};

export function parseUpdateAttesterManagerInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedUpdateAttesterManagerInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 4) {
    // 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: {
      owner: getNextAccount(),
      messageTransmitter: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getUpdateAttesterManagerInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
