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

export const ACCEPT_OWNERSHIP_DISCRIMINATOR = new Uint8Array([
  172, 23, 43, 13, 238, 213, 85, 150,
]);

export function getAcceptOwnershipDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    ACCEPT_OWNERSHIP_DISCRIMINATOR,
  );
}

export type AcceptOwnershipInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
  TAccountPendingOwner 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<
    [
      TAccountPendingOwner extends string
        ? ReadonlySignerAccount<TAccountPendingOwner> &
            AccountSignerMeta<TAccountPendingOwner>
        : TAccountPendingOwner,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type AcceptOwnershipInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type AcceptOwnershipInstructionDataArgs = {};

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

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

export function getAcceptOwnershipInstructionDataCodec(): FixedSizeCodec<
  AcceptOwnershipInstructionDataArgs,
  AcceptOwnershipInstructionData
> {
  return combineCodec(
    getAcceptOwnershipInstructionDataEncoder(),
    getAcceptOwnershipInstructionDataDecoder(),
  );
}

export type AcceptOwnershipAsyncInput<
  TAccountPendingOwner extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  pendingOwner: TransactionSigner<TAccountPendingOwner>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
};

export async function getAcceptOwnershipInstructionAsync<
  TAccountPendingOwner extends string,
  TAccountMessageTransmitter extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
>(
  input: AcceptOwnershipAsyncInput<
    TAccountPendingOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  AcceptOwnershipInstruction<
    TProgramAddress,
    TAccountPendingOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    pendingOwner: { value: input.pendingOwner ?? 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
  >;

  // Resolve default values.
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.pendingOwner),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getAcceptOwnershipInstructionDataEncoder().encode({}),
    programAddress,
  } as AcceptOwnershipInstruction<
    TProgramAddress,
    TAccountPendingOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type AcceptOwnershipInput<
  TAccountPendingOwner extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  pendingOwner: TransactionSigner<TAccountPendingOwner>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
};

export function getAcceptOwnershipInstruction<
  TAccountPendingOwner extends string,
  TAccountMessageTransmitter extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
>(
  input: AcceptOwnershipInput<
    TAccountPendingOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): AcceptOwnershipInstruction<
  TProgramAddress,
  TAccountPendingOwner,
  TAccountMessageTransmitter,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    pendingOwner: { value: input.pendingOwner ?? 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
  >;

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.pendingOwner),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getAcceptOwnershipInstructionDataEncoder().encode({}),
    programAddress,
  } as AcceptOwnershipInstruction<
    TProgramAddress,
    TAccountPendingOwner,
    TAccountMessageTransmitter,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedAcceptOwnershipInstruction<
  TProgram extends string = typeof MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    pendingOwner: TAccountMetas[0];
    messageTransmitter: TAccountMetas[1];
    eventAuthority: TAccountMetas[2];
    program: TAccountMetas[3];
  };
  data: AcceptOwnershipInstructionData;
};

export function parseAcceptOwnershipInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedAcceptOwnershipInstruction<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: {
      pendingOwner: getNextAccount(),
      messageTransmitter: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getAcceptOwnershipInstructionDataDecoder().decode(instruction.data),
  };
}
