/**
 * 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,
  getU64Decoder,
  getU64Encoder,
  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 SET_MAX_BURN_AMOUNT_PER_MESSAGE_DISCRIMINATOR = new Uint8Array([
  30, 128, 145, 240, 70, 237, 109, 207,
]);

export function getSetMaxBurnAmountPerMessageDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    SET_MAX_BURN_AMOUNT_PER_MESSAGE_DISCRIMINATOR,
  );
}

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

export type SetMaxBurnAmountPerMessageInstructionData = {
  discriminator: ReadonlyUint8Array;
  burnLimitPerMessage: bigint;
};

export type SetMaxBurnAmountPerMessageInstructionDataArgs = {
  burnLimitPerMessage: number | bigint;
};

export function getSetMaxBurnAmountPerMessageInstructionDataEncoder(): FixedSizeEncoder<SetMaxBurnAmountPerMessageInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["burnLimitPerMessage", getU64Encoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: SET_MAX_BURN_AMOUNT_PER_MESSAGE_DISCRIMINATOR,
    }),
  );
}

export function getSetMaxBurnAmountPerMessageInstructionDataDecoder(): FixedSizeDecoder<SetMaxBurnAmountPerMessageInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["burnLimitPerMessage", getU64Decoder()],
  ]);
}

export function getSetMaxBurnAmountPerMessageInstructionDataCodec(): FixedSizeCodec<
  SetMaxBurnAmountPerMessageInstructionDataArgs,
  SetMaxBurnAmountPerMessageInstructionData
> {
  return combineCodec(
    getSetMaxBurnAmountPerMessageInstructionDataEncoder(),
    getSetMaxBurnAmountPerMessageInstructionDataDecoder(),
  );
}

export type SetMaxBurnAmountPerMessageInput<
  TAccountTokenController extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  tokenController: TransactionSigner<TAccountTokenController>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  burnLimitPerMessage: SetMaxBurnAmountPerMessageInstructionDataArgs["burnLimitPerMessage"];
};

export function getSetMaxBurnAmountPerMessageInstruction<
  TAccountTokenController extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
>(
  input: SetMaxBurnAmountPerMessageInput<
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): SetMaxBurnAmountPerMessageInstruction<
  TProgramAddress,
  TAccountTokenController,
  TAccountTokenMinter,
  TAccountLocalToken,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    tokenController: {
      value: input.tokenController ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    localToken: { value: input.localToken ?? 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.tokenController),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getSetMaxBurnAmountPerMessageInstructionDataEncoder().encode(
      args as SetMaxBurnAmountPerMessageInstructionDataArgs,
    ),
    programAddress,
  } as SetMaxBurnAmountPerMessageInstruction<
    TProgramAddress,
    TAccountTokenController,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedSetMaxBurnAmountPerMessageInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    tokenController: TAccountMetas[0];
    tokenMinter: TAccountMetas[1];
    localToken: TAccountMetas[2];
    eventAuthority: TAccountMetas[3];
    program: TAccountMetas[4];
  };
  data: SetMaxBurnAmountPerMessageInstructionData;
};

export function parseSetMaxBurnAmountPerMessageInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetMaxBurnAmountPerMessageInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 5) {
    // 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: {
      tokenController: getNextAccount(),
      tokenMinter: getNextAccount(),
      localToken: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getSetMaxBurnAmountPerMessageInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
