import { useContext, useCallback } from "react";
import { IdentityContext } from "./IdentityProvider.js";
import { useSetSessionToken } from "@contember/react-client";
import { useSignOut } from "../../tenant/mutations/identity/signOut.js";
const useLogout = () => {
  const ctx = useContext(IdentityContext);
  const [tenantLogout] = useSignOut();
  const clearIdentity = ctx?.clearIdentity;
  const setSessionToken = useSetSessionToken();
  return useCallback(async ({ noRedirect = false } = {}) => {
    if (navigator.credentials && navigator.credentials.preventSilentAccess) {
      try {
        await navigator.credentials.preventSilentAccess();
      } catch {
      }
    }
    clearIdentity?.();
    setSessionToken(void 0);
    try {
      const response = await tenantLogout({});
      if (!response.signOut.ok) {
        console.warn(response.signOut.error);
      }
    } catch (e) {
      console.warn(e);
    }
    if (!noRedirect) {
      window.location.href = "/";
    }
  }, [clearIdentity, setSessionToken, tenantLogout]);
};
export { useLogout };
//# sourceMappingURL=useLogout.js.map
