/**
 * 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,
  getAddressFromResolvedInstructionAccount,
  type ResolvedInstructionAccount,
} from "@solana/kit/program-client-core";
import { findEventAuthorityPda, findOperatorPda } from "../pdas";
import { PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS } from "../programs";

export const REMOVE_OPERATOR_DISCRIMINATOR = 4;

export function getRemoveOperatorDiscriminatorBytes(): ReadonlyUint8Array {
  return getU8Encoder().encode(REMOVE_OPERATOR_DISCRIMINATOR);
}

export type RemoveOperatorInstruction<
  TProgram extends string =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountAdmin extends string | AccountMeta<string> = string,
  TAccountInstance extends string | AccountMeta<string> = string,
  TAccountOperator extends string | AccountMeta<string> = string,
  TAccountOperatorPda extends string | AccountMeta<string> = string,
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  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,
      TAccountAdmin extends string
        ? ReadonlySignerAccount<TAccountAdmin> &
            AccountSignerMeta<TAccountAdmin>
        : TAccountAdmin,
      TAccountInstance extends string
        ? ReadonlyAccount<TAccountInstance>
        : TAccountInstance,
      TAccountOperator extends string
        ? ReadonlyAccount<TAccountOperator>
        : TAccountOperator,
      TAccountOperatorPda extends string
        ? WritableAccount<TAccountOperatorPda>
        : TAccountOperatorPda,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountPrivateChannelEscrowProgram extends string
        ? ReadonlyAccount<TAccountPrivateChannelEscrowProgram>
        : TAccountPrivateChannelEscrowProgram,
      ...TRemainingAccounts,
    ]
  >;

export type RemoveOperatorInstructionData = { discriminator: number };

export type RemoveOperatorInstructionDataArgs = {};

export function getRemoveOperatorInstructionDataEncoder(): FixedSizeEncoder<RemoveOperatorInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([["discriminator", getU8Encoder()]]),
    (value) => ({ ...value, discriminator: REMOVE_OPERATOR_DISCRIMINATOR }),
  );
}

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

export function getRemoveOperatorInstructionDataCodec(): FixedSizeCodec<
  RemoveOperatorInstructionDataArgs,
  RemoveOperatorInstructionData
> {
  return combineCodec(
    getRemoveOperatorInstructionDataEncoder(),
    getRemoveOperatorInstructionDataDecoder(),
  );
}

export type RemoveOperatorAsyncInput<
  TAccountPayer extends string = string,
  TAccountAdmin extends string = string,
  TAccountInstance extends string = string,
  TAccountOperator extends string = string,
  TAccountOperatorPda extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountPrivateChannelEscrowProgram extends string = string,
> = {
  /** Transaction fee payer */
  payer: TransactionSigner<TAccountPayer>;
  /** Admin of Instance */
  admin: TransactionSigner<TAccountAdmin>;
  /** Instance PDA to validate admin authority */
  instance: Address<TAccountInstance>;
  /** Operator public key to be removed */
  operator: Address<TAccountOperator>;
  /** Existing Operator PDA */
  operatorPda?: Address<TAccountOperatorPda>;
  /** System program */
  systemProgram?: Address<TAccountSystemProgram>;
  /** Event authority PDA for emitting events */
  eventAuthority?: Address<TAccountEventAuthority>;
  /** Current program for CPI */
  privateChannelEscrowProgram?: Address<TAccountPrivateChannelEscrowProgram>;
};

export async function getRemoveOperatorInstructionAsync<
  TAccountPayer extends string,
  TAccountAdmin extends string,
  TAccountInstance extends string,
  TAccountOperator extends string,
  TAccountOperatorPda extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountPrivateChannelEscrowProgram extends string,
  TProgramAddress extends Address =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
>(
  input: RemoveOperatorAsyncInput<
    TAccountPayer,
    TAccountAdmin,
    TAccountInstance,
    TAccountOperator,
    TAccountOperatorPda,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  RemoveOperatorInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountAdmin,
    TAccountInstance,
    TAccountOperator,
    TAccountOperatorPda,
    TAccountSystemProgram,
    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 },
    admin: { value: input.admin ?? null, isWritable: false },
    instance: { value: input.instance ?? null, isWritable: false },
    operator: { value: input.operator ?? null, isWritable: false },
    operatorPda: { value: input.operatorPda ?? null, isWritable: true },
    systemProgram: { value: input.systemProgram ?? 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.operatorPda.value) {
    accounts.operatorPda.value = await findOperatorPda({
      instance: getAddressFromResolvedInstructionAccount(
        "instance",
        accounts.instance.value,
      ),
      wallet: getAddressFromResolvedInstructionAccount(
        "operator",
        accounts.operator.value,
      ),
    });
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  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("admin", accounts.admin),
      getAccountMeta("instance", accounts.instance),
      getAccountMeta("operator", accounts.operator),
      getAccountMeta("operatorPda", accounts.operatorPda),
      getAccountMeta("systemProgram", accounts.systemProgram),
      getAccountMeta("eventAuthority", accounts.eventAuthority),
      getAccountMeta(
        "privateChannelEscrowProgram",
        accounts.privateChannelEscrowProgram,
      ),
    ],
    data: getRemoveOperatorInstructionDataEncoder().encode({}),
    programAddress,
  } as RemoveOperatorInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountAdmin,
    TAccountInstance,
    TAccountOperator,
    TAccountOperatorPda,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >);
}

export type RemoveOperatorInput<
  TAccountPayer extends string = string,
  TAccountAdmin extends string = string,
  TAccountInstance extends string = string,
  TAccountOperator extends string = string,
  TAccountOperatorPda extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountPrivateChannelEscrowProgram extends string = string,
> = {
  /** Transaction fee payer */
  payer: TransactionSigner<TAccountPayer>;
  /** Admin of Instance */
  admin: TransactionSigner<TAccountAdmin>;
  /** Instance PDA to validate admin authority */
  instance: Address<TAccountInstance>;
  /** Operator public key to be removed */
  operator: Address<TAccountOperator>;
  /** Existing Operator PDA */
  operatorPda: Address<TAccountOperatorPda>;
  /** System program */
  systemProgram?: Address<TAccountSystemProgram>;
  /** Event authority PDA for emitting events */
  eventAuthority: Address<TAccountEventAuthority>;
  /** Current program for CPI */
  privateChannelEscrowProgram?: Address<TAccountPrivateChannelEscrowProgram>;
};

export function getRemoveOperatorInstruction<
  TAccountPayer extends string,
  TAccountAdmin extends string,
  TAccountInstance extends string,
  TAccountOperator extends string,
  TAccountOperatorPda extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountPrivateChannelEscrowProgram extends string,
  TProgramAddress extends Address =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
>(
  input: RemoveOperatorInput<
    TAccountPayer,
    TAccountAdmin,
    TAccountInstance,
    TAccountOperator,
    TAccountOperatorPda,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >,
  config?: { programAddress?: TProgramAddress },
): RemoveOperatorInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountAdmin,
  TAccountInstance,
  TAccountOperator,
  TAccountOperatorPda,
  TAccountSystemProgram,
  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 },
    admin: { value: input.admin ?? null, isWritable: false },
    instance: { value: input.instance ?? null, isWritable: false },
    operator: { value: input.operator ?? null, isWritable: false },
    operatorPda: { value: input.operatorPda ?? null, isWritable: true },
    systemProgram: { value: input.systemProgram ?? 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.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  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("admin", accounts.admin),
      getAccountMeta("instance", accounts.instance),
      getAccountMeta("operator", accounts.operator),
      getAccountMeta("operatorPda", accounts.operatorPda),
      getAccountMeta("systemProgram", accounts.systemProgram),
      getAccountMeta("eventAuthority", accounts.eventAuthority),
      getAccountMeta(
        "privateChannelEscrowProgram",
        accounts.privateChannelEscrowProgram,
      ),
    ],
    data: getRemoveOperatorInstructionDataEncoder().encode({}),
    programAddress,
  } as RemoveOperatorInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountAdmin,
    TAccountInstance,
    TAccountOperator,
    TAccountOperatorPda,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >);
}

export type ParsedRemoveOperatorInstruction<
  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];
    /** Admin of Instance */
    admin: TAccountMetas[1];
    /** Instance PDA to validate admin authority */
    instance: TAccountMetas[2];
    /** Operator public key to be removed */
    operator: TAccountMetas[3];
    /** Existing Operator PDA */
    operatorPda: TAccountMetas[4];
    /** System program */
    systemProgram: TAccountMetas[5];
    /** Event authority PDA for emitting events */
    eventAuthority: TAccountMetas[6];
    /** Current program for CPI */
    privateChannelEscrowProgram: TAccountMetas[7];
  };
  data: RemoveOperatorInstructionData;
};

export function parseRemoveOperatorInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedRemoveOperatorInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 8) {
    throw new SolanaError(
      SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS,
      {
        actualAccountMetas: instruction.accounts.length,
        expectedAccountMetas: 8,
      },
    );
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      payer: getNextAccount(),
      admin: getNextAccount(),
      instance: getNextAccount(),
      operator: getNextAccount(),
      operatorPda: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      privateChannelEscrowProgram: getNextAccount(),
    },
    data: getRemoveOperatorInstructionDataDecoder().decode(instruction.data),
  };
}
