const STORAGE_LOGIN_FLAG = "isLoggedIn";
const STORAGE_LOGIN_BOOT_ID = "loginBootId";

const getBootId = () => {
	// 由 vite.config.js 的 define 注入：重启 dev server 会变化
	try {
		// eslint-disable-next-line no-undef
		return String(__APP_BOOT_ID__ || "");
	} catch (_) {
		return "";
	}
};

export const getIsLoggedIn = () => {
	try {
		const flag = window.localStorage.getItem(STORAGE_LOGIN_FLAG) === "1";
		const savedBootId = window.localStorage.getItem(STORAGE_LOGIN_BOOT_ID) || "";
		return flag && savedBootId && savedBootId === getBootId();
	} catch (_) {
		return false;
	}
};

export const setIsLoggedIn = (val) => {
	try {
		if (val) {
			window.localStorage.setItem(STORAGE_LOGIN_FLAG, "1");
			window.localStorage.setItem(STORAGE_LOGIN_BOOT_ID, getBootId());
		} else {
			window.localStorage.removeItem(STORAGE_LOGIN_FLAG);
			window.localStorage.removeItem(STORAGE_LOGIN_BOOT_ID);
		}
	} catch (_) {
		// ignore
	}
};


