import { n as useMySiteApi } from "./mysite-api-context-kUTM3GNG.mjs";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
//#region ../../mysite/ui/src/portal/hooks/use-mysite-portal.ts
const PORTAL_MYSITE_KEYS = {
	profile: () => ["portal-mysite", "profile"],
	links: () => ["portal-mysite", "links"],
	favorites: () => ["portal-mysite", "favorites"],
	themes: () => ["portal-mysite", "themes"]
};
function usePortalMySiteProfile() {
	const api = useMySiteApi();
	return useQuery({
		queryKey: PORTAL_MYSITE_KEYS.profile(),
		queryFn: () => api.fetchProfile()
	});
}
function usePortalUpdateSettings(options) {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (body) => api.updateSettings(body),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.profile() });
			options?.onSuccess?.();
		}
	});
}
function usePortalLinks() {
	const api = useMySiteApi();
	return useQuery({
		queryKey: PORTAL_MYSITE_KEYS.links(),
		queryFn: () => api.listLinks()
	});
}
function usePortalCreateLink() {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (body) => api.createLink(body),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.links() });
		}
	});
}
function usePortalUpdateLink() {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: ({ linkId, body }) => api.updateLink(linkId, body),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.links() });
		}
	});
}
function usePortalDeleteLink() {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (linkId) => api.deleteLink(linkId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.links() });
		}
	});
}
function usePortalReorderLinks() {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: ({ orderedIds }) => api.reorderLinks(orderedIds),
		onMutate: async ({ optimisticItems }) => {
			await queryClient.cancelQueries({ queryKey: PORTAL_MYSITE_KEYS.links() });
			const previousData = queryClient.getQueryData(PORTAL_MYSITE_KEYS.links());
			queryClient.setQueryData(PORTAL_MYSITE_KEYS.links(), optimisticItems);
			return { previousData };
		},
		onError: (_err, _vars, context) => {
			if (context?.previousData) queryClient.setQueryData(PORTAL_MYSITE_KEYS.links(), context.previousData);
		},
		onSettled: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.links() });
		}
	});
}
function usePortalFavorites() {
	const api = useMySiteApi();
	return useQuery({
		queryKey: PORTAL_MYSITE_KEYS.favorites(),
		queryFn: () => api.listFavorites()
	});
}
function usePortalDeleteFavorite() {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (favoriteId) => api.deleteFavorite(favoriteId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.favorites() });
		}
	});
}
function usePortalReorderFavorites() {
	const api = useMySiteApi();
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: ({ orderedIds }) => api.reorderFavorites(orderedIds),
		onMutate: async ({ optimisticItems }) => {
			await queryClient.cancelQueries({ queryKey: PORTAL_MYSITE_KEYS.favorites() });
			const previousData = queryClient.getQueryData(PORTAL_MYSITE_KEYS.favorites());
			queryClient.setQueryData(PORTAL_MYSITE_KEYS.favorites(), optimisticItems);
			return { previousData };
		},
		onError: (_err, _vars, context) => {
			if (context?.previousData) queryClient.setQueryData(PORTAL_MYSITE_KEYS.favorites(), context.previousData);
		},
		onSettled: () => {
			queryClient.invalidateQueries({ queryKey: PORTAL_MYSITE_KEYS.favorites() });
		}
	});
}
function usePortalMySiteThemes() {
	const api = useMySiteApi();
	return useQuery({
		queryKey: PORTAL_MYSITE_KEYS.themes(),
		queryFn: () => api.listThemes()
	});
}
//#endregion
export { usePortalFavorites as a, usePortalMySiteThemes as c, usePortalUpdateLink as d, usePortalUpdateSettings as f, usePortalDeleteLink as i, usePortalReorderFavorites as l, usePortalCreateLink as n, usePortalLinks as o, usePortalDeleteFavorite as r, usePortalMySiteProfile as s, PORTAL_MYSITE_KEYS as t, usePortalReorderLinks as u };

//# sourceMappingURL=use-mysite-portal-ChDJ4z34.mjs.map