import store from '@/store'
import { openPage } from '@/core/nav/navigation'

export const LOGIN_ROUTE = '/pages/passport/lobby'

/** 当前是否已登录 */
export function isLoggedIn() {
	return !!(store.state.user && store.state.user.isLogin)
}

/**
 * 登录守卫：未登录时跳转登录页
 * @param {string|{ redirect?: string, message?: string, toast?: boolean }} options
 * @returns {boolean} 已登录 true；未登录 false（并已跳转）
 */
export function requireLogin(options = {}) {
	if (isLoggedIn()) return true

	const opts = typeof options === 'string' ? { message: options } : (options || {})
	const redirect = opts.redirect || LOGIN_ROUTE

	if (opts.toast && opts.message) {
		uni.showToast({ title: opts.message, icon: 'none' })
	}

	openPage(redirect)
	return false
}
