import { fetcher } from '@/src/hooks/fetcher';
import { COMMON_FONTS } from '@/src/utils/typography';
import { FontOption } from '@/types/typography';
import useSWR from 'swr';
import { ApiResponse } from '../types/api';

export const useGoogleFonts = (searchTerm = '') => {
  const { data, error } = useSWR<ApiResponse<FontOption[]>>(
    process.env.NODE_ENV === 'production' ? `/api/typography?search=${searchTerm}` : null,
    fetcher
  );
  if (searchTerm === '' || process.env.NODE_ENV === 'development') {
    return {
      fonts: COMMON_FONTS,
      isLoading: false,
      isError: false,
    };
  }

  return {
    fonts: data?.data,
    isLoading: !error && !data,
    isError: error,
  };
};
