import { create } from "zustand";
import { persist, createJSONStorage } from 'zustand/middleware';
import type { ClubInfo } from "../types";

interface Store {
  clubs: ClubInfo[];
  add(club: ClubInfo): void;
  unFav(id: string): void;
  isFav(id: string): boolean;
}
export const useClubStore = create(persist<Store>((set, get) => {
  return {
    clubs: [],
    add: (club: ClubInfo) => {
      set({ clubs: [...get().clubs, club] });
    },
    unFav: (id: string) => {
      const newClubs = get().clubs.filter(e => e.id !== id);
      set({ clubs: newClubs });
    },
    isFav: (id: string) => {
      return get().clubs.findIndex(e => e.id === id) > -1;
    },
  }
}, {
  name: 'club-storage',
  storage: createJSONStorage(() => localStorage),
}));