import { useQuery } from '@tanstack/react-query';

import { PullRequestsService } from '@/client/services/PullRequestsService';

export function usePrLabelRelations(
  workspaceName: string | null,
  repoName: string | null,
  number: number | null,
  limit = 50,
  offset = 0,
) {
  return useQuery({
    queryKey: [
      'workspace',
      workspaceName,
      'repos',
      repoName,
      'prs',
      number,
      'labels',
      { limit, offset },
    ],
    queryFn: () => {
      if (!workspaceName || !repoName || number === null) throw new Error('Missing params');
      return PullRequestsService.prListLabelRelations({
        workspaceName,
        repoName,
        number,
        limit,
        offset,
      }).then((r) => r.data);
    },
    enabled: !!workspaceName && !!repoName && number !== null,
  });
}
