import { r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { i as iconSuccess } from "./icon-mail-success-C8dZWFGQ.js";
import { i as iconError } from "./icon-mail-error-BTXt9n8h.js";
import { L as LoginWraper } from "./index-q4s1wmSu.js";
import { c as createStyles, z as useForm, b as Box, S as Stack, E as TextInput, B as Button, G as Group, T as Text } from "./vendor-mantine-h0WlM2Mj.js";
import { ao as useResetPasswordMutation } from "./index-DfebxkL9.js";
import { e as emailRegExp, g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { o as objectType, s as stringType, l as literalType, u as unionType } from "./types-espwwxTQ.js";
import "./vendor-icons-xWhXK_Ui.js";
const useStyles = createStyles((theme) => ({
  icon: {
    marginTop: "20px"
  },
  desc: {
    textAlign: "center",
    color: theme.colors.gray[6],
    lineHeight: "22px",
    marginBottom: "5px"
  },
  controls: {
    "& .mantine-InputWrapper-error": {
      marginBottom: "10px",
      marginTop: "10px",
      fontSize: "14px",
      position: "relative"
    }
  },
  subtleButton: {
    fontWeight: 100,
    padding: 0,
    alignSelf: "flex-end",
    "&:hover": {
      textDecoration: "underline",
      backgroundColor: "transparent"
    }
  }
}));
const RequestResetPassword = () => {
  var _a, _b, _c;
  const {
    t
  } = useTranslation();
  const [fetchResetPassword, {
    isError,
    isSuccess,
    isLoading
  }] = useResetPasswordMutation();
  const {
    classes
  } = useStyles();
  const enabledScheme = objectType({
    IsEnabled: literalType(true),
    Email: stringType().regex(emailRegExp, {
      message: t("requestResetPassword.incorrectEmail")
    }),
    AdditionalEmail: stringType().regex(emailRegExp, {
      message: t("requestResetPassword.incorrectEmail")
    })
  }).refine((data) => data.Email !== data.AdditionalEmail, {
    message: t("validation.emailsShouldNotBeTheSame"),
    path: ["AdditionalEmail"]
  });
  const disabledScheme = objectType({
    IsEnabled: literalType(false),
    Email: stringType().regex(emailRegExp, {
      message: t("requestResetPassword.incorrectEmail")
    }),
    AdditionalEmail: stringType().optional().nullable()
  });
  const validateScheme = unionType([enabledScheme, disabledScheme]);
  const form = useForm({
    validateInputOnChange: true,
    initialValues: {
      Email: "",
      AdditionalEmail: "",
      IsEnabled: false
    },
    validate: (values) => getValidateErrors(validateScheme, values)
  });
  const handleSubmit = async (values_0) => {
    try {
      const data_0 = {
        Email: values_0.Email,
        AdditionalEmail: values_0.AdditionalEmail
      };
      await fetchResetPassword(data_0);
    } catch (error) {
      console.error("Ошибка:", error);
    }
  };
  const handleShowAdditionalEmailField = reactExports.useCallback(() => {
    form.setFieldValue("IsEnabled", true);
  }, []);
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(LoginWraper, { title: !isSuccess && !isError ? t("requestResetPassword.title") : "", text: !isSuccess && !isError ? t(`requestResetPassword.${((_a = form == null ? void 0 : form.values) == null ? void 0 : _a.IsEnabled) ? "withAdditionalEmailText" : "text"}`) : "", children: [
    !isSuccess && !isError && /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { component: "form", onSubmit: form.onSubmit(handleSubmit), className: classes.controls, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { gap: "xs", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { size: "lg", radius: "md", name: "Email", variant: "filled", placeholder: t("requestResetPassword.email"), ...form.getInputProps("Email") }),
      ((_b = form == null ? void 0 : form.values) == null ? void 0 : _b.IsEnabled) ? /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { size: "lg", radius: "md", name: "Email", variant: "filled", placeholder: t("requestResetPassword.additionalEmail"), ...form.getInputProps("AdditionalEmail") }) : null,
      !((_c = form == null ? void 0 : form.values) == null ? void 0 : _c.IsEnabled) ? /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { variant: "subtle", className: classes.subtleButton, size: "md", onClick: () => handleShowAdditionalEmailField(), children: t("requestResetPassword.haveNoAccessToEmail") }) : null,
      /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { justify: "flex-start", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { size: "lg", w: "100%", type: "submit", disabled: !form.isValid(), loading: isLoading, children: t("send") }) })
    ] }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      isSuccess && /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { justify: "center", gap: "xs", children: /* @__PURE__ */ jsxRuntimeExports.jsx("img", { src: iconSuccess, alt: "icon", className: classes.icon }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.desc, children: t("requestResetPassword.successText") }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { justify: "center", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { radius: "md", w: "100%", size: "lg", component: "a", href: "/login", children: t("requestResetPassword.backMain") }) })
      ] }),
      isError && /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { justify: "center", gap: "xs", children: /* @__PURE__ */ jsxRuntimeExports.jsx("img", { src: iconError, alt: "icon", className: classes.icon }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.desc, children: t("requestResetPassword.errorText") }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { justify: "center", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { radius: "md", w: "100%", size: "lg", component: "a", href: "/password-reset", children: t("requestResetPassword.tryAgain") }) })
      ] })
    ] })
  ] });
};
export {
  RequestResetPassword
};
//# sourceMappingURL=index-BCxEOlV-.js.map
