import type { ComponentRecordType, GenerateMenuAndRoutesOptions } from '@vben/types';

import { useGlobSetting } from '@jnpf/hooks';

import { generateAccessible } from '@vben/access';
import { preferences } from '@vben/preferences';
import { useAccessStore } from '@vben/stores';

import { BasicLayout, IFrameView } from '#/layouts';
import { APP_PREFIX } from '#/utils/constants';
import { getRealJnpfAppEnCode } from '#/utils/jnpf';

const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');
const NotFoundView = () => import('#/views/_core/fallback/not-found.vue');
const OnlineModelView = () => import('#/views/common/dynamicModel/index.vue');
const OnlineDictView = () => import('#/views/common/dynamicDictionary/index.vue');
const OnlineReportView = () => import('#/views/common/dynamicReport/index.vue');
// 旧版报表
const OnlineDataReportView = () => import('#/views/common/dynamicDataReport/index.vue');
const OnlinePortalView = () => import('#/views/common/dynamicPortal/index.vue');

async function generateAccess(options: GenerateMenuAndRoutesOptions) {
  const pageMap: ComponentRecordType = import.meta.glob('../views/**/*.vue');

  const layoutMap: ComponentRecordType = {
    BasicLayout,
    IFrameView,
    OnlineModelView,
    OnlineDictView,
    OnlineReportView,
    OnlineDataReportView,
    OnlinePortalView,
    NotFoundView,
  };

  const globSetting = useGlobSetting();
  const accessStore = useAccessStore();
  const appEnCode = getRealJnpfAppEnCode();

  return await generateAccessible(preferences.app.accessMode, {
    ...options,
    // 可以指定没有权限跳转403页面
    forbiddenComponent,
    // 如果 route.meta.menuVisibleWithForbidden = true
    layoutMap,
    pageMap,
    globSetting,
    token: accessStore.accessToken || undefined,
    isAddHomeToMenu: !appEnCode,
    appEnCode,
    appPrefix: APP_PREFIX,
  });
}

export { generateAccess };
