
async function getUserList(q, token) {
  if (!token) {
    return "not_init";
  }




  const response = await fetch(`/api/chat/users` + (q ? `?q=${encodeURIComponent(q)}` : ""), {
    method: "GET",
    headers: {
      "Authorization": `Bearer ${token}`
    }
  });
  if (!response.ok) {
    const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
    throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  }
  const data = await response.json();
  return data.users || [];
}

async function getOnlineUserCount(token) {
  if (!token) {
    return "not_init";
  }

  const response = await fetch(`/api/chat/onlines`, {
    method: "GET",
    headers: {
      "Authorization": `Bearer ${token}`
    }
  });

  const data = await response.json();
  if (!data.success){
    throw new Error(data.error.message);
  }
  return data.data.count || 0;
}

export { getUserList, getOnlineUserCount };
