import { useQuery } from 'react-query';

import usePermissions from 'src/hooks/usePermissions';
import { getFeedPendingApprovalPosts } from 'src/services/posts';
import { ApprovalStatus } from 'src/types/groups';
import { UserPermissions } from 'src/utils/permissions';

import { feedKeys } from '../queries';

export const useFeedHasPendingPostRequests = () => {
  const { hasAll: isGroupAdmin } = usePermissions([
    UserPermissions.CAN_APPROVE_FEED_POSTS,
  ]);

  return useQuery(
    feedKeys.pendingApproval.postList({ limit: 1 }),
    () =>
      getFeedPendingApprovalPosts(
        { limit: 1, page: 1 },
        ApprovalStatus.PENDING,
      ),
    {
      select: res => res?.data.count > 0,
      enabled: isGroupAdmin,
    },
  );
};
