import { jsxs, jsx } from "react/jsx-runtime";
import { Message, SpinnerOverlay } from "@contember/ui";
import { useEffect } from "react";
import { MiscPageLayout } from "../MiscPageLayout.js";
import { InvalidIdentityFallback } from "./InvalidIdentityFallback.js";
import { IdentityProvider as IdentityProvider$1, IdentityState } from "@contember/react-identity";
const ClearIdentityHandler = ({ onInvalidIdentity }) => {
  useEffect(() => {
    if (onInvalidIdentity) {
      onInvalidIdentity();
    } else if (window.location.pathname !== "/") {
      window.location.href = "/";
    }
  }, [onInvalidIdentity]);
  return null;
};
const IdentityProvider = ({ children, onInvalidIdentity, allowUnauthenticated }) => {
  return /* @__PURE__ */ jsxs(IdentityProvider$1, { children: [
    /* @__PURE__ */ jsxs(IdentityState, { state: "cleared", children: [
      /* @__PURE__ */ jsx(ClearIdentityHandler, { onInvalidIdentity }),
      /* @__PURE__ */ jsx(MiscPageLayout, { children: /* @__PURE__ */ jsx(Message, { size: "large", padding: "large", display: "block", children: "Logging out…" }) })
    ] }),
    /* @__PURE__ */ jsx(IdentityState, { state: "failed", children: /* @__PURE__ */ jsx(InvalidIdentityFallback, {}) }),
    /* @__PURE__ */ jsx(IdentityState, { state: allowUnauthenticated ? "loading" : ["loading", "none"], children: /* @__PURE__ */ jsx(SpinnerOverlay, {}) }),
    /* @__PURE__ */ jsx(IdentityState, { state: allowUnauthenticated ? ["success", "none"] : "success", children })
  ] });
};
export {
  IdentityProvider
};
//# sourceMappingURL=IdentityProvider.js.map
