import { create } from "zustand";

import type { AccessContext } from "@/lib/permissions";

type AccessState = {
  context: AccessContext | null;
  permissions: string[];
  loading: boolean;
  setLoading: (loading: boolean) => void;
  setContext: (context: AccessContext) => void;
  reset: () => void;
};

export const useAccessStore = create<AccessState>((set) => ({
  context: null,
  permissions: [],
  loading: true,
  setLoading: (loading) => set({ loading }),
  setContext: (context) =>
    set({
      context,
      permissions: Array.isArray(context.permissions)
        ? context.permissions
        : [],
      loading: false,
    }),
  reset: () => set({ context: null, permissions: [], loading: false }),
}));
