import type { RouteMeta, RouteLocationNormalizedLoaded } from 'vue-router'
import { pathResolve } from '@/utils/routerHelper'

export const filterAffixTags = (routes: AppRouteRecordRaw[], parentPath = '') => {
  let tags: RouteLocationNormalizedLoaded[] = []
  routes.forEach((route) => {
    const meta = route.meta as RouteMeta
    const tagPath = pathResolve(parentPath, route.path)
    const currentSource = sessionStorage.getItem('LOGIN_SOURCE')
    if (currentSource === 'zhly' && route.meta.title === '设备地图') {
      route.meta.affix = true
    } else if (currentSource === 'zhzq' && route.meta.title === '设备地图') {
      route.meta.affix = true
    } else if (currentSource === 'spzx' && tagPath === '/video_center/protocol') {
      route.meta.affix = true
    } else if (currentSource === 'qhse' && tagPath === '/qhse/qhse_kanban') {
      route.meta.affix = true
    } else if (currentSource === 'yyhy' && tagPath === '/operation-meeting/fill') {
      route.meta.affix = true
    }
    if (meta?.affix) {
      tags.push({ ...route, path: tagPath, fullPath: tagPath } as RouteLocationNormalizedLoaded)
    }
    if (route.children) {
      const tempTags: RouteLocationNormalizedLoaded[] = filterAffixTags(route.children, tagPath)
      if (tempTags.length >= 1) {
        tags = [...tags, ...tempTags]
      }
    }
  })

  return tags
}
