import { jsx, jsxs } from "react/jsx-runtime";
import { useShowToast, Stack, FieldContainer, EmailInput, Button } from "@contember/ui";
import { useCallback } from "react";
import { useCreateResetPasswordRequest } from "../../mutations/password/createResetPasswordRequest.js";
import { useRedirect } from "@contember/react-routing";
import { useForm } from "../../lib/useForm.js";
const initialValues = {
  email: ""
};
const CreateResetPasswordRequestForm = ({ redirectOnSuccess }) => {
  const redirect = useRedirect();
  const addToast = useShowToast();
  const createResetPasswordRequest = useCreateResetPasswordRequest();
  const { register, isSubmitting, onSubmit } = useForm(initialValues, useCallback(
    async (values) => {
      const response = await createResetPasswordRequest({
        email: values.email
      });
      if (response.ok) {
        addToast({
          type: "success",
          message: `Check your inbox for the instructions.`,
          dismiss: true
        });
        redirect(redirectOnSuccess);
      } else {
        switch (response.error.code) {
          case "PERSON_NOT_FOUND":
            return addToast({ message: `E-mail not found`, type: "error" });
        }
      }
    },
    [addToast, createResetPasswordRequest, redirect, redirectOnSuccess]
  ));
  return /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsx(FieldContainer, { label: "E-mail", children: /* @__PURE__ */ jsx(EmailInput, { ...register("email"), required: true }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: void 0, children: /* @__PURE__ */ jsx(Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Reset password" }) })
  ] }) });
};
export {
  CreateResetPasswordRequestForm
};
//# sourceMappingURL=CreateResetPasswordRequestForm.js.map
