import { QbitClient } from "./qbit.client"
import { QbitCapabilities } from "./qbit.types"

export const detectCapabilities = async (
  client: QbitClient
): Promise<QbitCapabilities> => {
  const version = await client.getVersion();
  let hasPeersEndpoint = true;
  let hasBanEndpoint = true;

  try {
    await client.getTorrentPeers("__probe__");
  } catch (error) {
    const status = (error as any)?.response?.status;
    if (status !== 400 && status !== 404) {
      hasPeersEndpoint = false;
    }
  }

  try {
    await client.banPeers(["127.0.0.1:1"]);
  } catch (error) {
    const status = (error as any)?.response?.status;
    if (status !== 400 && status !== 404) {
      hasBanEndpoint = false;
    }
  }

  return { version, hasPeersEndpoint, hasBanEndpoint };
};
