import { useCallback, useEffect } from "react";
import { useIdentity } from "@contember/react-identity";
const getBacklinkFromUrl = () => {
  return new URLSearchParams(window.location.search).get("backlink");
};
const useSaveBacklink = () => {
  return useCallback(() => {
    const backlink = getBacklinkFromUrl();
    if (!backlink) {
      return;
    }
    sessionStorage.setItem("backlink", backlink);
  }, []);
};
const useRedirectToBacklinkCallback = () => {
  return 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 = useIdentity();
  const redirect = useRedirectToBacklinkCallback();
  useEffect(() => {
    if (!identity) {
      return;
    }
    redirect();
  }, [identity, redirect]);
};
export {
  useRedirectToBacklink,
  useRedirectToBacklinkCallback,
  useSaveBacklink
};
//# sourceMappingURL=useRedirectToBacklink.js.map
