import { create } from "zustand";
import topics from "./topics.json";

export const useStore = create((set, get) => ({
  topics,
  topicVersions: {},
  getTopicAtIndex: (index) => {
    const topicId = get().topics.allIds[index];
    if (!topicId) {
      return null;
    }
    return get().topics.byId[topicId];
  },
  selectTopicVersion: (id, version) =>
    set({ topicVersions: { ...get().topicVersions, [id]: version } }),
  selectedTopic: null,
  selectTopic: (id) => {
    const topic = get().topics.byId[id];
    if (topic) {
      set({ selectedTopic: topic });
    }
  },
  config: {
    displayTitle: true,
    narrowMode: true,
  },
  changeConfig: (config) => set({ config: { ...get().config, ...config } }),
}));
