// stores/cart.ts
// In a real Svelte app:
// import { writable } from 'svelte/store'
declare function writable<T>(initial: T): {
subscribe: (run: (value: T) => void) => () => void;
set: (value: T) => void;
};
// assumes shoppex is available (via SDK import in your app entry)
declare const shoppex: any
export const cart = writable([])
// Call this after `shoppex.init()` (for example in your root component's onMount)
export function startCartSync() {
const refresh = () => cart.set(shoppex.getCart())
refresh()
window.addEventListener('shoppex:cart-changed', refresh)
window.addEventListener('storage', refresh)
return () => {
window.removeEventListener('shoppex:cart-changed', refresh)
window.removeEventListener('storage', refresh)
}
}
export function addItem(productId, variantId) {
shoppex.addToCart(productId, variantId)
window.dispatchEvent(new CustomEvent('shoppex:cart-changed'))
}