// common/embedder-helpers.ts
function parentCorsMessage(res, flag = "") {
  const embedder = res.state?.embedder || {};
  const messageListener = (event) => {
    if (event.origin !== embedder.origin) return;
    if (event.data == "getActiveMassSession") {
      window.removeEventListener("message", messageListener);
      child?.postMessage({ state: res.state }, embedder.origin);
      if (embedder.origin != window.location.origin) {
        setTimeout(() => {
          try {
            if (window.history.length > 1) window.history.back();
            else window.close();
          } catch (e) {
            console.log(e);
            window.open(window.location.href, "_self")?.close();
          }
        }, 500);
      }
    }
  };
  window.addEventListener("message", messageListener, false);
  const confirmedCorsWindow = JSON.parse(localStorage.getItem("confirmedCorsWindow") || `[]`);
  if (embedder.origin && embedder.origin != window.location.origin && !confirmedCorsWindow.includes(embedder.origin)) {
    const ok = confirm(
      `Another window will open to recover the saved session. When the next window opens,
- You may need to allow popups.
- You may have to refresh it.
- After the session is recovered, this browser window should automatically close.`
    );
    if (ok) {
      if (embedder.origin && !confirmedCorsWindow.includes(embedder.origin)) confirmedCorsWindow.push(embedder.origin);
      localStorage.setItem("confirmedCorsWindow", JSON.stringify(confirmedCorsWindow.filter((d) => d != void 0)));
    }
  }
  if (flag != "noredirect") {
    document.body.innerHTML = `
			<div style='margin: 20px; padding: 20px; font-size: 24px'>
				The recovered session should be visible in another browser tab.
				You may go back in your browser history or close this browser tab.
			</div>
		`;
  }
  setTimeout(() => window.removeEventListener("message", messageListener), 8e3);
  const child = window.open(embedder.href, "_blank");
}
function childCorsMessage(opts) {
  if (!window.opener) return;
  const hostURL = sessionStorage.getItem("hostURL");
  if (hostURL === window.location.origin) return;
  opts.embeddedSessionState = JSON.parse(sessionStorage.getItem("embeddedSessionState") || `{}`);
  const messageListener = (event) => {
    if (event.origin != window.location.origin && event.origin !== hostURL) return;
    if (event.data.state) {
      window.removeEventListener("message", messageListener);
      Object.assign(opts.embeddedSessionState, event.data.state);
      sessionStorage.setItem("embeddedSessionState", JSON.stringify(opts.embeddedSessionState));
    }
  };
  window.addEventListener("message", messageListener, false);
  setTimeout(() => window.removeEventListener("message", messageListener), 1e3);
  let origin;
  try {
    if (window.opener?.origin) {
      origin = window.opener.origin;
    } else {
      origin = hostURL;
    }
  } catch (e) {
    console.log(e);
    origin = hostURL;
  }
  try {
    window.opener.postMessage("getActiveMassSession", origin);
  } catch (e) {
    console.log(e);
  }
}

export {
  parentCorsMessage,
  childCorsMessage
};
//# sourceMappingURL=chunk-PRZWSBMA.js.map
