import { QueryFunction, QueryKey, UseQueryResult, useQuery } from "@tanstack/react-query";

export interface IUseManyQueryOptions<TUseManyQueryData = unknown, _TUseManyQueryError = unknown> {
  queryKey: QueryKey;
  queryFn: QueryFunction<TUseManyQueryData, QueryKey>;
  enabled?: boolean;
  select?: (data: TUseManyQueryData) => TUseManyQueryData;
  queryOptions?: any;
}

export function useMany<TUseManyQueryData, TUseManyQueryError>(
  options: IUseManyQueryOptions<TUseManyQueryData, TUseManyQueryError>
): UseQueryResult<TUseManyQueryData, TUseManyQueryError> {
  const { queryKey, queryFn, enabled, select, queryOptions } = options;

  const queryResponse = useQuery<TUseManyQueryData, TUseManyQueryError>({
    queryKey,
    queryFn,
    keepPreviousData: true,
    ...queryOptions,
    enabled,
    select,
  });

  return queryResponse;
}
