/**
 * 与根目录 `goToPage.js` 中 getDecodedParams 及合作限制详情 query 解码行为一致，
 * 供智库/合作限制从 `@/utils/goToPage` 迁出后使用（不修改 `goToPage` 目录与 `goToPage.js`）。
 */
import router from "@/router/index";



/** 新窗口打开合作限制详情：id 为 base64(encodeURIComponent(明文))，与概览列表跳转一致 */
export function goToCooperationRestrictionsDetail(plainId, sessionTabTitle) {
	const s = String(plainId ?? "").trim();
	if (!s) return;
	if (sessionTabTitle != null && sessionTabTitle !== "") {
		window.sessionStorage.setItem("cooperationRestrictionsTabName", String(sessionTabTitle));
	}
	let encoded;
	try {
		encoded = btoa(encodeURIComponent(s));
	} catch (_) {
		return;
	}
	if (!encoded) return;
	const r = router.resolve({
		name: "CooperationRestrictionsDetail",
		query: { id: encoded }
	});
	window.open(r.href, "_blank");
}

/** 合作限制详情页 `route.query.id` 解码（兼容明文数字与 base64 包装） */
export function decodeCooperationRestrictionQueryId(raw) {
	if (raw == null) return null;
	const s0 = String(raw).trim();
	if (!s0) return null;
	if (/^\d+$/.test(s0)) return s0;
	if (/^[A-Za-z0-9+/_-]+={0,2}$/.test(s0)) {
		try {
			const normalized = s0.replace(/-/g, "+").replace(/_/g, "/");
			const padded = normalized + "===".slice((normalized.length + 3) % 4);
			const decoded = atob(padded);
			try {
				return decodeURIComponent(decoded);
			} catch (_) {
				return decoded;
			}
		} catch (_) {}
	}
	return s0;
}
