(() => { 'use strict'; const key = 'novera-wishlist-v1'; const read = () => { try { return JSON.parse(localStorage.getItem(key) || '[]').map(Number).filter(Boolean); } catch { return []; } }; const write = (ids) => localStorage.setItem(key, JSON.stringify([...new Set(ids)])); const sync = () => { const ids = read(); document.querySelectorAll('[data-wishlist-count]').forEach((node) => { node.textContent = String(ids.length); }); document.querySelectorAll('[data-wishlist-toggle]').forEach((button) => { const active = ids.includes(Number(button.dataset.wishlistToggle)); button.classList.toggle('is-active', active); button.setAttribute('aria-pressed', active ? 'true' : 'false'); button.setAttribute('aria-label', active ? 'Удалить из избранного' : 'Добавить в избранное'); }); const target = document.querySelector('[data-wishlist-results]'); if (target && Array.isArray(window.NOVERA_CATALOG)) { const products = window.NOVERA_CATALOG.filter((item) => ids.includes(Number(item.id))); target.innerHTML = products.length ? `
В избранном пока нет товаров.
'; } }; document.addEventListener('click', (event) => { const button = event.target.closest('[data-wishlist-toggle]'); if (!button) return; event.preventDefault(); const id = Number(button.dataset.wishlistToggle); const ids = read(); write(ids.includes(id) ? ids.filter((value) => value !== id) : [...ids,id]); sync(); }); document.readyState === 'loading' ? document.addEventListener('DOMContentLoaded', sync, {once:true}) : sync(); })();