// Anonymous daily tally of cookie-banner choices, used to measure how much
// consent-gated analytics (LinkedIn, GA4) under-report. Sends only the choice:
// no cookies, user, page or device data, so it needs no consent of its own.

export type ConsentChoice = "accepted" | "rejected" | "dismissed";

const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || "";

export function reportConsentChoice(choice: ConsentChoice): void {
  if (typeof window === "undefined" || !API_BASE_URL) return;
  // keepalive lets the count land even if the click navigates away. A failed
  // count is deliberately ignored: it must never break the banner.
  fetch(`${API_BASE_URL}/v1/consent-events`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ choice }),
    credentials: "omit",
    keepalive: true,
  }).catch(() => {});
}
