/*
 * @Author: Haijun Zhang
 * @Date: 2022-11-07 14:14:46
 * @LastEditTime: 2022-11-07 15:09:21
 * @LastEditors: Haijun Zhang
 * @Description:
 * @FilePath: \main-web\src\hooks\useRouteItem.ts
 */
import { unref } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { pathToRegexp } from 'path-to-regexp'

export default function () {
  const store = useStore()
  const route = useRoute()
  const routeMap = computed(() => store.state.permission.routeMap)
  const routeItem = computed(() => {
    // 找到直接返回，找不到正则匹配
    const item = unref(routeMap)[route.path]
    if (item) return item
    for (const path in unref(routeMap)) {
      const reg = pathToRegexp(path)
      if (reg.exec(route.path)) {
        return unref(routeMap)[path]
      }
    }
  })
  return {
    routeItem
  }
}
