import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'

export const routes: AppRouteRecordRaw[] = [
  {
    path: '/',
    name: 'index',
    redirect: '/application/index',
    component: () => import('@/viewsnew/layout/index.vue'),
    hidden: true,
    meta: {},
    children: [
      {
        path: 'application',
        name: 'application',
        hidden: true,
        component: () => import('@/viewsnew/application/index.vue'),
        meta: { hidden: true }
      }
    ]
  },
  {
    path: '/copilot',
    name: 'copilot',
    component: () => import('@/layout/index.vue'),
    hidden: true,
    meta: {},
    children: [
      {
        path: 'index',
        name: 'cpt',
        hidden: true,
        component: () => import('@/views/copilot/index.vue'),
        meta: { hidden: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    hidden: true,
    meta: {},
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/admin-login',
    name: 'admin-login',
    hidden: true,
    meta: {},
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/401',
    name: '401',
    hidden: true,
    meta: {},
    component: () => import('@/views/401/index.vue')
  },
  {
    path: '/dvCanvas',
    name: 'dvCanvas',
    hidden: true,
    meta: {},
    component: () => import('@/views/data-visualization/index.vue')
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    hidden: true,
    meta: {},
    component: () => import('@/views/dashboard/index.vue')
  },
  {
    path: '/dashboardPreview',
    name: 'dashboardPreview',
    hidden: true,
    meta: {},
    component: () => import('@/views/dashboard/DashboardPreviewShow.vue')
  },
  {
    path: '/chart',
    name: 'chart',
    hidden: true,
    meta: {},
    component: () => import('@/views/chart/index.vue')
  },
  {
    path: '/previewShow',
    name: 'previewShow',
    hidden: true,
    meta: {},
    component: () => import('@/views/data-visualization/PreviewShow.vue')
  },
  {
    path: '/DeResourceTree',
    name: 'DeResourceTree',
    hidden: true,
    meta: {},
    component: () => import('@/views/common/DeResourceTree.vue')
  },
  {
    path: '/dataset-embedded',
    name: 'dataset-embedded',
    hidden: true,
    meta: {},
    component: () => import('@/views/visualized/data/dataset/index.vue')
  },
  {
    path: '/dataset-embedded-form',
    name: 'dataset-embedded-form',
    hidden: true,
    meta: {},
    component: () => import('@/views/visualized/data/dataset/form/index.vue')
  },
  {
    path: '/preview',
    name: 'preview',
    hidden: true,
    meta: {},
    component: () => import('@/views/data-visualization/PreviewCanvas.vue')
  },
  {
    path: '/de-link/:uuid',
    name: 'link',
    hidden: true,
    meta: {},
    component: () => import('@/views/data-visualization/LinkContainer.vue')
  },
  {
    path: '/rich-text',
    name: 'rich-text',
    hidden: true,
    meta: {},
    component: () => import('@/custom-component/rich-text/DeRichTextView.vue')
  },
  {
    path: '/module',
    name: 'module',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/module/index.vue')
  },
  {
    path: '/modify-pwd',
    name: 'modify-pwd',
    hidden: true,
    meta: {},
    component: () => import('@/layout/index.vue'),
    children: [
      {
        path: 'index',
        name: 'mpi',
        hidden: true,
        component: () => import('@/views/system/modify-pwd/index.vue'),
        meta: { hidden: true }
      }
    ]
  },
  {
    path: '/chart-view',
    name: 'chart-view',
    hidden: true,
    meta: {},
    component: () => import('@/views/chart/ChartView.vue')
  },
  {
    path: '/dvsCanvas',
    name: 'dvsCanvas',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/data-visualization/index.vue')
  },
  {
    path: '/formcreatedesigner',
    name: 'formcreatedesigner',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/common/FormCreateDesigner.vue')
  },
  // {
  //   path: '/application',
  //   name: 'application',
  //   hidden: true,
  //   meta: {},
  //   component: () => import('@/viewsnew/application/index.vue')
  // },
  {
    path: '/datasetnew',
    name: 'datasetnew',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/service/dataset/index.vue')
  },
  {
    path: '/datasetForm',
    name: 'datasetForm',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/service/dataset/form/index.vue')
  },
  {
    path: '/datasourcenew',
    name: 'datasourcenew',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/service/datasource/index.vue')
  },
  {
    path: '/permissionset',
    name: 'permissionset',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/permissionset/index.vue')
  },
  {
    path: '/menuconfig',
    name: 'menuconfig',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/menuconfig/index.vue')
  },
  {
    path: '/PreviewSystem',
    name: 'PreviewSystem',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/PreviewSystem.vue')
  },
  {
    path: '/editNavbar',
    name: 'editNavbar',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/SfcEditor/NavbarEditor/editNavbar.vue')
  },
  {
    path: '/editLogin',
    name: 'editLogin',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/SfcEditor/LoginEditor/editlogin.vue')
  },
  {
    path: '/SystemLogin',
    name: 'SystemLogin',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/application/SystemLogin.vue')
  },
  {
    path: '/TemplateResource',
    name: 'TemplateResource',
    redirect: '/TemplateResource/index',
    component: () => import('@/viewsnew/layout/index.vue'),
    hidden: true,
    meta: {},
    children: [
      {
        path: 'index',
        name: 'TemplateResource',
        hidden: true,
        component: () => import('@/viewsnew/TemplateResource/index.vue'),
        meta: { hidden: true }
      },
    ]
  },{
    path: '/TemplatePreview',
    name: 'TemplatePreview',
    hidden: true,
    meta: {},
    component: () => import('@/viewsnew/TemplateResource/preview/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes as RouteRecordRaw[]
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Login']
  router.getRoutes().forEach(route => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
