"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const client = require("@contember/client");
const reactClient = require("@contember/react-client");
const ui = require("@contember/ui");
const react = require("react");
const signIn = require("../../mutations/identity/signIn.cjs");
const Link = require("../../../routing/Link.cjs");
const useForm = require("../../lib/useForm.cjs");
const initialValues = {
  email: "",
  password: "",
  otpToken: ""
};
const Login = ({ onLogin, resetLink }) => {
  const [errors, setErrors] = react.useState([]);
  const [otpRequired, setOtpRequired] = react.useState(false);
  const login = signIn.useSignIn();
  const setSessionToken = reactClient.useSetSessionToken();
  const { onSubmit, isSubmitting, register } = useForm.useForm(initialValues, react.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: client.getTenantErrorMessage(error.code) }]);
      }
    } else {
      setSessionToken(response.result.token);
      onLogin?.();
    }
  }, [login, onLogin, setSessionToken]));
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.ErrorList, { errors }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/person/Login.tsx",
      lineNumber: 50,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Email", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      ui.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__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      ui.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__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Two-factor code", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      ui.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__ */ jsxDevRuntime.jsxDEV(ui.Stack, { horizontal: true, align: "center", justify: "space-between", children: [
      resetLink && /* @__PURE__ */ jsxDevRuntime.jsxDEV(Link.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__ */ jsxDevRuntime.jsxDEV(ui.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);
};
exports.Login = Login;
//# sourceMappingURL=Login.cjs.map
