import { useCallback, createElement } from "react";
import { useShowToast, Stack, FieldContainer, TextInput, Button } from "@contember/ui";
import { useCreateResetPasswordRequest } from "../../mutations/password/createResetPasswordRequest.js";
import { useRedirect } from "../../../routing/binding/useRedirect.js";
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__ */ createElement("form", {
    onSubmit
  }, /* @__PURE__ */ createElement(Stack, {
    direction: "vertical",
    gap: "large"
  }, /* @__PURE__ */ createElement(FieldContainer, {
    label: "E-mail"
  }, /* @__PURE__ */ createElement(TextInput, {
    ...register("email"),
    required: true,
    type: "email"
  })), /* @__PURE__ */ createElement(FieldContainer, {
    label: void 0
  }, /* @__PURE__ */ createElement(Button, {
    distinction: "primary",
    type: "submit",
    disabled: isSubmitting
  }, "Reset password"))));
};
export { CreateResetPasswordRequestForm };
//# sourceMappingURL=CreateResetPasswordRequestForm.js.map
