import { RECIPES } from '../data/catalog.js?v=8'; import { getRecipeGridSectionHTML, renderRecipeGrid } from '../ui/recipeGrid.js'; const DEFAULT_MIN_MINUTES = 5; const DEFAULT_MAX_MINUTES = 120; /** Jak w spiżarni — cień „pigówek” i powłoki wyszukiwania */ const SEARCH_SHELL_SHADOW = '0 5px 10px rgba(0,0,0,0.16), 0 14px 22px rgba(0,0,0,0.24), 0 22px 34px rgba(0,0,0,0.18), inset 0 1px 0 rgba(255,255,255,0.04)'; let filterState = { query: '', slots: [], tags: [], minMinutes: DEFAULT_MIN_MINUTES, maxMinutes: DEFAULT_MAX_MINUTES, }; let isSearchExpanded = false; let recipeListDocListenersBound = false; 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 syncRecipeScrollShadow() { const searchShell = document.getElementById('recipe-search-shell'); if (searchShell) { searchShell.style.boxShadow = SEARCH_SHELL_SHADOW; } } function syncRecipeTopbarUI() { const defaultRow = document.getElementById('recipe-default-row'); const searchShell = document.getElementById('recipe-search-shell'); const showSearch = isSearchExpanded; if (defaultRow) { defaultRow.style.opacity = showSearch ? '0' : '1'; defaultRow.style.transform = showSearch ? 'translateY(-2px) scale(0.98)' : 'translateY(0) scale(1)'; defaultRow.style.pointerEvents = showSearch ? 'none' : 'auto'; } if (searchShell) { searchShell.style.opacity = showSearch ? '1' : '0'; searchShell.style.transform = showSearch ? 'translateY(0) scale(1)' : 'translateY(-2px) scale(0.98)'; searchShell.style.pointerEvents = showSearch ? 'auto' : 'none'; searchShell.style.boxShadow = SEARCH_SHELL_SHADOW; } } function closeSearch() { const input = document.getElementById('recipe-search-input'); const hadQuery = Boolean(input?.value); if (input) { input.value = ''; input.blur(); } filterState.query = ''; isSearchExpanded = false; syncRecipeTopbarUI(); if (hadQuery) renderGrid(); } function openSearch() { isSearchExpanded = true; window.closeFilters?.(); syncRecipeTopbarUI(); window.requestAnimationFrame(() => { document.getElementById('recipe-search-input')?.focus(); }); } function renderGrid() { const grid = document.getElementById('recipe-grid'); const emptyState = document.getElementById('recipe-empty-state'); if (!grid) return; renderRecipeGrid({ gridEl: grid, emptyStateEl: emptyState, recipes: getFilteredRecipes(), showSlotLabels: false, cardClassName: 'recipe-list-card', }); syncRecipeTopbarUI(); requestAnimationFrame(syncRecipeScrollShadow); } export function getRecipeListHTML() { return `

Katalog przepisów

${getRecipeGridSectionHTML({ scrollId: 'recipe-scroll', gridId: 'recipe-grid', emptyStateId: 'recipe-empty-state', scrollClassName: 'relative flex-1 overflow-y-auto px-4 pt-[5.35rem] pb-24 bg-[#2d2e2b]', gridClassName: 'grid grid-cols-3 gap-2 bg-[#2d2e2b]', emptyTitle: 'Brak wyników', emptyMessage: 'Zmień kryteria wyszukiwania lub filtry', })}
`; } 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-search-input')?.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeSearch(); }); document.getElementById('recipe-search-toggle')?.addEventListener('click', () => openSearch()); document.getElementById('recipe-search-close')?.addEventListener('click', () => closeSearch()); document.getElementById('recipe-filter-btn')?.addEventListener('click', (e) => { e.stopPropagation(); if (isSearchExpanded) { isSearchExpanded = false; syncRecipeTopbarUI(); } window.openFilters?.('recipes'); }); document.getElementById('recipe-grid')?.addEventListener('click', (e) => { const card = e.target.closest('.recipe-browser-card'); if (!card) return; const recipeId = card.getAttribute('data-recipe-id'); if (recipeId) window.openRecipeDetail?.(recipeId); }); document.getElementById('recipe-scroll')?.addEventListener('scroll', syncRecipeScrollShadow); syncRecipeScrollShadow(); if (!recipeListDocListenersBound) { recipeListDocListenersBound = true; document.addEventListener('keydown', (e) => { if (e.key !== 'Escape' || !isSearchExpanded) return; if (!document.getElementById('main-view')?.classList.contains('hidden')) { closeSearch(); } }); } }