import { create } from 'zustand'

export const useGlobalStore = create((set) => ({
  globalLoadingCount: 0,
  sidebarOpen: false,

  showGlobalLoading: () =>
    set((s) => ({ globalLoadingCount: s.globalLoadingCount + 1 })),

  hideGlobalLoading: () =>
    set((s) => ({
      globalLoadingCount: Math.max(0, s.globalLoadingCount - 1),
    })),

  setSidebarOpen: (open) => set({ sidebarOpen: open }),

  resetGlobal: () =>
    set({ globalLoadingCount: 0, sidebarOpen: false }),
}))

export const selectGlobalLoading = (state) => state.globalLoadingCount > 0

export const selectSidebarOpen = (state) => state.sidebarOpen

