import { toCarCard } from '@/adapters/toCarCard.adapter';
import { toHomeCarCard } from '@/adapters/toHomeCarCard.adapter';
import { getCarsRequest } from '@/api/cars';
import { CardUIType } from '@/types/car-card.type';
import { HomeCarCard } from '@/types/home-car-card.type';

type getCarsResponse =
  | {
      carCards: CardUIType[];
      homeCarCards: HomeCarCard[];
      error: false;
    }
  | {
      carCards: null;
      homeCarCards: null;
      error: true;
    };

export const getCars = async (): Promise<getCarsResponse> => {
  try {
    const response = await getCarsRequest();
    const carCards = toCarCard(response.cars);
    const homeCarCards = toHomeCarCard(response.cars);
    return {
      error: false,
      carCards,
      homeCarCards,
    };
  } catch {
    return {
      error: true,
      carCards: null,
      homeCarCards: null,
    };
  }
};
