"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const react = require("react");
const IdentityProvider = require("./IdentityProvider.cjs");
const reactClient = require("@contember/react-client");
const signOut = require("../../tenant/mutations/identity/signOut.cjs");
const useLogout = () => {
  const ctx = react.useContext(IdentityProvider.IdentityContext);
  const [tenantLogout] = signOut.useSignOut();
  const clearIdentity = ctx?.clearIdentity;
  const setSessionToken = reactClient.useSetSessionToken();
  return react.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]
  );
};
exports.useLogout = useLogout;
//# sourceMappingURL=useLogout.cjs.map
