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

export const SIDEBAR_COLLAPSED_W = 68
export const SIDEBAR_DEFAULT_W = 240
export const SIDEBAR_MIN_W = 200
export const SIDEBAR_MAX_W = 380

interface SidebarState {
  pinned: boolean
  pinnedWidth: number
  setPinned: (pinned: boolean) => void
  togglePinned: () => void
  setPinnedWidth: (w: number) => void
  resetPinnedWidth: () => void
}

export const useSidebarStore = create<SidebarState>()(
  persist(
    set => ({
      pinned: false,
      pinnedWidth: SIDEBAR_DEFAULT_W,
      setPinned: pinned => set({ pinned }),
      togglePinned: () => set(s => ({ pinned: !s.pinned })),
      setPinnedWidth: w =>
        set({
          pinnedWidth: Math.max(SIDEBAR_MIN_W, Math.min(SIDEBAR_MAX_W, w)),
        }),
      resetPinnedWidth: () => set({ pinnedWidth: SIDEBAR_DEFAULT_W }),
    }),
    { name: 'jf_sidebar' },
  ),
)
