import { adminListUsers } from "$lib/services";

export async function load({ fetch, url, cookies }) {
  const token = cookies.get("session_token") || "";
  const role = url.searchParams.get("role") || undefined;
  const search = url.searchParams.get("search") || undefined;
  const offset = parseInt(url.searchParams.get("offset") || "0", 10);
  const limit = 50;

  const result = await adminListUsers({ role, search, limit, offset }, fetch, token).catch(() => ({
    items: [],
    total: 0,
  }));

  return { ...result, role, search, offset, limit };
}
