import { create } from 'zustand';
import { listWorkspaces, type MobileWorkspace } from '../api/workspaces';

type WorkspaceState = {
  workspaces: MobileWorkspace[];
  activeWorkspaceId: string | null;
  hydrate: () => Promise<void>;
  setActiveWorkspace: (id: string) => void;
};

export const useWorkspaceStore = create<WorkspaceState>((set) => ({
  workspaces: [],
  activeWorkspaceId: null,
  async hydrate() {
    const workspaces = await listWorkspaces();
    set({
      workspaces,
      activeWorkspaceId: workspaces[0]?.id ?? null,
    });
  },
  setActiveWorkspace(id: string) {
    set({ activeWorkspaceId: id });
  },
}));
