/**
 * API functions for the world map / bans-by-country endpoint.
 */

import { get } from "./client";
import { ENDPOINTS } from "./endpoints";
import type { BansByCountryResponse, TimeRange } from "../types/map";
import type { BanOriginFilter } from "../types/ban";

/**
 * Fetch ban counts aggregated by country for the given time window.
 *
 * @param range  - Time-range preset.
 * @param origin - Origin filter: `"blocklist"`, `"selfblock"`, or `"all"`
 *   (default `"all"`, which omits the parameter entirely).
 */
export async function fetchBansByCountry(
  range: TimeRange = "24h",
  origin: BanOriginFilter = "all",
  source: "fail2ban" | "archive" = "fail2ban",
  countryCode?: string,
): Promise<BansByCountryResponse> {
  const params = new URLSearchParams({ range });
  if (origin !== "all") {
    params.set("origin", origin);
  }
  if (source !== "fail2ban") {
    params.set("source", source);
  }
  if (countryCode) {
    params.set("country_code", countryCode);
  }
  return get<BansByCountryResponse>(`${ENDPOINTS.dashboardBansByCountry}?${params.toString()}`);
}
