import { create } from "zustand";

interface SyncQueueState {
  /** Number of items waiting to be synced */
  queueCount: number;
  /** True while a sync pass is running */
  isSyncing: boolean;
  /** Mirrors navigator.onLine (updated client-side) */
  isOnline: boolean;

  setQueueCount: (n: number) => void;
  setSyncing: (v: boolean) => void;
  setOnline: (v: boolean) => void;
  incrementQueue: () => void;
  decrementQueue: () => void;
}

export const useSyncQueueStore = create<SyncQueueState>((set) => ({
  queueCount: 0,
  isSyncing: false,
  isOnline: true, // assume online until client hydrates

  setQueueCount: (n) => set({ queueCount: Math.max(0, n) }),
  setSyncing: (v) => set({ isSyncing: v }),
  setOnline: (v) => set({ isOnline: v }),
  incrementQueue: () => set((s) => ({ queueCount: s.queueCount + 1 })),
  decrementQueue: () => set((s) => ({ queueCount: Math.max(0, s.queueCount - 1) })),
}));
