import { keepPreviousData, queryOptions } from "@tanstack/react-query";

import { authClient } from "../auth-client";

export function getUsers() {
	return queryOptions({
		queryKey: ["getUsers"],
		queryFn: () => fetch(),
		staleTime: 5000,
		refetchOnWindowFocus: true,
		placeholderData: keepPreviousData,
	});
}

const fetch = async () => {
	if (window.location.hostname === "localhost") {
		await new Promise((res) => setTimeout(res, 1500));
	}

	const { data, error } = await authClient.admin.listUsers({
		query: {
			// searchValue: "some name",
			// searchField: "name",
			// searchOperator: "contains",
			limit: 100,
			offset: 0,
			sortBy: "name",
			// sortDirection: "desc",
			// filterField: "email",
			// filterValue: "hello@example.com",
			// filterOperator: "eq",
		},
	});

	if (error) {
		return error;
	}

	return data.users;
};
