import { create } from 'zustand'
import { tauriStore } from '@/utils/persist'
import { createJSONStorage, persist } from 'zustand/middleware'

interface AppState {
  version: string
  hasUpdate: boolean
  inited: boolean
  notice: string
  useMirror: boolean
}

interface AppAction {
  setVersion: (version: string) => void
  setHasUpdate: (hasUpdate: boolean) => void
  setInited: (hasInit: boolean) => void
  setNotice: (notice: string) => void
  setUseMirror: (useMirror: boolean) => void
}

const STORE_NAME = "app"
const { /* store,  */ storage } = tauriStore(STORE_NAME)

const useAppStore = create<AppState & AppAction>()(
  persist(
    (set/* , get */) => ({
      version: "0.0.1",
      hasUpdate: false,
      inited: false,
      notice: "Man! What can I say?",
      useMirror: true,
      setVersion: (version: string) => set(() => ({ version: version })),
      setHasUpdate: (hasUpdate: boolean) => set(() => ({ hasUpdate: hasUpdate })),
      setInited: (inited: boolean) => set(() => ({ inited: inited })),
      setNotice: (notice: string) => set(() => ({ notice: notice })),
      setUseMirror: (useMirror: boolean) => set(() => ({ useMirror: useMirror }))
    }),
    {
      name: STORE_NAME, // name of item in the storage (must be unique)
      storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
    },
  )
)

export default useAppStore