import fetcher from '@/lib/fetcher';
import useSWRHook, { type SWRConfiguration } from 'swr';

const DEFAULT_OPTIONS: SWRConfiguration = {
  shouldRetryOnError: false,
  revalidateOnFocus: false,
};

export const useSWR = (url?: string, options?: SWRConfiguration) => {
  return useSWRHook(url ?? null, fetcher, {
    ...DEFAULT_OPTIONS,
    ...options,
  });
};

export const useApiSWR = <T = unknown,>(
  url: string | null,
  options?: SWRConfiguration,
) => {
  const apiUrl = url ? `/api${url.startsWith('/') ? url : `/${url}`}` : null;

  return useSWRHook<T>(apiUrl, fetcher, {
    ...DEFAULT_OPTIONS,
    ...options,
  });
};
