import { RECIPES } from '../data/catalog.js'; import { MEAL_SLOTS } from '../planner/mealSlots.js'; function escapeHtml(s) { return String(s) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } const slotLabelMap = Object.fromEntries(MEAL_SLOTS.map((s) => [s.id, s.label])); function slotLabelsFor(recipe) { return (recipe.allowedSlots || []) .map((id) => slotLabelMap[id]) .filter(Boolean); } let filterState = { query: '', slots: [], tags: [], maxMinutes: 120, }; function matchesFilters(recipe) { const { query, slots, tags, maxMinutes } = filterState; if (query) { const q = query.toLowerCase(); const haystack = `${recipe.title} ${recipe.description || ''} ${(recipe.tags || []).join(' ')}`.toLowerCase(); if (!haystack.includes(q)) return false; } if (slots.length > 0) { if (!recipe.allowedSlots.some((s) => slots.includes(s))) return false; } if (tags.length > 0) { const recipeTags = (recipe.tags || []).map((t) => t.toLowerCase()); if (!tags.some((t) => recipeTags.includes(t.toLowerCase()))) return false; } if (recipe.minutes > maxMinutes) return false; return true; } function getFilteredRecipes() { return Object.values(RECIPES).filter(matchesFilters); } function renderRecipeCard(recipe) { const labels = slotLabelsFor(recipe); return `
${recipe.image ? `${escapeHtml(recipe.title)}` : `${escapeHtml(recipe.thumbLabel)}`}

${escapeHtml(recipe.title)}

${escapeHtml(recipe.description || '')}

${recipe.minutes} min
${recipe.nutritionPerServing.kcal} kcal
${labels.map((l) => `${escapeHtml(l)}`).join('')}
`; } function renderGrid() { const grid = document.getElementById('recipe-grid'); if (!grid) return; const recipes = getFilteredRecipes(); if (recipes.length === 0) { grid.innerHTML = `

Brak wyników

Zmień kryteria wyszukiwania lub filtry

`; return; } grid.innerHTML = recipes.map(renderRecipeCard).join(''); } export function getRecipeListHTML() { return `
`; } export function getFilterState() { return filterState; } export function applyFilters(newState) { Object.assign(filterState, newState); renderGrid(); } export function getFilteredCount() { return getFilteredRecipes().length; } export function refreshRecipeList() { renderGrid(); } export function setupRecipeList() { renderGrid(); document.getElementById('recipe-search-input')?.addEventListener('input', (e) => { filterState.query = e.target.value.trim(); renderGrid(); }); }