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

export const PAUSE_DISCRIMINATOR = new Uint8Array([
  211, 22, 221, 251, 74, 121, 193, 47,
]);

export function getPauseDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(PAUSE_DISCRIMINATOR);
}

export type PauseInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountPauser extends string | AccountMeta<string> = string,
  TAccountTokenMinter 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<
    [
      TAccountPauser extends string
        ? ReadonlySignerAccount<TAccountPauser> &
            AccountSignerMeta<TAccountPauser>
        : TAccountPauser,
      TAccountTokenMinter extends string
        ? WritableAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type PauseInstructionData = { discriminator: ReadonlyUint8Array };

export type PauseInstructionDataArgs = {};

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

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

export function getPauseInstructionDataCodec(): FixedSizeCodec<
  PauseInstructionDataArgs,
  PauseInstructionData
> {
  return combineCodec(
    getPauseInstructionDataEncoder(),
    getPauseInstructionDataDecoder(),
  );
}

export type PauseInput<
  TAccountPauser extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  pauser: TransactionSigner<TAccountPauser>;
  tokenMinter: Address<TAccountTokenMinter>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
};

export function getPauseInstruction<
  TAccountPauser extends string,
  TAccountTokenMinter extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
>(
  input: PauseInput<
    TAccountPauser,
    TAccountTokenMinter,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): PauseInstruction<
  TProgramAddress,
  TAccountPauser,
  TAccountTokenMinter,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    pauser: { value: input.pauser ?? null, isWritable: false },
    tokenMinter: { value: input.tokenMinter ?? 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.pauser),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getPauseInstructionDataEncoder().encode({}),
    programAddress,
  } as PauseInstruction<
    TProgramAddress,
    TAccountPauser,
    TAccountTokenMinter,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedPauseInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    pauser: TAccountMetas[0];
    tokenMinter: TAccountMetas[1];
    eventAuthority: TAccountMetas[2];
    program: TAccountMetas[3];
  };
  data: PauseInstructionData;
};

export function parsePauseInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedPauseInstruction<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: {
      pauser: getNextAccount(),
      tokenMinter: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getPauseInstructionDataDecoder().decode(instruction.data),
  };
}
