import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

export interface SpotlightState {
  open: boolean
  query: string
  filterType: string | null
  sel: number
  navigated: boolean
}

export type SpotlightFilter = 'all' | 'krate' | 'namespace' | 'pod' | 'deployment' | 'service'

const INITIAL_STATE: SpotlightState = {
  open: false,
  query: '',
  filterType: null,
  sel: 0,
  navigated: false,
}

export const useSpotlightStore = create<SpotlightState>()(
  devtools((set) => ({
    ...INITIAL_STATE,
    setOpen: (open: boolean) => set({ open }),
    setQuery: (query: string) => set({ query, navigated: false, sel: 0 }),
    setFilter: (filterType: SpotlightFilter | null) => set({ filterType }),
    setSel: (sel: number) => set({ sel, navigated: true }),
    toggle: () => set((state) => ({ open: !state.open })),
    close: () => set({ open: false, query: '', filterType: null, sel: 0, navigated: false }),
  }))
)
