(() => { 'use strict'; const cfg = window.CanisproutCore || {}; const storageKey = 'canisproutWishlist'; const getLocal = () => { try { return JSON.parse(localStorage.getItem(storageKey) || '[]').map(Number).filter(Boolean); } catch { return []; } }; const setLocal = (ids) => localStorage.setItem(storageKey, JSON.stringify([...new Set(ids.map(Number).filter(Boolean))])); let wishlist = getLocal(); const syncButtons = () => { document.querySelectorAll('[data-wishlist-count]').forEach((el) => { el.textContent = String(wishlist.length); }); document.querySelectorAll('[data-wishlist-product]').forEach((button) => { const active = wishlist.includes(Number(button.dataset.wishlistProduct)); button.classList.toggle('is-active', active); button.setAttribute('aria-pressed', String(active)); button.textContent = active ? '♥' : '♡'; }); }; const pushRemote = async () => { if (!cfg.loggedIn) return; await fetch(cfg.restUrl + 'wishlist', {method:'POST',headers:{'Content-Type':'application/json','X-WP-Nonce':cfg.nonce},body:JSON.stringify({ids:wishlist})}); }; const refreshCartCount = async () => { try { const response = await fetch('/wp-json/wc/store/v1/cart', {credentials:'same-origin'}); if (!response.ok) return; const cart = await response.json(); document.querySelectorAll('[data-cart-count]').forEach((el)=>{el.textContent=String(cart.items_count||0);}); } catch (error) {} }; const initWishlist = async () => { if (cfg.loggedIn) { try { const r = await fetch(cfg.restUrl + 'wishlist', {headers:{'X-WP-Nonce':cfg.nonce}}); if (r.ok) { const remote = await r.json(); wishlist = [...new Set([...wishlist,...remote.map(Number)])]; setLocal(wishlist); await pushRemote(); } } catch(e) {} } syncButtons(); renderWishlistPage(); }; document.addEventListener('click', async (event) => { const button = event.target.closest('[data-wishlist-product]'); if (button) { const id = Number(button.dataset.wishlistProduct); wishlist = wishlist.includes(id) ? wishlist.filter((x)=>x!==id) : [...wishlist,id]; setLocal(wishlist); syncButtons(); await pushRemote(); renderWishlistPage(); } const quick = event.target.closest('[data-quickview-product]'); if (quick) openQuickview(Number(quick.dataset.quickviewProduct)); if (event.target.closest('[data-quickview-close]')) closeQuickview(); }); const quickview = document.querySelector('[data-quickview]'); const openQuickview = async (id) => { if (!quickview) return; const content = quickview.querySelector('[data-quickview-content]'); content.innerHTML='
Loading…
'; quickview.hidden=false; quickview.setAttribute('aria-hidden','false'); document.body.style.overflow='hidden'; try { const r=await fetch(cfg.restUrl+'product/'+id); const p=await r.json(); if(!r.ok) throw new Error(p.message||'Error'); content.innerHTML=`${p.type==='simple'&&p.purchasable&&p.inStock?'Add to cart':'Choose options'}
${cfg.strings?.noResults||'No results'}
`;results.hidden=false;}catch(e){results.hidden=true;}},220);}); document.addEventListener('click',(e)=>{if(!e.target.closest('[data-live-search-form]')&&results)results.hidden=true;}); const renderWishlistPage = async () => { const root=document.querySelector('[data-wishlist-page]'); if(!root)return;if(!wishlist.length){root.innerHTML='';return;} const products=await Promise.all(wishlist.map(id=>fetch(cfg.restUrl+'product/'+id).then(r=>r.ok?r.json():null).catch(()=>null))); root.innerHTML=''; syncButtons(); }; document.querySelector('[data-demo-newsletter]')?.addEventListener('submit',(e)=>{e.preventDefault();alert('Demo form only. Connect a consent-aware email provider before use.');}); initWishlist(); refreshCartCount(); if (window.jQuery) window.jQuery(document.body).on('added_to_cart removed_from_cart updated_cart_totals', refreshCartCount); })();