export function mealToShoppingListSources(meal) { const sources = []; for (const recipe of meal.recipes) { for (const ingredient of recipe.ingredients) { sources.push({ ingredient, meal, recipe, }); } } for (const ingredient of meal.extra_ingredients) { sources.push({ ingredient, meal, }); } return sources; } export function removeMealFromShoppingListSources(sources, meal) { return sources.filter(source => source.meal?.id !== meal.id); } export function groupByProduct(sources) { const noProductKey = "no-product"; const noProduct = { name: "No product", }; const grouped = {}; for (const source of sources) { const key = source.ingredient?.product?.id ?? noProductKey; if (!key) { continue; } if (!grouped[key]) { grouped[key] = { product: source.ingredient.product ?? noProduct, sources: [], }; } grouped[key].sources.push(source); } return Object.values(grouped); }