// [REQ D6]  Render props – DataRenderer przekazuje dane i stan jako children(data, isLoading, error)
// [REQ T7]  Render props i HOC – wzorzec render props do abstrakcji logiki pobierania danych
import { useQuery } from '@tanstack/react-query';

/**
 * Render-prop component — fetches data via React Query and delegates
 * rendering entirely to `children`, which is called as a function.
 *
 * Usage:
 *   <DataRenderer queryKey={['services']} queryFn={fetchServices}>
 *     {({ data, isLoading, isError }) => ( ... )}
 *   </DataRenderer>
 */
const DataRenderer = ({ queryKey, queryFn, staleTime, children }) => {
  const result = useQuery({ queryKey, queryFn, staleTime: staleTime ?? 60_000 });
  return children(result);
};

export default DataRenderer;
