import { toCarDetailed } from '@/adapters/toCarDetailed.adapter';
import { getCarDetailedRequest } from '@/api/detailedCar';
import { Car } from '@/types/detailed-car.types';

type getCarsResponse =
  | {
      detailedCar: Car;
      error: false;
    }
  | {
      detailedCar: null;
      error: true;
    };

export const getDetailedCar = async (slug: string): Promise<getCarsResponse> => {
  try {
    const response = await getCarDetailedRequest(slug);
    const detailedCar = toCarDetailed(response.car);

    return {
      error: false,
      detailedCar,
    };
  } catch {
    return {
      error: true,
      detailedCar: null,
    };
  }
};
