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

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

interface PullFilters {
  state?: string;
  offset?: number;
  limit?: number;
}

export function useRepoPulls(
  workspaceName: string | null,
  repoName: string | null,
  filters: PullFilters = {},
) {
  const { state, offset = 0, limit = 20 } = filters;

  return useQuery({
    queryKey: ['repo', workspaceName, repoName, 'pulls', state, offset],
    queryFn: () => {
      if (!workspaceName || !repoName) throw new Error('Missing params');
      return PullRequestsService.prList({
        workspaceName,
        repoName,
        state: state === 'all' ? undefined : state,
        offset,
        limit,
      }).then((r) => r.data);
    },
    enabled: !!workspaceName && !!repoName,
  });
}
