import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { ContemberClient } from "@contember/react-client";
import { Providers, Button, Icon, SpinnerOverlay, ErrorList, Stack } from "@contember/ui";
import { useState, useEffect, useMemo } from "react";
import { MiscPageLayout } from "../MiscPageLayout.js";
import { Pages } from "../pageRouting/Pages.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, RequestProvider, Page } from "@contember/react-routing";
import { IdentityProvider } from "../Identity/IdentityProvider.js";
import { Link } from "../../routing/Link.js";
import { useLogout, useIdentity } from "@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__ */ jsxDEV(
    ContemberClient,
    {
      apiBaseUrl: props.apiBaseUrl,
      sessionToken: props.sessionToken,
      loginToken: props.loginToken,
      children: /* @__PURE__ */ jsxDEV(RoutingContext.Provider, { value: routing, children: /* @__PURE__ */ jsxDEV(RequestProvider, { children: /* @__PURE__ */ jsxDEV(Providers, { 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: 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__ */ 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: 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__ */ jsxDEV(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__ */ 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: 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__ */ 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__ */ 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__ */ jsxDEV(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__ */ 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: 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__ */ jsxDEV(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 = useLogout();
  const identity = useIdentity();
  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: 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__ */ 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: 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__ */ jsxDEV(
      MiscPageLayout,
      {
        heading: "Projects",
        actions: props.projectsPageActions,
        footerActions: /* @__PURE__ */ jsxDEV(Button, { onClick: () => logout(), distinction: "seamless", display: "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: 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__ */ jsxDEV(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] = useState(initialCollapsedEmailLogin ?? false);
  const [error, setError] = useState();
  const redirectToBacklink = useRedirectToBacklinkCallback();
  const idpAutoInit = useIDPAutoInit({ providers: identityProviders });
  const idpHandlerFeedback = useResponseHandlerFeedback({ onLogin: redirectToBacklink });
  const errors = useMemo(() => [
    ...error ? [{ message: error }] : [],
    ...idpAutoInit.type === "failed" ? [{ message: idpAutoInit.error }] : []
  ], [error, idpAutoInit]);
  if (idpHandlerFeedback !== null) {
    return idpHandlerFeedback;
  }
  if (idpAutoInit.type === "processing") {
    return /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(Fragment, { children: [
    /* @__PURE__ */ jsxDEV(ErrorList, { errors }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Login/LoginEntrypoint.tsx",
      lineNumber: 200,
      columnNumber: 3
    }, void 0),
    !collapsedEmailLogin && /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(Stack, { 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: 204,
        columnNumber: 46
      }, void 0)),
      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: 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);
};
export {
  LoginEntrypoint
};
//# sourceMappingURL=LoginEntrypoint.js.map
