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

export const useAsRouter = () => {
	// 获取当前路由
	function routerCur() {
		return router.currentRoute.value.path
	}

	// 获取当前路由父路由
	function routerArrayCur() {
		if(router.currentRoute.value.matched.length > 3) {
			return [router.currentRoute.value.matched[1].path, router.currentRoute.value.matched[2].path] 
		} else {
			return [router.currentRoute.value.matched[1].path]
		}
	}

	// 跳转路由
	function routerTo(url : string) {
		router.push(url)
	}

	// 匹配路由页面
	function routerDynamic(originRouter, actionsArray) {
		const finalRouter = originRouter.filter(item => actionsArray.some(action => action.action === item.name))
			.map((item) => ({
				...item,
				title: actionsArray.find(action => action.action === item.name)?.name || item.meta.title,
				meta: {
					...item.meta,
					title: actionsArray.find(action => action.action === item.name)?.name || item.meta.title,
				},
				children: item.children ? routerDynamic(item.children, actionsArray) : [],
			}));
		return finalRouter;
	}

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

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

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