import type { CreateRecipeFormDirtyEntries, CreateRecipeFormEntries } from "../pages/Create"; import { isRecipeMeal, type RecipeIngredient, type RecipeInstruction } from "../types/recipe"; export interface CreateRecipeFormValues { title: string; description: string; prepTime: string; cookTime: string; servingSize: string; category: string; difficulty: string; ingredients: RecipeIngredient[]; instructions: RecipeInstruction[]; } export function validateCreateRecipeForm(values: CreateRecipeFormValues, dirty: CreateRecipeFormDirtyEntries): CreateRecipeFormEntries { return { title: dirty.title ? values.title.length >= 1 && values.title.length <= 128 : true, description: dirty.description ? values.description.length >= 1 && values.description.length <= 1000 : true, prepTime: dirty.prepTime ? values.prepTime !== "" && Number(values.prepTime) >= 0 && Number(values.prepTime) <= 300 : true, cookTime: dirty.cookTime ? values.cookTime !== "" && Number(values.cookTime) >= 0 && Number(values.cookTime) <= 300 : true, servingSize: dirty.servingSize ? values.servingSize !== "" && Number(values.servingSize) >= 1 && Number(values.servingSize) <= 16 : true, category: dirty.category ? values.category !== "" && isRecipeMeal(values.category) : true, difficulty: dirty.difficulty ? values.difficulty !== "" && Number(values.difficulty) >= 1 && Number(values.difficulty) <= 5 : true, ingredients: values.ingredients.map(ingredient => { if (!dirty.ingredients[ingredient.Id]) { return { id: ingredient.Id, valid: true }; } let valid = true; if (ingredient.Name.trim() === "") valid = false; if (ingredient.Unit === "") valid = false; if (ingredient.Amount <= 0) valid = false; return { id: ingredient.Id, valid }; }), instructions: values.instructions.map(instruction => { if (!dirty.instructions[instruction.Id]) { return { id: instruction.Id, valid: true } } return { id: instruction.Id, valid: instruction.Content.trim() !== "" } }), } }