import { useUserStore } from '@/store/useUserStore'
import { useDepositStore } from '@/store/useDepositStore'
import { useAppStore } from '@/store/useAppStore'

// 路由权限拦截
export const setupRouterInterceptor = () => {
	const list = [
		'navigateTo',
		'redirectTo',
		'reLaunch',
		'switchTab'
	]
	
	const whiteList = [
		'/pages/home/Home',
		'/pages/index/index',
		'/pages/Login/Login',
		'/pages/Login/LoginV2',
		'/pages/Login/Register',
		'/pages/Login/ForgetPassword'
	]
	
	const depositList = [
		'/pages/wallet/Deposit'
	]
	
	list.forEach(item => {
		uni.addInterceptor(item, {
			invoke(e) {
				// const userStore = useUserStore()
				// if (!userStore.isLogin && !whiteList.includes(e.url)) {
				// 	uni.navigateTo({
				// 		url: '/pages/Login/LoginV2'
				// 	})
				// 	return false
				// }
				const appStore = useAppStore()
				const depositStore = useDepositStore()
				if (appStore.phoneNumberPrefix === '+91') {
					if (!depositStore.chargeInfo.rechargeOpen && depositList.includes(e.url)) {
						depositStore.showDepositClosePopup = true
						return false
					}
				}
				console.log('jump to the page:', e.url)
				return true
			}
		})
	})
}