import { store } from 'tauri-plugin-valtio';
import { DEFAULT_STORE_CONFIG } from '.';

// Usage:
// import {appStore} from "@/store/app"
// import { useSnapshot } from "valtio"
// const app = useSnapshot(appStore.state)
// { app.version }
// () => appStore.setVersion("0.0.1")

const defaultValue = {
  version: "0.0.1",
  hasUpdate: false,
  inited: false,
  notice: "",
  useMirror: true
}

export const appStore = store('app', { ...defaultValue }, DEFAULT_STORE_CONFIG);

export const setVersion = (version: string) => { appStore.state.version = version }
export const setHasUpdate = (hasUpdate: boolean) => { appStore.state.hasUpdate = hasUpdate }
export const setInited = (inited: boolean) => { appStore.state.inited = inited }
export const setNotice = (notice: string) => { appStore.state.notice = notice }
export const setUseMirror = (useMirror: boolean) => { appStore.state.useMirror = useMirror }

export const resetAppStore = () => {
  setVersion(defaultValue.version)
  setHasUpdate(defaultValue.hasUpdate)
  setInited(defaultValue.inited)
  setNotice(defaultValue.notice)
  setUseMirror(defaultValue.useMirror)
}