import { Routes } from "../routes/Routes";
import { createApiHeaders } from "./util";
import { AdminUserDTO } from "../dto/AdminUserDTO";
import { ActiveUserViewModel } from "../../chat/viewmodel/ActiveUserViewModel";
import { JsonAPI } from "../../chat/singleton/JsonAPI";

export async function getOtherUsers(
  authToken: string
): Promise<AdminUserDTO[]> {
  const response = await fetch(Routes.Admin.getOtherUsers, {
    method: "GET",
    headers: createApiHeaders(authToken),
  });

  let data = (await response.json()) as AdminUserDTO[];
  return data;
}

export async function getActiveUsers(
  authToken: string
): Promise<ActiveUserViewModel[]> {
  const response = await fetch(JsonAPI.ACTIVE_USERS_GET, {
    method: "GET",
    headers: createApiHeaders(authToken),
  });
  const data = await response.json();
  const data2 = data.map((d: any) => {
    if (d.lastActive == null) return d;

    d.lastActive = new Date(d.lastActive);
    return d;
  }) as ActiveUserViewModel[];
  return data2;
}
