import { create } from "zustand";
import * as api from "@/api/wishlist";

export const useWishlist = create((set) => ({
  wishlistNotices: [],
  toggleNoticeInWishlist: async (noticeId) => {
    try {
      await api.toggleNoticeStatus(noticeId);
      set((state) => {
        const exists = state.wishlistNotices.some(
          (item) => item.noticeId == noticeId
        );
        return exists
          ? {
              wishlistNotices: state.wishlistNotices.filter(
                (item) => item.noticeId != noticeId
              ),
            }
          : {
              wishlistNotices: [
                ...state.wishlistNotices,
                { noticeId },
              ],
            };
      });
    } catch (error) {
      console.error("Error toggling wishlist notice:", error);
    }
  },
  fetchWishlist: async () => {
  try {
    const data = await api.getWishlist();
    set({ wishlistNotices: data });
  } catch (error) {
    console.error("Error fetching wishlist:", error);
  }
},
}));
