/**
 * 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 ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import { findMinimumDepositPda } from "../pdas";
import { SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS } from "../programs";
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from "../shared";

export const SET_MINIMUM_DEPOSIT_AMOUNT_DISCRIMINATOR = new Uint8Array([
  176, 4, 74, 229, 206, 148, 151, 138,
]);

export function getSetMinimumDepositAmountDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    SET_MINIMUM_DEPOSIT_AMOUNT_DISCRIMINATOR,
  );
}

export type SetMinimumDepositAmountInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountMinimumDeposit extends string | AccountMeta<string> = string,
  TAccountBurnToken extends string | AccountMeta<string> = string,
  TAccountProgramData extends string | AccountMeta<string> = string,
  TAccountThisProgram extends string | AccountMeta<string> =
    "FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS",
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? WritableSignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountMinimumDeposit extends string
        ? WritableAccount<TAccountMinimumDeposit>
        : TAccountMinimumDeposit,
      TAccountBurnToken extends string
        ? ReadonlyAccount<TAccountBurnToken>
        : TAccountBurnToken,
      TAccountProgramData extends string
        ? ReadonlyAccount<TAccountProgramData>
        : TAccountProgramData,
      TAccountThisProgram extends string
        ? ReadonlyAccount<TAccountThisProgram>
        : TAccountThisProgram,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      ...TRemainingAccounts,
    ]
  >;

export type SetMinimumDepositAmountInstructionData = {
  discriminator: ReadonlyUint8Array;
  amount: bigint;
};

export type SetMinimumDepositAmountInstructionDataArgs = {
  amount: number | bigint;
};

export function getSetMinimumDepositAmountInstructionDataEncoder(): FixedSizeEncoder<SetMinimumDepositAmountInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["amount", getU64Encoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: SET_MINIMUM_DEPOSIT_AMOUNT_DISCRIMINATOR,
    }),
  );
}

export function getSetMinimumDepositAmountInstructionDataDecoder(): FixedSizeDecoder<SetMinimumDepositAmountInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["amount", getU64Decoder()],
  ]);
}

export function getSetMinimumDepositAmountInstructionDataCodec(): FixedSizeCodec<
  SetMinimumDepositAmountInstructionDataArgs,
  SetMinimumDepositAmountInstructionData
> {
  return combineCodec(
    getSetMinimumDepositAmountInstructionDataEncoder(),
    getSetMinimumDepositAmountInstructionDataDecoder(),
  );
}

export type SetMinimumDepositAmountAsyncInput<
  TAccountSigner extends string = string,
  TAccountMinimumDeposit extends string = string,
  TAccountBurnToken extends string = string,
  TAccountProgramData extends string = string,
  TAccountThisProgram extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  minimumDeposit?: Address<TAccountMinimumDeposit>;
  burnToken: Address<TAccountBurnToken>;
  programData: Address<TAccountProgramData>;
  thisProgram?: Address<TAccountThisProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  amount: SetMinimumDepositAmountInstructionDataArgs["amount"];
};

export async function getSetMinimumDepositAmountInstructionAsync<
  TAccountSigner extends string,
  TAccountMinimumDeposit extends string,
  TAccountBurnToken extends string,
  TAccountProgramData extends string,
  TAccountThisProgram extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: SetMinimumDepositAmountAsyncInput<
    TAccountSigner,
    TAccountMinimumDeposit,
    TAccountBurnToken,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  SetMinimumDepositAmountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMinimumDeposit,
    TAccountBurnToken,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    minimumDeposit: { value: input.minimumDeposit ?? null, isWritable: true },
    burnToken: { value: input.burnToken ?? null, isWritable: false },
    programData: { value: input.programData ?? null, isWritable: false },
    thisProgram: { value: input.thisProgram ?? null, isWritable: false },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.minimumDeposit.value) {
    accounts.minimumDeposit.value = await findMinimumDepositPda({
      burnToken: expectAddress(accounts.burnToken.value),
    });
  }
  if (!accounts.thisProgram.value) {
    accounts.thisProgram.value =
      "FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS" as Address<"FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS">;
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.minimumDeposit),
      getAccountMeta(accounts.burnToken),
      getAccountMeta(accounts.programData),
      getAccountMeta(accounts.thisProgram),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getSetMinimumDepositAmountInstructionDataEncoder().encode(
      args as SetMinimumDepositAmountInstructionDataArgs,
    ),
    programAddress,
  } as SetMinimumDepositAmountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMinimumDeposit,
    TAccountBurnToken,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >);
}

export type SetMinimumDepositAmountInput<
  TAccountSigner extends string = string,
  TAccountMinimumDeposit extends string = string,
  TAccountBurnToken extends string = string,
  TAccountProgramData extends string = string,
  TAccountThisProgram extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  minimumDeposit: Address<TAccountMinimumDeposit>;
  burnToken: Address<TAccountBurnToken>;
  programData: Address<TAccountProgramData>;
  thisProgram?: Address<TAccountThisProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  amount: SetMinimumDepositAmountInstructionDataArgs["amount"];
};

export function getSetMinimumDepositAmountInstruction<
  TAccountSigner extends string,
  TAccountMinimumDeposit extends string,
  TAccountBurnToken extends string,
  TAccountProgramData extends string,
  TAccountThisProgram extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: SetMinimumDepositAmountInput<
    TAccountSigner,
    TAccountMinimumDeposit,
    TAccountBurnToken,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): SetMinimumDepositAmountInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountMinimumDeposit,
  TAccountBurnToken,
  TAccountProgramData,
  TAccountThisProgram,
  TAccountSystemProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    minimumDeposit: { value: input.minimumDeposit ?? null, isWritable: true },
    burnToken: { value: input.burnToken ?? null, isWritable: false },
    programData: { value: input.programData ?? null, isWritable: false },
    thisProgram: { value: input.thisProgram ?? null, isWritable: false },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.thisProgram.value) {
    accounts.thisProgram.value =
      "FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS" as Address<"FsD4uqAyXiAcQapVTokNvdz1ZC5hMHjY6a93nYmEqZQS">;
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.minimumDeposit),
      getAccountMeta(accounts.burnToken),
      getAccountMeta(accounts.programData),
      getAccountMeta(accounts.thisProgram),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getSetMinimumDepositAmountInstructionDataEncoder().encode(
      args as SetMinimumDepositAmountInstructionDataArgs,
    ),
    programAddress,
  } as SetMinimumDepositAmountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMinimumDeposit,
    TAccountBurnToken,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >);
}

export type ParsedSetMinimumDepositAmountInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    minimumDeposit: TAccountMetas[1];
    burnToken: TAccountMetas[2];
    programData: TAccountMetas[3];
    thisProgram: TAccountMetas[4];
    systemProgram: TAccountMetas[5];
  };
  data: SetMinimumDepositAmountInstructionData;
};

export function parseSetMinimumDepositAmountInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetMinimumDepositAmountInstruction<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(),
      minimumDeposit: getNextAccount(),
      burnToken: getNextAccount(),
      programData: getNextAccount(),
      thisProgram: getNextAccount(),
      systemProgram: getNextAccount(),
    },
    data: getSetMinimumDepositAmountInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
