import { ref } from 'vue';
import { router } from '@/router';

export const useAsRouter = () => {
	// 获取当前路由
	function routerCur() {
		return router.currentRoute.value.path
	}
	
	// 获取当前路由父路由
	function routerArrayCur() {
		return router.currentRoute.value.matched[1].path;
	}
	
	// 获取当前父路由和子路由
	function routerArrayParentChildCur() {
		return router.currentRoute.value.matched[1];
	}
	
	// 跳转路由
	function routerTo(url : string) {
		router.push(url)
	}

	// 匹配路由页面
	function routerDynamic(userRouter : string[], originRouter : Object[]) {
		const finalRouter = originRouter.filter(item => userRouter.includes(item.name)).map(item => ({ ...item, children: item.children ? routerDynamic(userRouter, item.children) : null }));
		return finalRouter;
	}

	// 添加路由
	function routerAdd(route) {
		router.addRoute('', route);
	}

	// 初始化路由
	function routerInit() {
		return router.options.routes + router.options.routes.LayoutRoute;
	}

	return {
		routerCur,
		routerArrayCur,
		routerArrayParentChildCur,
		routerTo,
		routerDynamic,
		routerAdd,
		routerInit
	}
}