import { create } from "zustand";

interface ProjectState {
  currentProjectId: string | null;
  setCurrentProjectId: (id: string | null) => void;
}

const getInitialProjectId = () => {
  if (typeof window === "undefined") {
    return null;
  }
  return localStorage.getItem("active_project");
};

export const useProjectStore = create<ProjectState>((set) => ({
  currentProjectId: getInitialProjectId(),
  setCurrentProjectId: (id) => {
    if (typeof window !== "undefined") {
      if (id) {
        localStorage.setItem("active_project", id);
      } else {
        localStorage.removeItem("active_project");
      }
    }
    set({ currentProjectId: id });
  },
}));
