"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const ui = require("@contember/ui");
const react = require("react");
const resetPassword = require("../../mutations/password/resetPassword.cjs");
const reactRouting = require("@contember/react-routing");
const useForm = require("../../lib/useForm.cjs");
const initialValues = {
  password: "",
  passwordAgain: ""
};
const ResetPasswordForm = ({ redirectOnSuccess, token }) => {
  const redirect = reactRouting.useRedirect();
  const addToast = ui.useShowToast();
  const resetPassword$1 = resetPassword.useResetPassword();
  const { register, isSubmitting, onSubmit } = useForm.useForm(
    initialValues,
    react.useCallback(
      async (values) => {
        if (values.password !== values.passwordAgain) {
          return addToast({ message: `Passwords does not match`, type: "error", dismiss: true });
        }
        const response = await resetPassword$1({
          password: values.password,
          token
        });
        if (response.ok) {
          addToast({
            type: "success",
            message: `Password successfully set.`,
            dismiss: true
          });
          redirect(redirectOnSuccess);
        } else {
          switch (response.error.code) {
            case "PASSWORD_TOO_WEAK":
              return addToast({ message: `Password is too weak`, type: "error", dismiss: true });
            case "TOKEN_NOT_FOUND":
            case "TOKEN_USED":
            case "TOKEN_EXPIRED":
              return addToast({ message: `Reset link is not valid`, type: "error", dismiss: true });
          }
        }
      },
      [addToast, redirect, redirectOnSuccess, resetPassword$1, token]
    )
  );
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.PasswordInput, { ...register("password"), required: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
      lineNumber: 57,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
      lineNumber: 56,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Password again", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.PasswordInput, { ...register("passwordAgain"), required: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
      lineNumber: 60,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
      lineNumber: 59,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: void 0, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Set new password" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
      lineNumber: 63,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
      lineNumber: 62,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
    lineNumber: 55,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ResetPasswordForm.tsx",
    lineNumber: 54,
    columnNumber: 3
  }, void 0);
};
exports.ResetPasswordForm = ResetPasswordForm;
//# sourceMappingURL=ResetPasswordForm.cjs.map
