import { tokenStorage } from "./storage"

const BASE = process.env.NEXT_PUBLIC_API_BASE_URL

export async function logout() {
  const refresh = tokenStorage.getRefresh()

  try {
    if (refresh && BASE) {
      await fetch(`${BASE}/auth/logout/`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ refresh }),
      })
      // On ignore volontairement le résultat: même si 401/400, on purge quand même.
    }
  } catch {
    // on ignore volontairement
  } finally {
    tokenStorage.clear()
    if (typeof window !== "undefined") {
      localStorage.removeItem("acx_me")
    }
  }
}
