import { createRouter, createWebHashHistory } from 'vue-router';
import { IParam } from '@core';

const routes = [
  {
    path: "/apps/:app?",
    beforeEnter: async (to: any, from: any) => {
      const appParams:IParam = {};
      const auth: Promise<any> = await App.initApp(appParams);
      return auth;
    },
    meta: {  
      tag: '{{app.defaultPSAppIndexView.codeName}}'
    },
    component: () => import("@views/{{spinalCase app.defaultPSAppIndexView.psAppModule.codeName}}/{{spinalCase app.defaultPSAppIndexView.codeName}}"),
    children: [
      {{#each app.appEntityResources as |appEntityResource|}}
        {{#if appEntityResource.appDataEntity.allPSAppViews}}
            {{#each appEntityResource.appDataEntity.allPSAppViews as |appView|}}
            {{#if (eq appView.getRefFlag true)}}
            {{#if (or 
              (eq appView.viewType 'DEEDITVIEW') 
              (eq appView.viewType 'DEGRIDVIEW') 
              (eq appView.viewType 'DETREEEXPVIEW') 
              (eq appView.viewType 'DETREEVIEW') 
              (eq appView.viewType 'DETABEXPVIEW')
            ) }}
            {
              path: "{{appEntityResource.path}}/views/{{lowerCase appView.codeName}}",
              meta: {
                  tag:'{{appView.codeName}}'
              },
              component: () => import("@views/{{spinalCase appView.psAppModule.codeName}}/{{spinalCase appView.codeName}}"),
            },
            {{/if}}
            {{/if}}
            {{/each}}
        {{/if}}
      {{/each}}
      {{#each app.allPSAppViews as | appView |}}
      {{!-- 临时只支持首页视图、应用看板视图 --}}
        {{#if (or (eq appView.getViewType 'APPINDEXVIEW') (eq appView.viewType 'APPPORTALVIEW'))}}
            {{#unless (or appView.psAppDataEntity appView.defaultPage)}}
                {
                  path: "views/{{lowerCase appView.codeName}}",
                  meta: {
                      tag:'{{appView.codeName}}'
                  },
                  component: () => import("@views/{{spinalCase appView.psAppModule.codeName}}/{{spinalCase appView.codeName}}"),
                },
            {{/unless}}
        {{/if}}
      {{/each}}
    ]
  },
  {
    path: "/",
    redirect: "/apps/{{lowerCase app.codeName}}"
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@components/common/404.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@components/common/login.vue")
  },
  {
    path: '/lock',
    name: 'lock',
    component: () => import('@components/common/lock.vue'),
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

/**
 * 全局路由守卫
 */
router.beforeEach((to, from, next) => {
  next();
})
export default router;