import { jsxDEV } from "react/jsx-dev-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__ */ jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDEV(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "E-mail", children: /* @__PURE__ */ jsxDEV(EmailInput, { ...register("email"), required: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/CreateResetPasswordRequestForm.tsx",
      lineNumber: 46,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/CreateResetPasswordRequestForm.tsx",
      lineNumber: 45,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: void 0, children: /* @__PURE__ */ jsxDEV(Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Reset password" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/CreateResetPasswordRequestForm.tsx",
      lineNumber: 49,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/CreateResetPasswordRequestForm.tsx",
      lineNumber: 48,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/CreateResetPasswordRequestForm.tsx",
    lineNumber: 44,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/CreateResetPasswordRequestForm.tsx",
    lineNumber: 43,
    columnNumber: 3
  }, void 0);
};
export {
  CreateResetPasswordRequestForm
};
//# sourceMappingURL=CreateResetPasswordRequestForm.js.map
