import { jsxDEV } from "react/jsx-dev-runtime";
import { createContext, useState, useCallback, useEffect, useMemo } from "react";
import { useSessionToken } from "@contember/react-client";
import { MiscPageLayout } from "../MiscPageLayout.js";
import { Message, SpinnerContainer } from "@contember/ui";
import { InvalidIdentityFallback } from "./InvalidIdentityFallback.js";
import { useLogout } from "./useLogout.js";
import { EnvironmentExtensionProvider } from "@contember/binding";
import { identityEnvironmentExtension } from "./IdentityEnvironmentExtension.js";
import { useFetchMe } from "../../tenant/queries/me.js";
const IdentityContext = createContext(void 0);
const IdentityRefreshContext = createContext(() => {
  throw new Error("IdentityRefreshContext is not initialized");
});
const IdentityProvider = ({ children, onInvalidIdentity, allowUnauthenticated }) => {
  const sessionToken = useSessionToken();
  const fetchMe = useFetchMe();
  const [identityState, setIdentityState] = useState(() => ({ state: sessionToken ? "loading" : "none" }));
  const logout = useLogout();
  const clearIdentity = useCallback(() => setIdentityState({ state: "cleared" }), []);
  const refetch = useCallback(async () => {
    setIdentityState({ state: "loading" });
    try {
      const response = await fetchMe();
      const person = response.data.me.person;
      const projects = response.data.me.projects;
      const permissions = response.data.me.permissions;
      setIdentityState({
        state: "success",
        identity: {
          id: response.data.me.id,
          person: person ?? void 0,
          projects: projects.map((it) => ({
            name: it.project.name,
            slug: it.project.slug,
            roles: it.memberships.map((it2) => it2.role)
          })),
          permissions,
          email: person?.email ?? void 0,
          otpEnabled: person?.otpEnabled ?? void 0,
          personId: person?.id ?? void 0
        }
      });
    } catch (e) {
      console.error(e);
      if (typeof e === "object" && e !== null && "status" in e && e.status === 401) {
        logout({ noRedirect: true });
        clearIdentity();
        if (onInvalidIdentity) {
          onInvalidIdentity();
        } else if (window.location.pathname !== "/") {
          window.location.href = "/";
        }
      } else {
        setIdentityState({ state: "failed" });
      }
    }
  }, [clearIdentity, fetchMe, logout, onInvalidIdentity]);
  useEffect(
    () => {
      if (sessionToken === void 0) {
        setIdentityState({ state: "none" });
        if (!allowUnauthenticated) {
          if (onInvalidIdentity) {
            onInvalidIdentity();
          } else if (window.location.pathname !== "/") {
            window.location.href = "/";
          }
        }
        return;
      }
    },
    [sessionToken, allowUnauthenticated, onInvalidIdentity]
  );
  useEffect(
    () => {
      if (sessionToken !== void 0) {
        refetch();
      }
    },
    [sessionToken, refetch]
  );
  const identityContextValue = useMemo(
    () => identityState.state === "success" ? { clearIdentity, identity: identityState.identity } : void 0,
    [identityState, clearIdentity]
  );
  if (identityState.state === "cleared") {
    return /* @__PURE__ */ jsxDEV(MiscPageLayout, { children: /* @__PURE__ */ jsxDEV(Message, { size: "large", flow: "generousBlock", children: "Logging out…" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
      lineNumber: 149,
      columnNumber: 5
    }, globalThis) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
      lineNumber: 148,
      columnNumber: 4
    }, globalThis);
  }
  if (identityState.state === "failed") {
    return /* @__PURE__ */ jsxDEV(InvalidIdentityFallback, {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
      lineNumber: 155,
      columnNumber: 10
    }, globalThis);
  }
  const pending = identityState.state === "loading" || !allowUnauthenticated && identityState.state === "none";
  return /* @__PURE__ */ jsxDEV(SpinnerContainer, { enabled: pending, children: pending ? null : /* @__PURE__ */ jsxDEV(EnvironmentExtensionProvider, { extension: identityEnvironmentExtension, state: identityContextValue?.identity ?? null, children: /* @__PURE__ */ jsxDEV(IdentityContext.Provider, { value: identityContextValue, children: /* @__PURE__ */ jsxDEV(IdentityRefreshContext.Provider, { value: refetch, children }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
    lineNumber: 165,
    columnNumber: 7
  }, globalThis) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
    lineNumber: 164,
    columnNumber: 6
  }, globalThis) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
    lineNumber: 163,
    columnNumber: 5
  }, globalThis) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Identity/IdentityProvider.tsx",
    lineNumber: 161,
    columnNumber: 3
  }, globalThis);
};
export {
  IdentityContext,
  IdentityProvider,
  IdentityRefreshContext
};
//# sourceMappingURL=IdentityProvider.js.map
