import {
	NavigationGuardNext,
	RouteLocationNormalized,
	RouteRecordRaw,
} from 'vue-router'
import TabbarLayout from '@layout/index.vue'
import GlobalWrap from '@layout/GlobalWrap.vue'
import { IMSDK } from '@/utils/imCommon'
import conversationRouters from './modules/conversation'
import contactRouters from './modules/contact'
import profileRouters from './modules/profile'
import loginRouters from './modules/login'
import momentsRouters from './modules/moments'
import { getIMToken, getIMUserID } from '@/utils/storage'

const loginCheck = async (
	to : RouteLocationNormalized,
	from : RouteLocationNormalized,
	next : NavigationGuardNext,
) => {
	try {
		await IMSDK.getLoginStatus()
		next()
	} catch (error) {
		const IMToken = getIMToken()
		const IMUserID = getIMUserID()
		if (!IMToken || !IMUserID) {
			next('login')
			return
		}
		if (to.path !== '/conversation') {
			next('conversation')//代码修改暂时注释总之就是非常奇怪
			// next()
			return
		}
		next()
	}
}

const routes : Array<RouteRecordRaw> = [
	{
		path: '/',
		name: 'GlobalWrap',
		component: GlobalWrap,
		children: [
			{
				path: '/',
				name: 'TabbarLayout',
				redirect: 'conversation',
				component: TabbarLayout,
				beforeEnter: loginCheck,
				children: [
					{
						path: '/conversation',
						name: 'Conversation',
						component: () => import('@pages/conversation/index/index.vue'),
					},
					{
						path: '/contact',
						name: 'Contact',
						component: () => import('@pages/contact/index/index.vue'),
					},
					{
						path: '/moments',
						name: 'Moments',
						component: () => import('@pages/moments/index/index.vue'),
					},
					{
						path: '/profile',
						name: 'Profile',
						component: () => import('@pages/profile/index/index.vue'),
					},
				],
			},
			{
				path: '/',
				name: 'Common',
				beforeEnter: loginCheck,
				children: [
					...conversationRouters,
					...contactRouters,
					...profileRouters,
					...momentsRouters,
				],
			},
			...loginRouters,
		],
	},
]

export default routes