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

import { api } from "../apiHelper";

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

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

	const { data } = await api.get(
		`/notification/sub${userId ? `?userId=${userId}` : ""}`,
	);

	return data.data;
};
