// composables/useCart.ts
// In a real Vue app:
// import { ref, onMounted, onUnmounted } from 'vue'
type Ref<T> = { value: T };
declare function ref<T>(value: T): Ref<T>;
declare function onMounted(fn: () => void | Promise<void>): void;
declare function onUnmounted(fn: () => void): void;
// assumes shoppex is available (via SDK import in your app entry)
declare const shoppex: any
export function useCart() {
const items = ref(shoppex.getCart())
const itemCount = ref(shoppex.getCartItemCount())
function refresh() {
items.value = shoppex.getCart()
itemCount.value = shoppex.getCartItemCount()
}
onMounted(() => {
window.addEventListener('shoppex:cart-changed', refresh)
window.addEventListener('storage', refresh)
})
onUnmounted(() => {
window.removeEventListener('shoppex:cart-changed', refresh)
window.removeEventListener('storage', refresh)
})
return { items, itemCount }
}
// After modifying cart:
function addItem(productId: string, variantId: string) {
shoppex.addToCart(productId, variantId)
window.dispatchEvent(new CustomEvent('shoppex:cart-changed'))
}