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

import { regionsQueryKeys } from '../regions-query-keys';
import { regionsApi, type PostGetRegionsReturn } from '../regions-api';

import type { BaseAxiosError } from '../../../../types';

export const useGetRegionsQuery = (
  megaregion: number | 'all' | undefined,
  country: number | 'all' | undefined,
  token: string,
  enabled: boolean
) => {
  return useQuery<PostGetRegionsReturn, BaseAxiosError>({
    queryKey: regionsQueryKeys.getRegions(token, megaregion, country),
    queryFn: async () => {
      const response = await regionsApi.getRegions(token, megaregion, country);
      return response.data;
    },
    enabled
  });
};
