"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const react = require("react");
const reactIdentity = require("@contember/react-identity");
const getBacklinkFromUrl = () => {
  return new URLSearchParams(window.location.search).get("backlink");
};
const useSaveBacklink = () => {
  return react.useCallback(() => {
    const backlink = getBacklinkFromUrl();
    if (!backlink) {
      return;
    }
    sessionStorage.setItem("backlink", backlink);
  }, []);
};
const useRedirectToBacklinkCallback = () => {
  return react.useCallback(() => {
    const backlink = getBacklinkFromUrl() ?? sessionStorage.getItem("backlink");
    if (!backlink) {
      return;
    }
    sessionStorage.removeItem("backlink");
    const resolvedBacklink = new URL(backlink, window.location.href);
    if (resolvedBacklink.origin === window.location.origin) {
      window.location.href = resolvedBacklink.toString();
    }
  }, []);
};
const useRedirectToBacklink = () => {
  const identity = reactIdentity.useIdentity();
  const redirect = useRedirectToBacklinkCallback();
  react.useEffect(() => {
    if (!identity) {
      return;
    }
    redirect();
  }, [identity, redirect]);
};
exports.useRedirectToBacklink = useRedirectToBacklink;
exports.useRedirectToBacklinkCallback = useRedirectToBacklinkCallback;
exports.useSaveBacklink = useSaveBacklink;
//# sourceMappingURL=useRedirectToBacklink.cjs.map
