import { jsxDEV } from "react/jsx-dev-runtime";
import { getTenantErrorMessage } from "@contember/client";
import { useSetSessionToken } from "@contember/react-client";
import { Stack, ErrorList, FieldContainer, EmailInput, PasswordInput, TextInput, Button } from "@contember/ui";
import { useState, useCallback } from "react";
import { useSignIn } from "../../mutations/identity/signIn.js";
import { Link } from "../../../routing/Link.js";
import { useForm } from "../../lib/useForm.js";
const initialValues = {
  email: "",
  password: "",
  otpToken: ""
};
const Login = ({ onLogin, resetLink }) => {
  const [errors, setErrors] = useState([]);
  const [otpRequired, setOtpRequired] = useState(false);
  const login = useSignIn();
  const setSessionToken = useSetSessionToken();
  const { onSubmit, isSubmitting, register } = useForm(initialValues, useCallback(async (values) => {
    const response = await login({
      email: values.email,
      password: values.password,
      otpToken: values.otpToken || void 0,
      expiration: 14 * 24 * 3600
    });
    setErrors([]);
    if (!response.ok) {
      const error = response.error;
      if (response.error.code === "OTP_REQUIRED") {
        setOtpRequired(true);
      } else {
        setErrors([{ message: getTenantErrorMessage(error.code) }]);
      }
    } else {
      setSessionToken(response.result.token);
      onLogin?.();
    }
  }, [login, onLogin, setSessionToken]));
  return /* @__PURE__ */ jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDEV(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDEV(ErrorList, { errors }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
      lineNumber: 50,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Email", children: /* @__PURE__ */ jsxDEV(
      EmailInput,
      {
        ...register("email"),
        autoComplete: "username",
        autoFocus: !otpRequired,
        disabled: otpRequired
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
        lineNumber: 52,
        columnNumber: 6
      },
      void 0
    ) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
      lineNumber: 51,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxDEV(
      PasswordInput,
      {
        ...register("password"),
        autoComplete: "current-password",
        disabled: otpRequired
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
        lineNumber: 60,
        columnNumber: 6
      },
      void 0
    ) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
      lineNumber: 59,
      columnNumber: 5
    }, void 0),
    otpRequired && /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Two-factor code", children: /* @__PURE__ */ jsxDEV(
      TextInput,
      {
        autoFocus: true,
        autoComplete: "one-time-code",
        ...register("otpToken")
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
        lineNumber: 67,
        columnNumber: 6
      },
      void 0
    ) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
      lineNumber: 66,
      columnNumber: 21
    }, void 0),
    /* @__PURE__ */ jsxDEV(Stack, { horizontal: true, align: "center", justify: "space-between", children: [
      resetLink && /* @__PURE__ */ jsxDEV(Link, { to: resetLink, children: "Forgot your password?" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
        lineNumber: 74,
        columnNumber: 20
      }, void 0),
      /* @__PURE__ */ jsxDEV(Button, { type: "submit", distinction: "primary", display: "block", disabled: isSubmitting, children: "Submit" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
        lineNumber: 75,
        columnNumber: 6
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
      lineNumber: 73,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
    lineNumber: 49,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
    lineNumber: 48,
    columnNumber: 3
  }, void 0);
};
export {
  Login
};
//# sourceMappingURL=Login.js.map
