import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { ContemberClient } from "@contember/react-client";
import { StyleProvider, ToasterProvider, Toaster, Button, Icon, ErrorList, Stack } from "@contember/ui";
import { useState, useEffect, useMemo } from "react";
import { MiscPageLayout } from "../MiscPageLayout.js";
import { Pages } from "../pageRouting/Pages.js";
import { Page } from "../pageRouting/Page.js";
import { CreateResetPasswordRequestForm } from "../../tenant/components/password/CreateResetPasswordRequestForm.js";
import { FillResetPasswordTokenForm } from "../../tenant/components/password/FillResetPasswordTokenForm.js";
import { ResetPasswordForm } from "../../tenant/components/password/ResetPasswordForm.js";
import { useRedirectToBacklink, useRedirectToBacklinkCallback } from "../../tenant/components/person/useRedirectToBacklink.js";
import { ProjectListButtons } from "../Project/ProjectList.js";
import { useIDPAutoInit } from "../../tenant/components/idp/useIDPAutoInit.js";
import { useResponseHandlerFeedback } from "../../tenant/components/idp/IDPResponseHandler.js";
import { Login } from "../../tenant/components/person/Login.js";
import { IDPInitButton } from "../../tenant/components/idp/IDPInitButton.js";
import { RoutingContext } from "../../routing/RoutingContext.js";
import { RequestProvider } from "../../routing/RequestContext.js";
import { IdentityProvider } from "../Identity/IdentityProvider.js";
import { Link } from "../../routing/binding/Link.js";
import { useLogout } from "../Identity/useLogout.js";
import { useOptionalIdentity } from "../Identity/useOptionalIdentity.js";
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__ */ jsxDEV(
    ContemberClient,
    {
      apiBaseUrl: props.apiBaseUrl,
      sessionToken: props.sessionToken,
      loginToken: props.loginToken,
      children: /* @__PURE__ */ jsxDEV(StyleProvider, { children: /* @__PURE__ */ jsxDEV(ToasterProvider, { children: [
        /* @__PURE__ */ jsxDEV(RoutingContext.Provider, { value: routing, children: /* @__PURE__ */ jsxDEV(RequestProvider, { children: /* @__PURE__ */ jsxDEV(Pages, { children: [
          /* @__PURE__ */ jsxDEV(Page, { name: indexPageName, children: /* @__PURE__ */ jsxDEV(IdentityProvider, { allowUnauthenticated: true, children: /* @__PURE__ */ 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: 65,
              columnNumber: 11
            },
            globalThis
          ) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 64,
            columnNumber: 10
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 63,
            columnNumber: 9
          }, globalThis),
          /* @__PURE__ */ jsxDEV(Page, { name: resetRequestPageName, children: /* @__PURE__ */ jsxDEV(MiscPageLayout, { heading: "Password reset", actions: /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Link, { to: indexPageName, children: "← Back to login" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 77,
            columnNumber: 11
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 76,
            columnNumber: 60
          }, globalThis), children: /* @__PURE__ */ jsxDEV(CreateResetPasswordRequestForm, { redirectOnSuccess: redirectOnSuccessPageName }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 79,
            columnNumber: 11
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 76,
            columnNumber: 10
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 75,
            columnNumber: 9
          }, globalThis),
          /* @__PURE__ */ jsxDEV(Page, { name: redirectOnSuccessPageName, children: /* @__PURE__ */ jsxDEV(MiscPageLayout, { heading: "Password reset", actions: /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Link, { to: indexPageName, children: "← Back to login" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 84,
            columnNumber: 11
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 83,
            columnNumber: 60
          }, globalThis), children: [
            /* @__PURE__ */ 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: 86,
              columnNumber: 11
            }, globalThis),
            /* @__PURE__ */ 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: 89,
              columnNumber: 11
            }, globalThis),
            /* @__PURE__ */ jsxDEV(FillResetPasswordTokenForm, { resetLink: `${passwordResetPageName}(token: $token)` }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
              lineNumber: 92,
              columnNumber: 11
            }, globalThis)
          ] }, void 0, true, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 83,
            columnNumber: 10
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 82,
            columnNumber: 9
          }, globalThis),
          /* @__PURE__ */ jsxDEV(Page, { name: passwordResetPageName, children: ({ token }) => /* @__PURE__ */ jsxDEV(MiscPageLayout, { heading: "Set a new password", actions: /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Link, { to: indexPageName, children: "← Back to login" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 98,
            columnNumber: 12
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 97,
            columnNumber: 65
          }, globalThis), children: /* @__PURE__ */ jsxDEV(ResetPasswordForm, { token, redirectOnSuccess: indexPageName }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 100,
            columnNumber: 12
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 97,
            columnNumber: 11
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 95,
            columnNumber: 9
          }, globalThis)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 62,
          columnNumber: 8
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 61,
          columnNumber: 7
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 60,
          columnNumber: 6
        }, globalThis),
        /* @__PURE__ */ jsxDEV(Toaster, {}, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 107,
          columnNumber: 6
        }, globalThis)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 59,
        columnNumber: 5
      }, globalThis) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 58,
        columnNumber: 4
      }, globalThis)
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 53,
      columnNumber: 3
    },
    globalThis
  );
};
const LoginEntrypointIndex = (props) => {
  const logout = useLogout();
  const identity = useOptionalIdentity();
  const [projectSlugs, setProjectSlugs] = useState();
  const projectsProvider = props.projects;
  useEffect(() => {
    (async () => {
      if (identity === void 0) {
        return;
      }
      setProjectSlugs(projectsProvider instanceof Function ? await projectsProvider() : projectsProvider);
    })();
  }, [identity, projectsProvider]);
  const projects = useMemo(() => {
    return identity?.projects.filter((it) => projectSlugs?.includes(it.slug));
  }, [identity?.projects, projectSlugs]);
  useRedirectToBacklink();
  if (identity === void 0) {
    return /* @__PURE__ */ jsxDEV(MiscPageLayout, { heading: props.heading ?? "Contember Admin", children: /* @__PURE__ */ jsxDEV(LoginContainer, { identityProviders: props.identityProviders, collapsedEmailLogin: props.collapsedEmailLogin }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 137,
      columnNumber: 5
    }, globalThis) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 136,
      columnNumber: 4
    }, globalThis);
  } else if (projects === void 0) {
    return /* @__PURE__ */ jsxDEV(
      MiscPageLayout,
      {
        heading: "Projects",
        actions: /* @__PURE__ */ jsxDEV(Fragment, { children: [
          props.projectsPageActions,
          /* @__PURE__ */ jsxDEV(Button, { onClick: () => logout(), size: "small", distinction: "seamless", children: /* @__PURE__ */ jsxDEV(Icon, { blueprintIcon: "log-out" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 147,
            columnNumber: 79
          }, globalThis) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 147,
            columnNumber: 6
          }, globalThis)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 145,
          columnNumber: 14
        }, globalThis),
        children: "Loading projects..."
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 143,
        columnNumber: 4
      },
      globalThis
    );
  } else if (projects.length === 1) {
    window.location.href = props.formatProjectUrl(projects[0]);
    return null;
  } else {
    return /* @__PURE__ */ jsxDEV(
      MiscPageLayout,
      {
        heading: "Projects",
        actions: props.projectsPageActions,
        footerActions: /* @__PURE__ */ jsxDEV(Button, { onClick: () => logout(), distinction: "seamless", flow: "block", children: [
          "Sign out ",
          /* @__PURE__ */ jsxDEV(Icon, { blueprintIcon: "log-out" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
            lineNumber: 163,
            columnNumber: 98
          }, globalThis)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 163,
          columnNumber: 20
        }, globalThis),
        children: /* @__PURE__ */ jsxDEV(ProjectListButtons, { projects, formatProjectUrl: props.formatProjectUrl }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
          lineNumber: 165,
          columnNumber: 5
        }, globalThis)
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 160,
        columnNumber: 4
      },
      globalThis
    );
  }
};
const LoginContainer = ({ identityProviders = [], collapsedEmailLogin: initialCollapsedEmailLogin }) => {
  const [collapsedEmailLogin, setCollapsedEmailLogin] = useState(initialCollapsedEmailLogin ?? false);
  const [error, setError] = useState();
  const redirectToBacklink = useRedirectToBacklinkCallback();
  useIDPAutoInit({ onError: setError, providers: identityProviders });
  const idpHandlerFeedback = useResponseHandlerFeedback({ onLogin: redirectToBacklink });
  if (idpHandlerFeedback !== null) {
    return idpHandlerFeedback;
  }
  const visibleIdentityProviders = identityProviders.filter((it) => !it.hidden);
  const showAlternativeLoginButtons = visibleIdentityProviders.length > 0 || collapsedEmailLogin;
  return /* @__PURE__ */ jsxDEV(Fragment, { children: [
    /* @__PURE__ */ jsxDEV(ErrorList, { errors: error ? [{ message: error }] : [] }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 194,
      columnNumber: 3
    }, globalThis),
    !collapsedEmailLogin && /* @__PURE__ */ jsxDEV(Login, { resetLink: resetRequestPageName, onLogin: redirectToBacklink }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 195,
      columnNumber: 28
    }, globalThis),
    showAlternativeLoginButtons && /* @__PURE__ */ jsxDEV(Stack, { direction: "vertical", children: [
      visibleIdentityProviders.map((it, i) => /* @__PURE__ */ jsxDEV(IDPInitButton, { provider: it, onError: setError }, i, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 198,
        columnNumber: 46
      }, globalThis)),
      collapsedEmailLogin && /* @__PURE__ */ jsxDEV(Button, { onClick: () => setCollapsedEmailLogin(false), children: "Login with email" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
        lineNumber: 199,
        columnNumber: 29
      }, globalThis)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 197,
      columnNumber: 4
    }, globalThis)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
    lineNumber: 193,
    columnNumber: 9
  }, globalThis);
};
export {
  LoginEntrypoint
};
//# sourceMappingURL=LoginEntrypoint.js.map
