/**
 * 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 Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
  type WritableAccount,
} from "@solana/kit";
import {
  findEventAuthorityPda,
  findRentFundPda,
  findRepayRentFundDebtRentClaimPda,
} from "../pdas";
import { SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS } from "../programs";
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from "../shared";

export const REPAY_RENT_FUND_DEBT_DISCRIMINATOR = new Uint8Array([
  111, 95, 222, 174, 241, 41, 61, 78,
]);

export function getRepayRentFundDebtDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    REPAY_RENT_FUND_DEBT_DISCRIMINATOR,
  );
}

export type RepayRentFundDebtInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountRentFund extends string | AccountMeta<string> = string,
  TAccountRecipient extends string | AccountMeta<string> = string,
  TAccountRentClaim extends string | AccountMeta<string> = string,
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountRentFund extends string
        ? WritableAccount<TAccountRentFund>
        : TAccountRentFund,
      TAccountRecipient extends string
        ? WritableAccount<TAccountRecipient>
        : TAccountRecipient,
      TAccountRentClaim extends string
        ? WritableAccount<TAccountRentClaim>
        : TAccountRentClaim,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type RepayRentFundDebtInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type RepayRentFundDebtInstructionDataArgs = {};

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

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

export function getRepayRentFundDebtInstructionDataCodec(): FixedSizeCodec<
  RepayRentFundDebtInstructionDataArgs,
  RepayRentFundDebtInstructionData
> {
  return combineCodec(
    getRepayRentFundDebtInstructionDataEncoder(),
    getRepayRentFundDebtInstructionDataDecoder(),
  );
}

export type RepayRentFundDebtAsyncInput<
  TAccountRentFund extends string = string,
  TAccountRecipient extends string = string,
  TAccountRentClaim extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  rentFund?: Address<TAccountRentFund>;
  recipient: Address<TAccountRecipient>;
  rentClaim?: Address<TAccountRentClaim>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
};

export async function getRepayRentFundDebtInstructionAsync<
  TAccountRentFund extends string,
  TAccountRecipient extends string,
  TAccountRentClaim extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: RepayRentFundDebtAsyncInput<
    TAccountRentFund,
    TAccountRecipient,
    TAccountRentClaim,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  RepayRentFundDebtInstruction<
    TProgramAddress,
    TAccountRentFund,
    TAccountRecipient,
    TAccountRentClaim,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    recipient: { value: input.recipient ?? null, isWritable: true },
    rentClaim: { value: input.rentClaim ?? null, isWritable: true },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    program: { value: input.program ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Resolve default values.
  if (!accounts.rentFund.value) {
    accounts.rentFund.value = await findRentFundPda();
  }
  if (!accounts.rentClaim.value) {
    accounts.rentClaim.value = await findRepayRentFundDebtRentClaimPda({
      recipient: expectAddress(accounts.recipient.value),
    });
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.rentFund),
      getAccountMeta(accounts.recipient),
      getAccountMeta(accounts.rentClaim),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getRepayRentFundDebtInstructionDataEncoder().encode({}),
    programAddress,
  } as RepayRentFundDebtInstruction<
    TProgramAddress,
    TAccountRentFund,
    TAccountRecipient,
    TAccountRentClaim,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type RepayRentFundDebtInput<
  TAccountRentFund extends string = string,
  TAccountRecipient extends string = string,
  TAccountRentClaim extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  rentFund: Address<TAccountRentFund>;
  recipient: Address<TAccountRecipient>;
  rentClaim: Address<TAccountRentClaim>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
};

export function getRepayRentFundDebtInstruction<
  TAccountRentFund extends string,
  TAccountRecipient extends string,
  TAccountRentClaim extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address =
    typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: RepayRentFundDebtInput<
    TAccountRentFund,
    TAccountRecipient,
    TAccountRentClaim,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): RepayRentFundDebtInstruction<
  TProgramAddress,
  TAccountRentFund,
  TAccountRecipient,
  TAccountRentClaim,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    recipient: { value: input.recipient ?? null, isWritable: true },
    rentClaim: { value: input.rentClaim ?? null, isWritable: true },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    program: { value: input.program ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.rentFund),
      getAccountMeta(accounts.recipient),
      getAccountMeta(accounts.rentClaim),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getRepayRentFundDebtInstructionDataEncoder().encode({}),
    programAddress,
  } as RepayRentFundDebtInstruction<
    TProgramAddress,
    TAccountRentFund,
    TAccountRecipient,
    TAccountRentClaim,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedRepayRentFundDebtInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    rentFund: TAccountMetas[0];
    recipient: TAccountMetas[1];
    rentClaim: TAccountMetas[2];
    systemProgram: TAccountMetas[3];
    eventAuthority: TAccountMetas[4];
    program: TAccountMetas[5];
  };
  data: RepayRentFundDebtInstructionData;
};

export function parseRepayRentFundDebtInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedRepayRentFundDebtInstruction<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: {
      rentFund: getNextAccount(),
      recipient: getNextAccount(),
      rentClaim: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getRepayRentFundDebtInstructionDataDecoder().decode(instruction.data),
  };
}
