import { useQuery } from 'react-query';

import { getGroupPendingApprovalPosts } from 'src/services/groups';
import { ApprovalStatus } from 'src/types/groups';

import { useGroupMember } from '../GroupMemberContext';
import { groupsKeys } from '../queries';

export const useGroupHasPendingPostRequests = (groupId: string) => {
  const { userIsMember, isGroupAdmin } = useGroupMember();

  return useQuery(
    groupsKeys.pendingApproval.postList(groupId, { limit: 1 }),
    () =>
      getGroupPendingApprovalPosts(
        groupId,
        { limit: 1, page: 1 },
        ApprovalStatus.PENDING,
      ),
    {
      select: res => res?.data.count > 0,
      enabled: userIsMember && isGroupAdmin,
    },
  );
};
