/**
 * 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,
  getStructDecoder,
  getStructEncoder,
  getU8Decoder,
  getU8Encoder,
  SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS,
  SolanaError,
  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 {
  getAccountMetaFactory,
  type ResolvedInstructionAccount,
} from "@solana/kit/program-client-core";
import { findEventAuthorityPda } from "../pdas";
import { PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS } from "../programs";

export const SET_NEW_ADMIN_DISCRIMINATOR = 5;

export function getSetNewAdminDiscriminatorBytes(): ReadonlyUint8Array {
  return getU8Encoder().encode(SET_NEW_ADMIN_DISCRIMINATOR);
}

export type SetNewAdminInstruction<
  TProgram extends string =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountCurrentAdmin extends string | AccountMeta<string> = string,
  TAccountInstance extends string | AccountMeta<string> = string,
  TAccountNewAdmin extends string | AccountMeta<string> = string,
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountPrivateChannelEscrowProgram extends string | AccountMeta<string> =
    "9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountCurrentAdmin extends string
        ? ReadonlySignerAccount<TAccountCurrentAdmin> &
            AccountSignerMeta<TAccountCurrentAdmin>
        : TAccountCurrentAdmin,
      TAccountInstance extends string
        ? WritableAccount<TAccountInstance>
        : TAccountInstance,
      TAccountNewAdmin extends string
        ? ReadonlySignerAccount<TAccountNewAdmin> &
            AccountSignerMeta<TAccountNewAdmin>
        : TAccountNewAdmin,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountPrivateChannelEscrowProgram extends string
        ? ReadonlyAccount<TAccountPrivateChannelEscrowProgram>
        : TAccountPrivateChannelEscrowProgram,
      ...TRemainingAccounts,
    ]
  >;

export type SetNewAdminInstructionData = { discriminator: number };

export type SetNewAdminInstructionDataArgs = {};

export function getSetNewAdminInstructionDataEncoder(): FixedSizeEncoder<SetNewAdminInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([["discriminator", getU8Encoder()]]),
    (value) => ({ ...value, discriminator: SET_NEW_ADMIN_DISCRIMINATOR }),
  );
}

export function getSetNewAdminInstructionDataDecoder(): FixedSizeDecoder<SetNewAdminInstructionData> {
  return getStructDecoder([["discriminator", getU8Decoder()]]);
}

export function getSetNewAdminInstructionDataCodec(): FixedSizeCodec<
  SetNewAdminInstructionDataArgs,
  SetNewAdminInstructionData
> {
  return combineCodec(
    getSetNewAdminInstructionDataEncoder(),
    getSetNewAdminInstructionDataDecoder(),
  );
}

export type SetNewAdminAsyncInput<
  TAccountPayer extends string = string,
  TAccountCurrentAdmin extends string = string,
  TAccountInstance extends string = string,
  TAccountNewAdmin extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountPrivateChannelEscrowProgram extends string = string,
> = {
  /** Transaction fee payer */
  payer: TransactionSigner<TAccountPayer>;
  /** Current admin of Instance */
  currentAdmin: TransactionSigner<TAccountCurrentAdmin>;
  /** Instance PDA to update admin */
  instance: Address<TAccountInstance>;
  /** New admin public key */
  newAdmin: TransactionSigner<TAccountNewAdmin>;
  /** Event authority PDA for emitting events */
  eventAuthority?: Address<TAccountEventAuthority>;
  /** Current program for CPI */
  privateChannelEscrowProgram?: Address<TAccountPrivateChannelEscrowProgram>;
};

export async function getSetNewAdminInstructionAsync<
  TAccountPayer extends string,
  TAccountCurrentAdmin extends string,
  TAccountInstance extends string,
  TAccountNewAdmin extends string,
  TAccountEventAuthority extends string,
  TAccountPrivateChannelEscrowProgram extends string,
  TProgramAddress extends Address =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
>(
  input: SetNewAdminAsyncInput<
    TAccountPayer,
    TAccountCurrentAdmin,
    TAccountInstance,
    TAccountNewAdmin,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  SetNewAdminInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountCurrentAdmin,
    TAccountInstance,
    TAccountNewAdmin,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    currentAdmin: { value: input.currentAdmin ?? null, isWritable: false },
    instance: { value: input.instance ?? null, isWritable: true },
    newAdmin: { value: input.newAdmin ?? null, isWritable: false },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    privateChannelEscrowProgram: {
      value: input.privateChannelEscrowProgram ?? null,
      isWritable: false,
    },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedInstructionAccount
  >;

  // Resolve default values.
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }
  if (!accounts.privateChannelEscrowProgram.value) {
    accounts.privateChannelEscrowProgram.value =
      "9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU" as Address<"9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta("payer", accounts.payer),
      getAccountMeta("currentAdmin", accounts.currentAdmin),
      getAccountMeta("instance", accounts.instance),
      getAccountMeta("newAdmin", accounts.newAdmin),
      getAccountMeta("eventAuthority", accounts.eventAuthority),
      getAccountMeta(
        "privateChannelEscrowProgram",
        accounts.privateChannelEscrowProgram,
      ),
    ],
    data: getSetNewAdminInstructionDataEncoder().encode({}),
    programAddress,
  } as SetNewAdminInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountCurrentAdmin,
    TAccountInstance,
    TAccountNewAdmin,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >);
}

export type SetNewAdminInput<
  TAccountPayer extends string = string,
  TAccountCurrentAdmin extends string = string,
  TAccountInstance extends string = string,
  TAccountNewAdmin extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountPrivateChannelEscrowProgram extends string = string,
> = {
  /** Transaction fee payer */
  payer: TransactionSigner<TAccountPayer>;
  /** Current admin of Instance */
  currentAdmin: TransactionSigner<TAccountCurrentAdmin>;
  /** Instance PDA to update admin */
  instance: Address<TAccountInstance>;
  /** New admin public key */
  newAdmin: TransactionSigner<TAccountNewAdmin>;
  /** Event authority PDA for emitting events */
  eventAuthority: Address<TAccountEventAuthority>;
  /** Current program for CPI */
  privateChannelEscrowProgram?: Address<TAccountPrivateChannelEscrowProgram>;
};

export function getSetNewAdminInstruction<
  TAccountPayer extends string,
  TAccountCurrentAdmin extends string,
  TAccountInstance extends string,
  TAccountNewAdmin extends string,
  TAccountEventAuthority extends string,
  TAccountPrivateChannelEscrowProgram extends string,
  TProgramAddress extends Address =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
>(
  input: SetNewAdminInput<
    TAccountPayer,
    TAccountCurrentAdmin,
    TAccountInstance,
    TAccountNewAdmin,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >,
  config?: { programAddress?: TProgramAddress },
): SetNewAdminInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountCurrentAdmin,
  TAccountInstance,
  TAccountNewAdmin,
  TAccountEventAuthority,
  TAccountPrivateChannelEscrowProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    currentAdmin: { value: input.currentAdmin ?? null, isWritable: false },
    instance: { value: input.instance ?? null, isWritable: true },
    newAdmin: { value: input.newAdmin ?? null, isWritable: false },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    privateChannelEscrowProgram: {
      value: input.privateChannelEscrowProgram ?? null,
      isWritable: false,
    },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedInstructionAccount
  >;

  // Resolve default values.
  if (!accounts.privateChannelEscrowProgram.value) {
    accounts.privateChannelEscrowProgram.value =
      "9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU" as Address<"9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta("payer", accounts.payer),
      getAccountMeta("currentAdmin", accounts.currentAdmin),
      getAccountMeta("instance", accounts.instance),
      getAccountMeta("newAdmin", accounts.newAdmin),
      getAccountMeta("eventAuthority", accounts.eventAuthority),
      getAccountMeta(
        "privateChannelEscrowProgram",
        accounts.privateChannelEscrowProgram,
      ),
    ],
    data: getSetNewAdminInstructionDataEncoder().encode({}),
    programAddress,
  } as SetNewAdminInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountCurrentAdmin,
    TAccountInstance,
    TAccountNewAdmin,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >);
}

export type ParsedSetNewAdminInstruction<
  TProgram extends string =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    /** Transaction fee payer */
    payer: TAccountMetas[0];
    /** Current admin of Instance */
    currentAdmin: TAccountMetas[1];
    /** Instance PDA to update admin */
    instance: TAccountMetas[2];
    /** New admin public key */
    newAdmin: TAccountMetas[3];
    /** Event authority PDA for emitting events */
    eventAuthority: TAccountMetas[4];
    /** Current program for CPI */
    privateChannelEscrowProgram: TAccountMetas[5];
  };
  data: SetNewAdminInstructionData;
};

export function parseSetNewAdminInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetNewAdminInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 6) {
    throw new SolanaError(
      SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS,
      {
        actualAccountMetas: instruction.accounts.length,
        expectedAccountMetas: 6,
      },
    );
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      payer: getNextAccount(),
      currentAdmin: getNextAccount(),
      instance: getNextAccount(),
      newAdmin: getNextAccount(),
      eventAuthority: getNextAccount(),
      privateChannelEscrowProgram: getNextAccount(),
    },
    data: getSetNewAdminInstructionDataDecoder().decode(instruction.data),
  };
}
