"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-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__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxRuntime.jsx(ui.PasswordInput, { ...register("password"), required: true }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Password again", children: /* @__PURE__ */ jsxRuntime.jsx(ui.PasswordInput, { ...register("passwordAgain"), required: true }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: void 0, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Set new password" }) })
  ] }) });
};
exports.ResetPasswordForm = ResetPasswordForm;
//# sourceMappingURL=ResetPasswordForm.cjs.map
