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

export const SET_MIN_FEE_DISCRIMINATOR = new Uint8Array([
  114, 198, 35, 3, 41, 196, 194, 246,
]);

export function getSetMinFeeDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(SET_MIN_FEE_DISCRIMINATOR);
}

export type SetMinFeeInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountMinFeeController extends string | AccountMeta<string> = string,
  TAccountTokenMessenger 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<
    [
      TAccountMinFeeController extends string
        ? ReadonlySignerAccount<TAccountMinFeeController> &
            AccountSignerMeta<TAccountMinFeeController>
        : TAccountMinFeeController,
      TAccountTokenMessenger extends string
        ? WritableAccount<TAccountTokenMessenger>
        : TAccountTokenMessenger,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type SetMinFeeInstructionData = {
  discriminator: ReadonlyUint8Array;
  newMinFee: number;
};

export type SetMinFeeInstructionDataArgs = { newMinFee: number };

export function getSetMinFeeInstructionDataEncoder(): FixedSizeEncoder<SetMinFeeInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["newMinFee", getU32Encoder()],
    ]),
    (value) => ({ ...value, discriminator: SET_MIN_FEE_DISCRIMINATOR }),
  );
}

export function getSetMinFeeInstructionDataDecoder(): FixedSizeDecoder<SetMinFeeInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["newMinFee", getU32Decoder()],
  ]);
}

export function getSetMinFeeInstructionDataCodec(): FixedSizeCodec<
  SetMinFeeInstructionDataArgs,
  SetMinFeeInstructionData
> {
  return combineCodec(
    getSetMinFeeInstructionDataEncoder(),
    getSetMinFeeInstructionDataDecoder(),
  );
}

export type SetMinFeeAsyncInput<
  TAccountMinFeeController extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  minFeeController: TransactionSigner<TAccountMinFeeController>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  newMinFee: SetMinFeeInstructionDataArgs["newMinFee"];
};

export async function getSetMinFeeInstructionAsync<
  TAccountMinFeeController extends string,
  TAccountTokenMessenger extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: SetMinFeeAsyncInput<
    TAccountMinFeeController,
    TAccountTokenMessenger,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  SetMinFeeInstruction<
    TProgramAddress,
    TAccountMinFeeController,
    TAccountTokenMessenger,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    minFeeController: {
      value: input.minFeeController ?? null,
      isWritable: false,
    },
    tokenMessenger: { value: input.tokenMessenger ?? 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.minFeeController),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getSetMinFeeInstructionDataEncoder().encode(
      args as SetMinFeeInstructionDataArgs,
    ),
    programAddress,
  } as SetMinFeeInstruction<
    TProgramAddress,
    TAccountMinFeeController,
    TAccountTokenMessenger,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type SetMinFeeInput<
  TAccountMinFeeController extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  minFeeController: TransactionSigner<TAccountMinFeeController>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  newMinFee: SetMinFeeInstructionDataArgs["newMinFee"];
};

export function getSetMinFeeInstruction<
  TAccountMinFeeController extends string,
  TAccountTokenMessenger extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
>(
  input: SetMinFeeInput<
    TAccountMinFeeController,
    TAccountTokenMessenger,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): SetMinFeeInstruction<
  TProgramAddress,
  TAccountMinFeeController,
  TAccountTokenMessenger,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    minFeeController: {
      value: input.minFeeController ?? null,
      isWritable: false,
    },
    tokenMessenger: { value: input.tokenMessenger ?? 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.minFeeController),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getSetMinFeeInstructionDataEncoder().encode(
      args as SetMinFeeInstructionDataArgs,
    ),
    programAddress,
  } as SetMinFeeInstruction<
    TProgramAddress,
    TAccountMinFeeController,
    TAccountTokenMessenger,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedSetMinFeeInstruction<
  TProgram extends string = typeof TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    minFeeController: TAccountMetas[0];
    tokenMessenger: TAccountMetas[1];
    eventAuthority: TAccountMetas[2];
    program: TAccountMetas[3];
  };
  data: SetMinFeeInstructionData;
};

export function parseSetMinFeeInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetMinFeeInstruction<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: {
      minFeeController: getNextAccount(),
      tokenMessenger: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getSetMinFeeInstructionDataDecoder().decode(instruction.data),
  };
}
