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

export const WITHDRAW_RENT_FUND_DISCRIMINATOR = new Uint8Array([
  153, 28, 108, 116, 132, 70, 161, 125,
]);

export function getWithdrawRentFundDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    WITHDRAW_RENT_FUND_DISCRIMINATOR,
  );
}

export type WithdrawRentFundInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountRentFund extends string | AccountMeta<string> = string,
  TAccountRecipient 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,
      TAccountRentFund extends string
        ? WritableAccount<TAccountRentFund>
        : TAccountRentFund,
      TAccountRecipient extends string
        ? WritableAccount<TAccountRecipient>
        : TAccountRecipient,
      TAccountProgramData extends string
        ? ReadonlyAccount<TAccountProgramData>
        : TAccountProgramData,
      TAccountThisProgram extends string
        ? ReadonlyAccount<TAccountThisProgram>
        : TAccountThisProgram,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      ...TRemainingAccounts,
    ]
  >;

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

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

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

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

export function getWithdrawRentFundInstructionDataCodec(): FixedSizeCodec<
  WithdrawRentFundInstructionDataArgs,
  WithdrawRentFundInstructionData
> {
  return combineCodec(
    getWithdrawRentFundInstructionDataEncoder(),
    getWithdrawRentFundInstructionDataDecoder(),
  );
}

export type WithdrawRentFundAsyncInput<
  TAccountSigner extends string = string,
  TAccountRentFund extends string = string,
  TAccountRecipient extends string = string,
  TAccountProgramData extends string = string,
  TAccountThisProgram extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  rentFund?: Address<TAccountRentFund>;
  recipient: Address<TAccountRecipient>;
  programData: Address<TAccountProgramData>;
  thisProgram?: Address<TAccountThisProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  amount: WithdrawRentFundInstructionDataArgs["amount"];
};

export async function getWithdrawRentFundInstructionAsync<
  TAccountSigner extends string,
  TAccountRentFund extends string,
  TAccountRecipient extends string,
  TAccountProgramData extends string,
  TAccountThisProgram extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: WithdrawRentFundAsyncInput<
    TAccountSigner,
    TAccountRentFund,
    TAccountRecipient,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  WithdrawRentFundInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountRentFund,
    TAccountRecipient,
    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 },
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    recipient: { value: input.recipient ?? null, isWritable: true },
    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.rentFund.value) {
    accounts.rentFund.value = await findRentFundPda();
  }
  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.rentFund),
      getAccountMeta(accounts.recipient),
      getAccountMeta(accounts.programData),
      getAccountMeta(accounts.thisProgram),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getWithdrawRentFundInstructionDataEncoder().encode(
      args as WithdrawRentFundInstructionDataArgs,
    ),
    programAddress,
  } as WithdrawRentFundInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountRentFund,
    TAccountRecipient,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >);
}

export type WithdrawRentFundInput<
  TAccountSigner extends string = string,
  TAccountRentFund extends string = string,
  TAccountRecipient extends string = string,
  TAccountProgramData extends string = string,
  TAccountThisProgram extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  rentFund: Address<TAccountRentFund>;
  recipient: Address<TAccountRecipient>;
  programData: Address<TAccountProgramData>;
  thisProgram?: Address<TAccountThisProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  amount: WithdrawRentFundInstructionDataArgs["amount"];
};

export function getWithdrawRentFundInstruction<
  TAccountSigner extends string,
  TAccountRentFund extends string,
  TAccountRecipient extends string,
  TAccountProgramData extends string,
  TAccountThisProgram extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: WithdrawRentFundInput<
    TAccountSigner,
    TAccountRentFund,
    TAccountRecipient,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): WithdrawRentFundInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountRentFund,
  TAccountRecipient,
  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 },
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    recipient: { value: input.recipient ?? null, isWritable: true },
    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.rentFund),
      getAccountMeta(accounts.recipient),
      getAccountMeta(accounts.programData),
      getAccountMeta(accounts.thisProgram),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getWithdrawRentFundInstructionDataEncoder().encode(
      args as WithdrawRentFundInstructionDataArgs,
    ),
    programAddress,
  } as WithdrawRentFundInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountRentFund,
    TAccountRecipient,
    TAccountProgramData,
    TAccountThisProgram,
    TAccountSystemProgram
  >);
}

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

export function parseWithdrawRentFundInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedWithdrawRentFundInstruction<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(),
      rentFund: getNextAccount(),
      recipient: getNextAccount(),
      programData: getNextAccount(),
      thisProgram: getNextAccount(),
      systemProgram: getNextAccount(),
    },
    data: getWithdrawRentFundInstructionDataDecoder().decode(instruction.data),
  };
}
