"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ui = require("@contember/ui");
const react = require("react");
const MiscPageLayout = require("../MiscPageLayout.cjs");
const InvalidIdentityFallback = require("./InvalidIdentityFallback.cjs");
const reactIdentity = require("@contember/react-identity");
const ClearIdentityHandler = ({ onInvalidIdentity }) => {
  react.useEffect(() => {
    if (onInvalidIdentity) {
      onInvalidIdentity();
    } else if (window.location.pathname !== "/") {
      window.location.href = "/";
    }
  }, [onInvalidIdentity]);
  return null;
};
const IdentityProvider = ({ children, onInvalidIdentity, allowUnauthenticated }) => {
  return /* @__PURE__ */ jsxRuntime.jsxs(reactIdentity.IdentityProvider, { children: [
    /* @__PURE__ */ jsxRuntime.jsxs(reactIdentity.IdentityState, { state: "cleared", children: [
      /* @__PURE__ */ jsxRuntime.jsx(ClearIdentityHandler, { onInvalidIdentity }),
      /* @__PURE__ */ jsxRuntime.jsx(MiscPageLayout.MiscPageLayout, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Message, { size: "large", padding: "large", display: "block", children: "Logging out…" }) })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(reactIdentity.IdentityState, { state: "failed", children: /* @__PURE__ */ jsxRuntime.jsx(InvalidIdentityFallback.InvalidIdentityFallback, {}) }),
    /* @__PURE__ */ jsxRuntime.jsx(reactIdentity.IdentityState, { state: allowUnauthenticated ? "loading" : ["loading", "none"], children: /* @__PURE__ */ jsxRuntime.jsx(ui.SpinnerOverlay, {}) }),
    /* @__PURE__ */ jsxRuntime.jsx(reactIdentity.IdentityState, { state: allowUnauthenticated ? ["success", "none"] : "success", children })
  ] });
};
exports.IdentityProvider = IdentityProvider;
//# sourceMappingURL=IdentityProvider.cjs.map
