import menuConfig from './menuConfig.js'
// 兼容飞冰二次开发的js
// 以下部分是由 Iceworks 相关工具自动生成的路由
// 请勿随意改变注释内容（<!-- auto generated routes start --> and <!-- auto generated routes end -->），飞冰以这2段注释为起始和结束位置重写内容
// <!-- auto generated routes start -->
let autoGeneratedRoutes = [];
// <!-- auto generated routes end -->

// 将 Iceworks 当前支持的标准 routes 配置转成支持 vue-router 的格式
function vueRoutesAdapter(routeConfig) {
  const routes = []
  routeConfig.forEach((route) => {
    let currentPath = route.path
    // vue router 需要 path 第一个为 /
    if (currentPath[0] !== '/') {
      currentPath = `/${currentPath}`
    }
    const tempRoute = {
      path: currentPath,
      component: route.component,
      children: [],
      meta: route.meta,
      name: route.name
    }
    if (route.children && route.children.length) {
      tempRoute.children = vueRoutesAdapter(route.children)
    }
    if (route.indexRoute && tempRoute.component) {
      menuConfig.forEach((menu) => {
        if (menu.path === '/' + route.indexRoute.component.name.toLowerCase()) {
          tempRoute.meta = {
            title: menu.title
          }
          tempRoute.component = route.indexRoute.component
          tempRoute.name = menu.name
        }
      })
    }
    routes.push(tempRoute)
  })

  return routes
}

autoGeneratedRoutes = vueRoutesAdapter(autoGeneratedRoutes)
export default autoGeneratedRoutes
