import { RECIPES } from '../data/catalog.js?v=2'; import { MEAL_SLOTS } from '../planner/mealSlots.js'; import { applyFilters, getFilterState, getFilteredCount, refreshRecipeList } from './RecipeList.js'; function escapeHtml(s) { return String(s) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } function collectAllTags() { const tags = new Set(); for (const r of Object.values(RECIPES)) { for (const t of (r.tags || [])) tags.add(t); } return [...tags].sort((a, b) => a.localeCompare(b, 'pl')); } export function getFilterHTML() { return ` `; } let localSlots = []; let localTags = []; let localMaxMinutes = 120; function renderSlotChips() { const wrap = document.getElementById('filter-slot-chips'); if (!wrap) return; wrap.innerHTML = MEAL_SLOTS.map((slot) => { const active = localSlots.includes(slot.id); const cls = active ? 'px-4 py-2 bg-gray-900 text-white rounded-full text-sm font-medium transition-colors' : 'px-4 py-2 bg-gray-100 text-gray-700 hover:bg-gray-200 rounded-full text-sm font-medium transition-colors'; return ``; }).join(''); wrap.querySelectorAll('[data-filter-slot]').forEach((btn) => { btn.addEventListener('click', () => { const id = btn.dataset.filterSlot; const idx = localSlots.indexOf(id); if (idx >= 0) localSlots.splice(idx, 1); else localSlots.push(id); renderSlotChips(); updateResultCount(); }); }); } function renderTagChips() { const wrap = document.getElementById('filter-tag-chips'); if (!wrap) return; const allTags = collectAllTags(); wrap.innerHTML = allTags.map((tag) => { const active = localTags.includes(tag.toLowerCase()); const cls = active ? 'px-4 py-2 bg-gray-900 text-white rounded-full text-sm font-medium transition-colors' : 'px-4 py-2 bg-gray-100 text-gray-700 hover:bg-gray-200 rounded-full text-sm font-medium transition-colors'; return ``; }).join(''); wrap.querySelectorAll('[data-filter-tag]').forEach((btn) => { btn.addEventListener('click', () => { const tag = btn.dataset.filterTag.toLowerCase(); const idx = localTags.indexOf(tag); if (idx >= 0) localTags.splice(idx, 1); else localTags.push(tag); renderTagChips(); updateResultCount(); }); }); } function updateResultCount() { applyFilters({ slots: localSlots, tags: localTags, maxMinutes: localMaxMinutes }); const count = getFilteredCount(); const applyBtn = document.getElementById('filter-apply-btn'); if (applyBtn) applyBtn.textContent = `Pokaż ${count} wyników`; } export function setupFilter() { const timeSlider = document.getElementById('prep-time-slider'); const timeDisplay = document.getElementById('time-display'); if (timeSlider) { timeSlider.addEventListener('input', (e) => { const val = Number(e.target.value); localMaxMinutes = val; timeDisplay.textContent = val >= 120 ? 'ponad 120 min' : `${val} min`; updateResultCount(); }); } document.getElementById('filter-close-btn')?.addEventListener('click', () => { window.closeFilters(); }); document.getElementById('filter-apply-btn')?.addEventListener('click', () => { applyFilters({ slots: localSlots, tags: localTags, maxMinutes: localMaxMinutes }); refreshRecipeList(); window.closeFilters(); }); document.getElementById('filter-clear-btn')?.addEventListener('click', () => { localSlots = []; localTags = []; localMaxMinutes = 120; if (timeSlider) timeSlider.value = '120'; if (timeDisplay) timeDisplay.textContent = '120 min'; renderSlotChips(); renderTagChips(); applyFilters({ slots: [], tags: [], maxMinutes: 120 }); updateResultCount(); }); window.openFilters = () => { const state = getFilterState(); localSlots = [...state.slots]; localTags = [...state.tags]; localMaxMinutes = state.maxMinutes; if (timeSlider) timeSlider.value = String(localMaxMinutes); if (timeDisplay) timeDisplay.textContent = localMaxMinutes >= 120 ? 'ponad 120 min' : `${localMaxMinutes} min`; renderSlotChips(); renderTagChips(); updateResultCount(); const fv = document.getElementById('filter-view'); fv.classList.remove('hidden'); fv.classList.add('flex'); }; window.closeFilters = () => { const fv = document.getElementById('filter-view'); fv.classList.add('hidden'); fv.classList.remove('flex'); }; }