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/{{#if app.defaultPSAppIndexView.psAppModule.codeName}}{{spinalCase app.defaultPSAppIndexView.psAppModule.codeName}}{{else}}default{{/if}}/{{spinalCase app.defaultPSAppIndexView.codeName}}"),
    children: [
      {{#each app.appEntityResources as |appEntityResource|}}
        {{#if appEntityResource.appDataEntity.allPSAppViews}}
          {{#each appEntityResource.appDataEntity.allPSAppViews as |appView|}}
          {{#if (eq appView.refFlag true)}}
          {{#if (or 
            (eq appView.viewType 'DEEDITVIEW')
            (eq appView.viewType 'DEGRIDVIEW')
            (eq appView.viewType 'DEGRIDEXPVIEW')
            (eq appView.viewType 'DELISTVIEW')
            (eq appView.viewType 'DELISTEXPVIEW')
            (eq appView.viewType 'DEDATAVIEW')
            (eq appView.viewType 'DEPANELVIEW')
            (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/{{#if appView.psAppModule.codeName}}{{spinalCase appView.psAppModule.codeName}}{{else}}default{{/if}}/{{spinalCase appView.codeName}}"),
      },
          {{/if}}
          {{/if}}
          {{/each}}
        {{/if}}
      {{/each}}
      {{#each app.allPSAppViews as | appView |}}
      {{!-- 临时只支持首页视图、应用看板视图 --}}
        {{#if (or (eq appView.viewType 'APPINDEXVIEW') (eq appView.viewType 'APPPORTALVIEW'))}}
            {{#unless (or appView.psAppDataEntity appView.defaultPage)}}
      {
        path: "views/{{lowerCase appView.codeName}}",
        meta: {
            tag:'{{appView.codeName}}'
        },
        component: () => import("@views/{{#if appView.psAppModule.codeName}}{{spinalCase appView.psAppModule.codeName}}{{else}}default{{/if}}/{{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;