import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import config from '$config/config';

let routes: Array<RouteRecordRaw> = [
  {
    path:'/',
    redirect:'home',
  }
];

// 收容所有的page页面
const pagesContText = require.context('../pages',true,/page.ts$/);


let useRoutesName = [];

pagesContText.keys().map((item:string)=>{

  // 获取模块
  let modal = pagesContText(item);
  // 鉴定模块
  if (modal.default) {
    // 设置router模块
    let routerPage:RouterPageConfig = modal.default;

    // 判断是否存在name
    if (routerPage.name === undefined) {
      routerPage.name = getPathName(item);
    }

    // 判断是否存在 path
    if (routerPage.path === undefined) {
      routerPage.path = '/' + routerPage.name;
    }


    // 判断是否指定了 nav
    if (routerPage.meta !== undefined && routerPage.meta.nav !== undefined) {
      let nav:RouterPageMetaNavConfig = routerPage.meta.nav;
      if (nav.name === undefined) {
        nav.name = routerPage.name;
      }
    }

    useRoutesName.push(routerPage.name);

    routes.push(routerPage as RouteRecordRaw);

  }
});



// 获取name值
function getPathName(path:string){

  let paths:string[] = path.split('/');

  return paths[paths.length - 2];

}

config.useRoutesName = useRoutesName;

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

export default router
