<template>
  <!-- 左侧菜单栏 -->
  <div :class="{ 'has-logo': showLogo }" :style="{ backgroundColor: sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground }">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar :class="sideTheme" wrap-class="scrollbar-wrapper">
      <el-menu
          :default-active="activeMenu"
          :collapse="isCollapse"
          :background-color="sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground"
          :text-color="sideTheme === 'theme-dark' ? variables.menuColor : variables.menuLightColor"
          :unique-opened="true"
          :active-text-color="theme"
          :collapse-transition="false"
          mode="vertical"
      >
        <sidebar-item
            v-for="(route, index) in showRoutes"
            :key="route.path + index"
            :item="route"
            :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/assets/styles/variables.module.scss'
import useAppStore from '@/store/modules/app'
import useSettingsStore from '@/store/modules/settings'
import usePermissionStore from '@/store/modules/permission'

const route = useRoute()
const appStore = useAppStore()
const settingsStore = useSettingsStore()
const permissionStore = usePermissionStore()

const showLogo = computed(() => settingsStore.sidebarLogo)
const sideTheme = computed(() => settingsStore.sideTheme)
const theme = computed(() => settingsStore.theme)
const isCollapse = computed(() => !appStore.sidebar.opened)

const activeMenu = computed(() => {
  const { meta, path } = route
  if (meta.activeMenu) {
    return meta.activeMenu
  }
  return path
})

const legacySubSystemPrefixes = ['/pipeNetwork']

function isSubSystemPath(path) {
  return path.startsWith('/subSystem/') || legacySubSystemPrefixes.some(prefix => path === prefix || path.startsWith(`${prefix}/`))
}

function getCurrentSystemPrefix(path) {
  if (path.startsWith('/subSystem/')) {
    return path.split('/').slice(0, 3).join('/')
  }
  return legacySubSystemPrefixes.find(prefix => path === prefix || path.startsWith(`${prefix}/`)) || ''
}

const showRoutes = computed(() => {
  const currentPath = route.path
  const allRoutes = permissionStore.sidebarRouters

  if (currentPath.startsWith('/portal')) {
    return []
  }

  const isAdminPage =
    currentPath.startsWith('/index') ||
    currentPath.startsWith('/system') ||
    currentPath.startsWith('/log') ||
    currentPath.startsWith('/monitor') ||
    !isSubSystemPath(currentPath)

  if (isAdminPage) {
    return allRoutes.filter(item => !isSubSystemPath(item.path))
  }

  if (isSubSystemPath(currentPath)) {
    const currentSystemPrefix = getCurrentSystemPrefix(currentPath)
    return allRoutes.filter(item => currentSystemPrefix && item.path.startsWith(currentSystemPrefix))
  }

  return []
})

</script>
