type ApiResponse = {
  status: number;
  json(): Promise<{ success?: boolean; trackId?: string; error?: string }>;
};

export const favoritesPageRoute = "/en/dashboard/favorites";
export const removeControlName = "Remove";

export async function removeFavorite(
  trackId: string,
  request: (path: string, init: { method: "DELETE" }) => Promise<ApiResponse>,
): Promise<{ visible: string; removedTrackId: string | null }> {
  const response = await request(`/api/dashboard/customer/favorites/${trackId}`, {
    method: "DELETE",
  });
  const body = await response.json();

  if (response.status === 200 && body.success) {
    return { visible: "Removed from favorites", removedTrackId: trackId };
  }
  if (response.status === 404 && body.error === "favorite_not_found") {
    return { visible: "Favorite no longer exists", removedTrackId: null };
  }
  if (response.status === 401) {
    return { visible: "Sign in to view favorites", removedTrackId: null };
  }
  throw new Error("Unexpected favorite response");
}
