/*
 * @Author: wjc
 * @Date: 2025-04-09 10:19:11
 * @LastEditors: wjc
 * @LastEditTime: 2026-01-16 14:50:03
 * @Description: 全局状态管理
 */
import type { App } from 'vue'

import * as Pinia from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate'

const store = Pinia.createPinia()
store.use(createStorage())

/**
 * 使用 uniapp 的持久化 api
 * @returns PiniaPluginPersistedstate
 */
function createStorage() {
  return createPersistedState({
    storage: {
      getItem(key: string) {
        return uni.getStorageSync(key)
      },
      setItem(key: string, value: any) {
        uni.setStorageSync(key, value)
      },
    },
  })
}

export function setupStores(app: App<Element>) {
  app.use(store)
}

export { store }
