import { jsxDEV } from "react/jsx-dev-runtime";
import { useShowToast, Stack, FieldContainer, PasswordInput, Button } from "@contember/ui";
import { useCallback } from "react";
import { useResetPassword } from "../../mutations/password/resetPassword.js";
import { useRedirect } from "@contember/react-routing";
import { useForm } from "../../lib/useForm.js";
const initialValues = {
  password: "",
  passwordAgain: ""
};
const ResetPasswordForm = ({ redirectOnSuccess, token }) => {
  const redirect = useRedirect();
  const addToast = useShowToast();
  const resetPassword = useResetPassword();
  const { register, isSubmitting, onSubmit } = useForm(
    initialValues,
    useCallback(
      async (values) => {
        if (values.password !== values.passwordAgain) {
          return addToast({ message: `Passwords does not match`, type: "error", dismiss: true });
        }
        const response = await resetPassword({
          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, token]
    )
  );
  return /* @__PURE__ */ jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDEV(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(FieldContainer, { label: "Password again", children: /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(FieldContainer, { label: void 0, children: /* @__PURE__ */ jsxDEV(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);
};
export {
  ResetPasswordForm
};
//# sourceMappingURL=ResetPasswordForm.js.map
