import { getToken, getMasterDashboardUrl } from "hse-auth";

/**
 * Open the Master Dashboard SPA (port 5174) with shared JWT SSO.
 * @param {string} path e.g. `/` or `/personalized-dashboard`
 */
export function openMasterDashboard(path = "/") {
  const base = String(getMasterDashboardUrl()).replace(/\/+$/, "");
  const pathNorm = path.startsWith("/") ? path : `/${path}`;
  let url = `${base}${pathNorm}`;
  const token = getToken();
  if (token) {
    const sep = url.includes("?") ? "&" : "?";
    url += `${sep}token=${encodeURIComponent(token)}`;
  }
  window.location.assign(url);
}

export { getMasterDashboardUrl };
