Response
Since writing files isn't permitted here, I'll just give you the content directly.
```ts
// src/stores/cart.ts
import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
export interface CartItem {
id: string
name: string
price: number
quantity: number
}
export const useCartStore = defineStore('cart', () => {
const items = ref<CartItem[]>([])
function add(item: Omit<CartItem, 'quantity'>, quantity = 1) {
const existing = items.value.find((i) => i.id === item.id)
if (existing) {
existing.quantity += quantity
} else {
items.value.push({ ...item, quantity })
}
}
function remove(id: string) {
const index = items.value.findIndex((i) => i.id === id)
if (index !== -1) {
items.value.splice(index, 1)
}
}
const total = computed(() =>
items.value.reduce((sum, item) => sum + item.price * item.quantity, 0),
)
return { items, add, remove, total }
})
```
`add` merges duplicate IDs by incrementing quantity; `remove` takes an item ID; `total` is a computed getter summing `price * quantity`. If you want it split further (e.g. `removeAll`, `clear`, persistence) let me know.