import { createPinia } from 'pinia'
import { LazyStore } from '@tauri-apps/plugin-store'

const lazyStore = new LazyStore('panda.json')
const pinia = createPinia()
pinia.use(({ store }) => {
  const storeKey = `panda-${store.$id}`
  store.$subscribe((_, state) => {
    lazyStore.set(storeKey, JSON.stringify(state)).then(() => {
      lazyStore.save()
    })
  })
  lazyStore.get(storeKey).then((result: string) => {
    if (result) store.$patch(JSON.parse(result))
  })
})

export default pinia
