import { RECIPES } from '../data/catalog.js?v=2';
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]));
const DEFAULT_MIN_MINUTES = 5;
const DEFAULT_MAX_MINUTES = 120;
const SEARCH_SHELL_BASE_SHADOW =
'inset 0 1px 0 rgba(255,255,255,0.045), 0 0 0 1px rgba(255,255,255,0.015)';
const SEARCH_HEADER_SCROLLED_SHADOW = '0 10px 16px rgba(0,0,0,0.34)';
function slotLabelsFor(recipe) {
return (recipe.allowedSlots || [])
.map((id) => slotLabelMap[id])
.filter(Boolean);
}
let filterState = {
query: '',
slots: [],
tags: [],
minMinutes: DEFAULT_MIN_MINUTES,
maxMinutes: DEFAULT_MAX_MINUTES,
};
function matchesFilters(recipe) {
const { query, slots, tags, minMinutes, maxMinutes } = filterState;
if (query) {
const q = query.toLowerCase();
const haystack = `${recipe.title} ${(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 (minMinutes > DEFAULT_MIN_MINUTES && recipe.minutes < minMinutes) return false;
if (maxMinutes < DEFAULT_MAX_MINUTES && 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.thumbLabel)}`}
${escapeHtml(recipe.title)}
${recipe.minutes} min
${recipe.nutritionPerServing.kcal} kcal
${labels.map((l) => `${escapeHtml(l)}`).join('')}
`;
}
function syncRecipeScrollShadow() {
const scroll = document.getElementById('recipe-scroll');
const shadow = document.getElementById('recipe-top-bar-shadow');
const header = document.getElementById('recipe-top-bar');
const searchShell = document.getElementById('recipe-search-shell');
if (!shadow || !header || !searchShell) return;
if (!scroll) {
shadow.style.opacity = '0';
header.style.boxShadow = 'none';
searchShell.style.boxShadow = SEARCH_SHELL_BASE_SHADOW;
return;
}
const isScrolled = scroll.scrollTop > 6;
shadow.style.opacity = isScrolled ? '1' : '0';
header.style.boxShadow = isScrolled ? SEARCH_HEADER_SCROLLED_SHADOW : 'none';
searchShell.style.boxShadow = SEARCH_SHELL_BASE_SHADOW;
}
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
`;
requestAnimationFrame(syncRecipeScrollShadow);
return;
}
grid.innerHTML = recipes.map(renderRecipeCard).join('');
requestAnimationFrame(syncRecipeScrollShadow);
}
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();
});
document.getElementById('recipe-scroll')?.addEventListener('scroll', syncRecipeScrollShadow);
syncRecipeScrollShadow();
}