import { useQuery } from 'react-query';

import usePermissions from 'src/hooks/usePermissions';
import { getPostableGroups } from 'src/services/groups';
import { UserPermissions } from 'src/utils/permissions';

const useCanSharePost = (): boolean => {
  const { hasAll: canCreateFeedPosts } = usePermissions([
    UserPermissions.CREATE_POSTS,
  ]);

  const { data } = useQuery({
    queryKey: ['groups', 'list', 'postable', 'can-share'],
    queryFn: () => getPostableGroups({ page: 1, limit: 1 }),
    enabled: !canCreateFeedPosts,
  });

  if (canCreateFeedPosts) return true;

  return (data?.data.items.length ?? 0) > 0;
};

export default useCanSharePost;
