import { defineStore } from 'pinia'

interface Route {
  label: string
  path: string
  query?: string
}
interface BreadCrumb {
  routeList: Route[]
}
// 需要添加多级菜单的页面，值为route的name
const whiteList = ['Booking Detail', 'Tracking Detail', 'Add VGM', 'Public Tracking Detail']

export const useBreadCrumb = defineStore('breadCrumb', {
  state: (): BreadCrumb => ({
    routeList: JSON.parse(localStorage.getItem('routeList')) || []
  }),
  getters: {},
  actions: {
    setRouteList(toRoute: any) {
      const index = this.routeList.findIndex((item) => item.label === toRoute.name)
      if (index !== -1) {
        this.routeList.splice(index + 1)
      } else if (toRoute.name === 'Public Tracking Detail') {
        this.routeList = [
          {
            label: 'Public Tracking',
            path: '/public-tracking',
            query: ''
          },
          {
            label: 'Public Tracking Detail',
            path: '/public-tracking/detail',
            query: toRoute.query
          }
        ]
      } else if (toRoute.name && whiteList.includes(toRoute.name)) {
        this.routeList.push({
          label: toRoute.name,
          path: toRoute.path,
          query: toRoute.query
        })
      } else {
        this.routeList = [
          {
            label: toRoute.name,
            path: toRoute.path,
            query: toRoute.query
          }
        ]
      }
      localStorage.setItem('routeList', JSON.stringify(this.routeList))
    },
    getUpperRoute() {
      return this.routeList[this.routeList.length - 2]
    }
  }
})
