"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactClient = require("@contember/react-client");
const ui = require("@contember/ui");
const react = require("react");
const MiscPageLayout = require("../MiscPageLayout.cjs");
const Pages = require("../pageRouting/Pages.cjs");
const CreateResetPasswordRequestForm = require("../../tenant/components/password/CreateResetPasswordRequestForm.cjs");
const FillResetPasswordTokenForm = require("../../tenant/components/password/FillResetPasswordTokenForm.cjs");
const ResetPasswordForm = require("../../tenant/components/password/ResetPasswordForm.cjs");
const useRedirectToBacklink = require("../../tenant/components/person/useRedirectToBacklink.cjs");
const ProjectList = require("../Project/ProjectList.cjs");
const useIDPAutoInit = require("../../tenant/components/idp/useIDPAutoInit.cjs");
const IDPResponseHandler = require("../../tenant/components/idp/IDPResponseHandler.cjs");
const Login = require("../../tenant/components/person/Login.cjs");
const IDPInitButton = require("../../tenant/components/idp/IDPInitButton.cjs");
const reactRouting = require("@contember/react-routing");
const IdentityProvider = require("../Identity/IdentityProvider.cjs");
const Link = require("../../routing/Link.cjs");
const reactIdentity = require("@contember/react-identity");
const indexPageName = "index";
const resetRequestPageName = "resetRequest";
const redirectOnSuccessPageName = "resetRequestSuccess";
const passwordResetPageName = "passwordReset";
const LoginEntrypoint = (props) => {
  const routing = {
    basePath: props.basePath ?? "/",
    routes: {},
    defaultDimensions: {},
    pageInQuery: true
  };
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
    reactClient.ContemberClient,
    {
      apiBaseUrl: props.apiBaseUrl,
      sessionToken: props.sessionToken,
      loginToken: props.loginToken,
      children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactRouting.RoutingContext.Provider, { value: routing, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactRouting.RequestProvider, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Providers, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(Pages.Pages, { children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactRouting.Page, { name: indexPageName, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(IdentityProvider.IdentityProvider, { allowUnauthenticated: true, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
          LoginEntrypointIndex,
          {
            projects: props.projects,
            formatProjectUrl: props.formatProjectUrl,
            identityProviders: props.identityProviders,
            heading: props.heading,
            projectsPageActions: props.projectsPageActions,
            collapsedEmailLogin: props.collapsedEmailLogin
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 64,
            columnNumber: 10
          },
          void 0
        ) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 63,
          columnNumber: 9
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 62,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactRouting.Page, { name: resetRequestPageName, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(MiscPageLayout.MiscPageLayout, { heading: "Password reset", actions: /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(Link.Link, { to: indexPageName, children: "← Back to login" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 76,
          columnNumber: 10
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 75,
          columnNumber: 59
        }, void 0), children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(CreateResetPasswordRequestForm.CreateResetPasswordRequestForm, { redirectOnSuccess: redirectOnSuccessPageName }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 78,
          columnNumber: 10
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 75,
          columnNumber: 9
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 74,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactRouting.Page, { name: redirectOnSuccessPageName, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(MiscPageLayout.MiscPageLayout, { heading: "Password reset", actions: /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(Link.Link, { to: indexPageName, children: "← Back to login" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 83,
          columnNumber: 10
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 82,
          columnNumber: 59
        }, void 0), children: [
          /* @__PURE__ */ jsxDevRuntime.jsxDEV("p", { children: "Password reset request has been successfully created. Please check your inbox for the instructions." }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 85,
            columnNumber: 10
          }, void 0),
          /* @__PURE__ */ jsxDevRuntime.jsxDEV("p", { children: "Please follow the link in e-mail or copy the reset token here:" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 88,
            columnNumber: 10
          }, void 0),
          /* @__PURE__ */ jsxDevRuntime.jsxDEV(FillResetPasswordTokenForm.FillResetPasswordTokenForm, { resetLink: `${passwordResetPageName}(token: $token)` }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 91,
            columnNumber: 10
          }, void 0)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 82,
          columnNumber: 9
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 81,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactRouting.Page, { name: passwordResetPageName, children: ({ token }) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(MiscPageLayout.MiscPageLayout, { heading: "Set a new password", actions: /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(Link.Link, { to: indexPageName, children: "← Back to login" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 97,
          columnNumber: 11
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 96,
          columnNumber: 64
        }, void 0), children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ResetPasswordForm.ResetPasswordForm, { token, redirectOnSuccess: indexPageName }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 99,
          columnNumber: 11
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 96,
          columnNumber: 10
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 94,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 61,
        columnNumber: 7
      }, void 0) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 60,
        columnNumber: 6
      }, void 0) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 59,
        columnNumber: 5
      }, void 0) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 58,
        columnNumber: 4
      }, void 0)
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 53,
      columnNumber: 3
    },
    void 0
  );
};
const LoginEntrypointIndex = (props) => {
  const logout = reactIdentity.useLogout();
  const identity = reactIdentity.useIdentity();
  const [projectSlugs, setProjectSlugs] = react.useState();
  const projectsProvider = props.projects;
  react.useEffect(() => {
    (async () => {
      if (identity === void 0) {
        return;
      }
      setProjectSlugs(projectsProvider instanceof Function ? await projectsProvider() : projectsProvider);
    })();
  }, [identity, projectsProvider]);
  const projects = react.useMemo(() => {
    return identity?.projects.filter((it) => projectSlugs?.includes(it.slug));
  }, [identity?.projects, projectSlugs]);
  useRedirectToBacklink.useRedirectToBacklink();
  if (identity === void 0) {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(MiscPageLayout.MiscPageLayout, { heading: props.heading ?? "Contember Admin", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(LoginContainer, { identityProviders: props.identityProviders, collapsedEmailLogin: props.collapsedEmailLogin }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 134,
      columnNumber: 5
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 133,
      columnNumber: 4
    }, void 0);
  } else if (projects === void 0) {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      MiscPageLayout.MiscPageLayout,
      {
        heading: "Projects",
        actions: /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
          props.projectsPageActions,
          /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { onClick: () => logout(), size: "small", distinction: "seamless", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Icon, { blueprintIcon: "log-out" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 144,
            columnNumber: 75
          }, void 0) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 144,
            columnNumber: 6
          }, void 0)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 142,
          columnNumber: 14
        }, void 0),
        children: "Loading projects..."
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 140,
        columnNumber: 4
      },
      void 0
    );
  } else if (projects.length === 1) {
    window.location.href = props.formatProjectUrl(projects[0]);
    return null;
  } else {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      MiscPageLayout.MiscPageLayout,
      {
        heading: "Projects",
        actions: props.projectsPageActions,
        footerActions: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { onClick: () => logout(), distinction: "seamless", display: "block", children: [
          "Sign out ",
          /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Icon, { blueprintIcon: "log-out" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 160,
            columnNumber: 101
          }, void 0)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 160,
          columnNumber: 20
        }, void 0),
        children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ProjectList.ProjectListButtons, { projects, formatProjectUrl: props.formatProjectUrl }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 162,
          columnNumber: 5
        }, void 0)
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 157,
        columnNumber: 4
      },
      void 0
    );
  }
};
const LoginContainer = ({ identityProviders = [], collapsedEmailLogin: initialCollapsedEmailLogin }) => {
  const [collapsedEmailLogin, setCollapsedEmailLogin] = react.useState(initialCollapsedEmailLogin ?? false);
  const [error, setError] = react.useState();
  const redirectToBacklink = useRedirectToBacklink.useRedirectToBacklinkCallback();
  const idpAutoInit = useIDPAutoInit.useIDPAutoInit({ providers: identityProviders });
  const idpHandlerFeedback = IDPResponseHandler.useResponseHandlerFeedback({ onLogin: redirectToBacklink });
  const errors = react.useMemo(() => [
    ...error ? [{ message: error }] : [],
    ...idpAutoInit.type === "failed" ? [{ message: idpAutoInit.error }] : []
  ], [error, idpAutoInit]);
  if (idpHandlerFeedback !== null) {
    return idpHandlerFeedback;
  }
  if (idpAutoInit.type === "processing") {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.SpinnerOverlay, {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 191,
      columnNumber: 10
    }, void 0);
  }
  const visibleIdentityProviders = identityProviders.filter((it) => !it.hidden);
  const showAlternativeLoginButtons = visibleIdentityProviders.length > 0 || collapsedEmailLogin;
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.ErrorList, { errors }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 200,
      columnNumber: 3
    }, void 0),
    !collapsedEmailLogin && /* @__PURE__ */ jsxDevRuntime.jsxDEV(Login.Login, { resetLink: resetRequestPageName, onLogin: redirectToBacklink }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 201,
      columnNumber: 28
    }, void 0),
    showAlternativeLoginButtons && /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { children: [
      visibleIdentityProviders.map((it, i) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(IDPInitButton.IDPInitButton, { provider: it, onError: setError }, i, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 204,
        columnNumber: 46
      }, void 0)),
      collapsedEmailLogin && /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { onClick: () => setCollapsedEmailLogin(false), children: "Login with email" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 205,
        columnNumber: 29
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 203,
      columnNumber: 4
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
    lineNumber: 199,
    columnNumber: 9
  }, void 0);
};
exports.LoginEntrypoint = LoginEntrypoint;
//# sourceMappingURL=LoginEntrypoint.cjs.map
