import { create } from 'zustand'

// Counter (not boolean) so an aborted-then-restarted loop on rapid
// filter changes can't flip the flag false while a fresh loop is still
// active. react-query's isFetching doesn't cover this loop because it
// runs after queryFn resolves and writes via setQueryData directly.
interface PhotosBackgroundLoadingState {
  inFlight: number
  start: () => void
  stop: () => void
}

export const usePhotosBackgroundLoadingStore = create<PhotosBackgroundLoadingState>((set) => ({
  inFlight: 0,
  start: () => set((s) => ({ inFlight: s.inFlight + 1 })),
  stop: () => set((s) => ({ inFlight: Math.max(0, s.inFlight - 1) })),
}))
