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


interface AppState {
  version: string
  hasUpdate: boolean
  hasInit: boolean
  notice: string
  useMirror: boolean
  setVersion: (version: string) => void
  init: () => void
}

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

const useAppStore = create<AppState>()(
  persist(
    (set, get) => ({
      version: "0.0.1",
      hasUpdate: false,
      hasInit: false,
      notice: "Man! What can I say?",
      useMirror: true,
      setVersion: (version: string) => set(() => ({ version: version })),
      init: () => {
        console.log('init')
        console.log(get().version)
        // const version = await store.get("version")
        // set(() => ({ version: version }))
      },
      // increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
    }),
    {
      name: 'app', // name of item in the storage (must be unique)
      storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
    },
  ))

export default useAppStore