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

export const EMERGENCY_DELETE_ROOT_BUNDLE_DISCRIMINATOR = new Uint8Array([
  226, 158, 1, 74, 84, 113, 24, 152,
]);

export function getEmergencyDeleteRootBundleDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    EMERGENCY_DELETE_ROOT_BUNDLE_DISCRIMINATOR,
  );
}

export type EmergencyDeleteRootBundleInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountCloser extends string | AccountMeta<string> = string,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountRootBundle 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<
    [
      TAccountSigner extends string
        ? ReadonlySignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountCloser extends string
        ? WritableAccount<TAccountCloser>
        : TAccountCloser,
      TAccountState extends string
        ? ReadonlyAccount<TAccountState>
        : TAccountState,
      TAccountRootBundle extends string
        ? WritableAccount<TAccountRootBundle>
        : TAccountRootBundle,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type EmergencyDeleteRootBundleInstructionData = {
  discriminator: ReadonlyUint8Array;
  rootBundleId: number;
};

export type EmergencyDeleteRootBundleInstructionDataArgs = {
  rootBundleId: number;
};

export function getEmergencyDeleteRootBundleInstructionDataEncoder(): FixedSizeEncoder<EmergencyDeleteRootBundleInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["rootBundleId", getU32Encoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: EMERGENCY_DELETE_ROOT_BUNDLE_DISCRIMINATOR,
    }),
  );
}

export function getEmergencyDeleteRootBundleInstructionDataDecoder(): FixedSizeDecoder<EmergencyDeleteRootBundleInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["rootBundleId", getU32Decoder()],
  ]);
}

export function getEmergencyDeleteRootBundleInstructionDataCodec(): FixedSizeCodec<
  EmergencyDeleteRootBundleInstructionDataArgs,
  EmergencyDeleteRootBundleInstructionData
> {
  return combineCodec(
    getEmergencyDeleteRootBundleInstructionDataEncoder(),
    getEmergencyDeleteRootBundleInstructionDataDecoder(),
  );
}

export type EmergencyDeleteRootBundleAsyncInput<
  TAccountSigner extends string = string,
  TAccountCloser extends string = string,
  TAccountState extends string = string,
  TAccountRootBundle extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  closer: Address<TAccountCloser>;
  state: Address<TAccountState>;
  rootBundle: Address<TAccountRootBundle>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  rootBundleId: EmergencyDeleteRootBundleInstructionDataArgs["rootBundleId"];
};

export async function getEmergencyDeleteRootBundleInstructionAsync<
  TAccountSigner extends string,
  TAccountCloser extends string,
  TAccountState extends string,
  TAccountRootBundle extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: EmergencyDeleteRootBundleAsyncInput<
    TAccountSigner,
    TAccountCloser,
    TAccountState,
    TAccountRootBundle,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  EmergencyDeleteRootBundleInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountCloser,
    TAccountState,
    TAccountRootBundle,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

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

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.closer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.rootBundle),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getEmergencyDeleteRootBundleInstructionDataEncoder().encode(
      args as EmergencyDeleteRootBundleInstructionDataArgs,
    ),
    programAddress,
  } as EmergencyDeleteRootBundleInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountCloser,
    TAccountState,
    TAccountRootBundle,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type EmergencyDeleteRootBundleInput<
  TAccountSigner extends string = string,
  TAccountCloser extends string = string,
  TAccountState extends string = string,
  TAccountRootBundle extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  closer: Address<TAccountCloser>;
  state: Address<TAccountState>;
  rootBundle: Address<TAccountRootBundle>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  rootBundleId: EmergencyDeleteRootBundleInstructionDataArgs["rootBundleId"];
};

export function getEmergencyDeleteRootBundleInstruction<
  TAccountSigner extends string,
  TAccountCloser extends string,
  TAccountState extends string,
  TAccountRootBundle extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: EmergencyDeleteRootBundleInput<
    TAccountSigner,
    TAccountCloser,
    TAccountState,
    TAccountRootBundle,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): EmergencyDeleteRootBundleInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountCloser,
  TAccountState,
  TAccountRootBundle,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: false },
    closer: { value: input.closer ?? null, isWritable: true },
    state: { value: input.state ?? null, isWritable: false },
    rootBundle: { value: input.rootBundle ?? 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.signer),
      getAccountMeta(accounts.closer),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.rootBundle),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getEmergencyDeleteRootBundleInstructionDataEncoder().encode(
      args as EmergencyDeleteRootBundleInstructionDataArgs,
    ),
    programAddress,
  } as EmergencyDeleteRootBundleInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountCloser,
    TAccountState,
    TAccountRootBundle,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedEmergencyDeleteRootBundleInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    closer: TAccountMetas[1];
    state: TAccountMetas[2];
    rootBundle: TAccountMetas[3];
    eventAuthority: TAccountMetas[4];
    program: TAccountMetas[5];
  };
  data: EmergencyDeleteRootBundleInstructionData;
};

export function parseEmergencyDeleteRootBundleInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedEmergencyDeleteRootBundleInstruction<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: {
      signer: getNextAccount(),
      closer: getNextAccount(),
      state: getNextAccount(),
      rootBundle: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getEmergencyDeleteRootBundleInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
