import { defineStore } from 'pinia';
import { model } from '../model';
// import { router } from '@/router';
import { RouteRecordRaw } from 'vue-router';
// import { getMenuInit } from '@/api/commom';

// 清洗网络路由数据
const filterAsyncRoutes = (
	routes: readonly RouteRecordRaw[],
	merge: boolean,
	path?: string
) => {
	const res: RouteRecordRaw[] = []
	const modules = import.meta.glob(['@/views/**/*.vue', '@/layout/index.vue'])
	routes.forEach((route) => {
		const r = { ...route }
		if (!r.path) {
			r.path = ''
		}
		if (path != undefined && merge) {
			r.path = `${path === '/' ? '' : path}/${r.path}`
		}
		if (!r.meta) {
			r.meta = {}
			if (!r.meta.title) {
				r.meta.title = ''
			}
			if (!r.meta.hidden) {
				r.meta.hidden = false
			}
			if (!r.meta.icon) {
				r.meta.icon = ''
			}
		}
		if (r.component) {
			for (const path in modules) {
				if (path.indexOf(r.component.toString()) !== -1) {
					r.component = modules[path]
				}
			}
		}

		if (r.children) {
			r.children = filterAsyncRoutes(r.children, merge, r.path)
		}
		// if (!r.meta.hidden) {
		res.push(r)
		// }
	})
	return res
}

// 路由清洗-供应菜单
const filterConstantRoutes = (routes: RouteRecordRaw[], path?: string) => {
	const res: RouteRecordRaw[] = []
	routes.forEach((route) => {
		const r = { ...route }
		if (!r.path) {
			r.path = ''
		}
		if (path != undefined) {
			r.path = `${path === '/' ? '' : path}/${r.path}`
		}
		if (!r.meta) {
			r.meta = {}
			if (!r.meta.title) {
				r.meta.title = ''
			}
			if (!r.meta.hidden) {
				r.meta.hidden = false
			}
			if (!r.meta.icon) {
				r.meta.icon = ''
			}
		}
		if (r.children) {
			r.children = filterConstantRoutes(r.children, r.path)
		}
		// if (!r.meta.hidden) {
		res.push(r)
		// }
	})
	return res
}

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 pinia 的唯一 id
export const useRoutesStore = defineStore('routes', {
	state: () => {
		return {
			menus: [] as RouteRecordRaw[],
			tagList: [
				{
					path: '/dashboard',
					fullPath: '/dashboard',
					meta: { title: '首页', affix: true },
				},
			] as model.TagView[],
			tagNameList: [] as string[],
		}
	},
	actions: {
		async handleInitMenu() {
			const routes: readonly RouteRecordRaw[] = router.options.routes
			const res: any = await getMenuInit()
			let dataList = res.dataList

			let _routeRecordRaw = filterAsyncRoutes(
				dataList as readonly RouteRecordRaw[],
				true
			)

			for (const item of _routeRecordRaw) {
				router.addRoute(item)
			}

			_routeRecordRaw = filterAsyncRoutes(
				dataList as readonly RouteRecordRaw[],
				false
			)

			let newRouters = _routeRecordRaw.concat(routes)

			this.menus = filterConstantRoutes(newRouters)
		},
		// 初始化标签页
		handleInitTagList() {
			this.tagList = [
				{
					path: '/dashboard',
					fullPath: '/dashboard',
					meta: { title: '首页', affix: true },
				},
			]
			this.tagNameList = []
		},
		// 添加标签页
		handleSetTagList(val: model.TagView) {
			if (
				this.tagList.findIndex(
					(el: model.TagView) => val.fullPath === el.fullPath
				) === -1
			) {
				this.tagList.push(val)
				// 添加缓存组件
				this.tagNameList.push(val.path as string)
			} else {
				// router.replace({ path: val.fullPath as string })
			}
		},
		// 添加标签页
		handleAddTagList(val: model.TagView) {
			const index = this.tagList.findIndex(
					(el: model.TagView) => val.fullPath === el.fullPath
				)
			if (
				 index === -1
			) {
				this.tagList.push(val)
				// 添加缓存组件
				this.tagNameList.push(val.path as string)
			} else {
				// 更新组件参数
				this.tagList.splice(index, 1, val)
			}
		},
		// 关闭标签页
		handleCloseTag(val: model.TagView, path: string) {
			let index = this.tagList.findIndex(
				(el: model.TagView) => val.fullPath === el.fullPath
			)
			this.tagList = this.tagList.filter(
				(el: model.TagView) => val.fullPath !== el.fullPath
			)
			if (index !== -1 && index !== 0) {
				if (path !== val.fullPath) return
				router.replace({ path: this.tagList[index - 1].fullPath as string })
			}
			// 移除缓存组件
			let index1 = this.tagNameList.findIndex((el: string) => val.path === el)
			if (index1 !== -1) {
				this.tagNameList.splice(index1, 1)
			}
		},
		closeTag(name: any) {
			this.tagList = this.tagList.filter(
				(el: model.TagView) => name !== el.path
			)
			let index1 = this.tagNameList.findIndex((el: string) => name === el)
			if (index1 !== -1) {
				this.tagNameList.splice(index1, 1)
			}
		},
		// 关闭其他标签页
		handleCloseOtherTag(val: model.TagView) {
			this.tagList = [
				{
					path: '/dashboard',
					fullPath: '/dashboard',
					meta: {
						title: '首页',
						affix: true,
					},
				},
			]
			if (val.fullPath !== '/dashboard') {
				this.tagList.push(val)
				router.replace({ path: this.tagList[1].fullPath as string })
			} else {
				router.replace({ path: this.tagList[0].fullPath as string })
			}
			this.tagNameList = []
			this.tagNameList.push(val.path as string)
		},
		// 关闭全部标签页
		handleCloseAllTag() {
			this.tagList = [
				{
					path: '/dashboard',
					fullPath: '/dashboard',
					meta: { title: '首页', affix: true },
				},
			]
			router.replace({ path: this.tagList[0].fullPath as string })
			this.tagNameList = []
		},
	},
})
