"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 createResetPasswordRequest = require("../../mutations/password/createResetPasswordRequest.cjs");
const reactRouting = require("@contember/react-routing");
const useForm = require("../../lib/useForm.cjs");
const initialValues = {
  email: ""
};
const CreateResetPasswordRequestForm = ({ redirectOnSuccess }) => {
  const redirect = reactRouting.useRedirect();
  const addToast = ui.useShowToast();
  const createResetPasswordRequest$1 = createResetPasswordRequest.useCreateResetPasswordRequest();
  const { register, isSubmitting, onSubmit } = useForm.useForm(initialValues, react.useCallback(
    async (values) => {
      const response = await createResetPasswordRequest$1({
        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$1, redirect, redirectOnSuccess]
  ));
  return /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "E-mail", children: /* @__PURE__ */ jsxRuntime.jsx(ui.EmailInput, { ...register("email"), required: true }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: void 0, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Reset password" }) })
  ] }) });
};
exports.CreateResetPasswordRequestForm = CreateResetPasswordRequestForm;
//# sourceMappingURL=CreateResetPasswordRequestForm.cjs.map
