import { defineStore } from 'pinia'

export const useTabbarStore = defineStore('tabbar', () => {
  const index = ref(0)

  const tabbarList = [
    {
      name: '首页',
      path: '/'
    },
    {
      name: '境外旅游',
      path: '/travel'
    },
    {
      name: '出国劳务',
      path: '/labour'
    },
    {
      name: '写游记',
      path: '/profile/notes?tab=published'
    }
  ]

  watch(index, async (val) => {
    tabbarList.forEach(async (item, i) => {
      if (i === val) {
        await navigateTo({
          path: item.path
        })
        return
      }
    })
  })

  const route = useRoute()
  watch(
    () => route.path,
    (val) => {
      const targetIndex = tabbarList.findIndex((item) => item.path === val)
      index.value = targetIndex
    },
    {
      immediate: true
    }
  )

  return {
    tabbarList,
    index
  }
})
