import AsyncStorage from '@react-native-async-storage/async-storage'; import { Recipe } from '../types'; import { DEFAULT_RECIPES } from './defaultRecipes'; const CUSTOM_RECIPES_KEY = '@camrecipe_pro:custom_recipes'; export async function getCustomRecipes(): Promise { try { const data = await AsyncStorage.getItem(CUSTOM_RECIPES_KEY); if (!data) return []; return JSON.parse(data); } catch (error) { console.error('Failed to load custom recipes:', error); return []; } } export async function getAllRecipes(): Promise { const custom = await getCustomRecipes(); return [...DEFAULT_RECIPES, ...custom]; } export async function saveCustomRecipe(recipe: Omit): Promise { const custom = await getCustomRecipes(); const newRecipe: Recipe = { ...recipe, id: `custom_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, isCustom: true, }; const updated = [...custom, newRecipe]; await AsyncStorage.setItem(CUSTOM_RECIPES_KEY, JSON.stringify(updated)); return newRecipe; } export async function deleteCustomRecipe(id: string): Promise { const custom = await getCustomRecipes(); const updated = custom.filter(r => r.id !== id); await AsyncStorage.setItem(CUSTOM_RECIPES_KEY, JSON.stringify(updated)); }