import { createRouter, createWebHistory } from 'vue-router';
import { CommonStore } from '@/stores/common';
import EmptyLayout from '@/layouts/empty.vue';
import DefaultLayout from '@/layouts/default.vue';
import CidLayout from '@/layouts/cid.vue';
import routerData from '@/router/routerData';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: EmptyLayout,
      children: [
        {
          path: '/login',
          name: 'Login',
          // component: () => import('@/views/login/index.vue'),
          component: () => import('@/views/error/developing.vue'),
        },
      ],
    },
    {
      path: '/',
      component: window?. __POWERED_BY_WUJIE__ ? CidLayout : DefaultLayout,
      children: routerData,
    },
    {
      path: '/:pathMatch(.*)*',
      component: EmptyLayout,
      children: [
        {
          path: '/:pathMatch(.*)*',
          name: '404',
          component: () => import('@/views/error/404.vue'),
        },
      ],
    },
  ],
});

router.afterEach((to) => {
  routerData.some((menu) => {
    if (menu.children && menu.children.length > 0) {
      return menu.children.some((subMenu) => {
        if (subMenu.path === to.path) {
          CommonStore().addAlivePage(subMenu);
          return true;
        }
      });
    } else {
      if (menu.path === to.path) {
        CommonStore().addAlivePage(menu);
        return true;
      }
    }
  });
});

export default router;
