export const useFavoritesStore = defineStore('favorites', {
    state: () => {
        return {
            items: []
        }
    },
    getters: {
        find: (state) => (id) => state.items.find((item) => item.id == id)
    },
    actions: {
        async update(id) {
            const item = this.find(id)
            let stat = item ? 'delete' : 'add'
            const body = { favoriteProducts: id, stat }
            const { status } = await useFetch(`/api/users/fave`, {
                headers: { Authorization: useCookie('token') }, method: 'put', body
            })
            if (status.value == 'success') {
                if (item) this.items = this.items.filter((item) => item.id != id)
                else this.items.push({ id })
            }
        }
    }
})
