import { create } from "zustand";
import { persist } from "zustand/middleware";

export interface BasicFeedItem {
  displayName: string;
}

type FeedDefsState = {
  feeds: Record<string, BasicFeedItem>;
  setFeedDef: (id: string, feed: BasicFeedItem) => void;
  defaultFeed: string;
  setDefaultFeed: (feed: string) => void;
};

export const useFeedDefsStore = create<FeedDefsState>()(
  persist(
    (set) => ({
      feeds: {},
      setFeedDef: (id, feed) =>
        set((state) => ({
          feeds: {
            ...state.feeds,
            [id]: feed,
          },
        })),
      defaultFeed: "timeline",
      setDefaultFeed: (feed) =>
        set(() => ({
          defaultFeed: feed,
        })),
    }),
    {
      name: "feed-defs",
      partialize: (state) => ({
        feeds: state.feeds,
        defaultFeed: state.defaultFeed,
      }),
    }
  )
);
