import { defineStore } from 'pinia'
import { ref } from 'vue'
import Evt from '@/common/utils/evt.ts'

export const useAppSettingStore = defineStore('AppSetting', () => {
  const theme = ref<string>('light')
  const language = ref<'zh' | 'en'>('zh')
  const logo = ref<string | null>(null)
  const collectedMenus = ref<AppTypes.Menu[]>([])
  const defaultAddress = reactive({
    lat: 31.96873,
    lng: 118.798171,
    province: '320000',
    city: '320100',
    provinceName: '江苏省',
    cityName: '南京市',
  })

  const menus = ref<AppTypes.Menu[]>([])
  const menuTree = ref<AppTypes.Menu[]>([])
  const stationId = ref<string>('')

  function $reset() {
    collectedMenus.value = []
    menus.value = []
    menuTree.value = []
    stationId.value = ''
  }

  Evt.on('logout', $reset)

  return {
    collectedMenus,
    menus,
    menuTree,
    theme,
    logo,
    language,
    stationId,
    defaultAddress,
    $reset,
  }
}, {
  persist: {
    pick: [
      'theme',
      'collectedMenus',
      'menus',
      'menuTree',
      'language',
      'logo',
    ],
  },
})
