import type { RecipeDto } from "../dtos/RecipeDto";
import { get, postJson } from "../utils/requests";


/**
 * Util for handling the recipe api
 */
// read base url from .env file
const BASE_URL = import.meta.env.VITE_API_BASE;

/**
 * URL for handling recipes
 */
const RECIPE_URL = `${BASE_URL}/recipe`

/**
 * Load a single recipe
 * @param id ID of the recipe to load
 * @returns A single recipe
 */
export async function fetchRecipe(id: string): Promise<RecipeDto> {
  const res = await get(`${RECIPE_URL}/${id}`)
  return res.json()
}

/**
 * Either create a new recipe or update an existing one
 * @param recipe Recipe to create or update
 * @returns Saved recipe
 */
export async function createOrUpdateRecipe(recipe: RecipeDto): Promise<RecipeDto> {
  const res = await postJson(RECIPE_URL + "/create-or-update", JSON.stringify(recipe));
  return res.json();
}
