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

export const CLOSE_CLAIM_ACCOUNT_DISCRIMINATOR = new Uint8Array([
  241, 146, 203, 216, 58, 222, 91, 118,
]);

export function getCloseClaimAccountDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    CLOSE_CLAIM_ACCOUNT_DISCRIMINATOR,
  );
}

export type CloseClaimAccountInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountMint extends string | AccountMeta<string> = string,
  TAccountRefundAddress extends string | AccountMeta<string> = string,
  TAccountClaimAccount extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? WritableSignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountMint extends string
        ? ReadonlyAccount<TAccountMint>
        : TAccountMint,
      TAccountRefundAddress extends string
        ? ReadonlyAccount<TAccountRefundAddress>
        : TAccountRefundAddress,
      TAccountClaimAccount extends string
        ? WritableAccount<TAccountClaimAccount>
        : TAccountClaimAccount,
      ...TRemainingAccounts,
    ]
  >;

export type CloseClaimAccountInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type CloseClaimAccountInstructionDataArgs = {};

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

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

export function getCloseClaimAccountInstructionDataCodec(): FixedSizeCodec<
  CloseClaimAccountInstructionDataArgs,
  CloseClaimAccountInstructionData
> {
  return combineCodec(
    getCloseClaimAccountInstructionDataEncoder(),
    getCloseClaimAccountInstructionDataDecoder(),
  );
}

export type CloseClaimAccountAsyncInput<
  TAccountSigner extends string = string,
  TAccountMint extends string = string,
  TAccountRefundAddress extends string = string,
  TAccountClaimAccount extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  mint: Address<TAccountMint>;
  refundAddress: Address<TAccountRefundAddress>;
  claimAccount?: Address<TAccountClaimAccount>;
};

export async function getCloseClaimAccountInstructionAsync<
  TAccountSigner extends string,
  TAccountMint extends string,
  TAccountRefundAddress extends string,
  TAccountClaimAccount extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: CloseClaimAccountAsyncInput<
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  CloseClaimAccountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount
  >
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    mint: { value: input.mint ?? null, isWritable: false },
    refundAddress: { value: input.refundAddress ?? null, isWritable: false },
    claimAccount: { value: input.claimAccount ?? null, isWritable: true },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Resolve default values.
  if (!accounts.claimAccount.value) {
    accounts.claimAccount.value = await findClaimAccountPda({
      mint: expectAddress(accounts.mint.value),
      refundAddress: expectAddress(accounts.refundAddress.value),
    });
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.refundAddress),
      getAccountMeta(accounts.claimAccount),
    ],
    data: getCloseClaimAccountInstructionDataEncoder().encode({}),
    programAddress,
  } as CloseClaimAccountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount
  >);
}

export type CloseClaimAccountInput<
  TAccountSigner extends string = string,
  TAccountMint extends string = string,
  TAccountRefundAddress extends string = string,
  TAccountClaimAccount extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  mint: Address<TAccountMint>;
  refundAddress: Address<TAccountRefundAddress>;
  claimAccount: Address<TAccountClaimAccount>;
};

export function getCloseClaimAccountInstruction<
  TAccountSigner extends string,
  TAccountMint extends string,
  TAccountRefundAddress extends string,
  TAccountClaimAccount extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: CloseClaimAccountInput<
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount
  >,
  config?: { programAddress?: TProgramAddress },
): CloseClaimAccountInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountMint,
  TAccountRefundAddress,
  TAccountClaimAccount
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    mint: { value: input.mint ?? null, isWritable: false },
    refundAddress: { value: input.refundAddress ?? null, isWritable: false },
    claimAccount: { value: input.claimAccount ?? null, isWritable: true },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.refundAddress),
      getAccountMeta(accounts.claimAccount),
    ],
    data: getCloseClaimAccountInstructionDataEncoder().encode({}),
    programAddress,
  } as CloseClaimAccountInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountMint,
    TAccountRefundAddress,
    TAccountClaimAccount
  >);
}

export type ParsedCloseClaimAccountInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    mint: TAccountMetas[1];
    refundAddress: TAccountMetas[2];
    claimAccount: TAccountMetas[3];
  };
  data: CloseClaimAccountInstructionData;
};

export function parseCloseClaimAccountInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedCloseClaimAccountInstruction<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: {
      signer: getNextAccount(),
      mint: getNextAccount(),
      refundAddress: getNextAccount(),
      claimAccount: getNextAccount(),
    },
    data: getCloseClaimAccountInstructionDataDecoder().decode(instruction.data),
  };
}
