import { E as compilerRuntimeExports, j as jsxRuntimeExports, r as reactExports, a0 as useParams } from "./vendor-react-DYNCg6N5.js";
import { c as useAppSelector, al as useLazyUpdatePasswordWithCodeQuery, am as useLazyCheckUserWithCodeQuery, e as isCrypt } from "./index-DfebxkL9.js";
import { u as useStyles$1, I as InvalidLinkContent } from "./InvalidLinkContent-CWLs7Xhy.js";
import { g as getPasswordStrength, a as generatePassword, b as generatePasswordRegExp } from "./passwordUtils-B3CN2t6O.js";
import { p as preserveCursorPosition, u as useFetchToCrypt } from "./preserveCursorPosition-NFvD_VQV.js";
import { L as LoginWraper } from "./index-q4s1wmSu.js";
import { a as IUpdatePasswordWithCodeEnum } from "./types-B9VC9FeZ.js";
import { i as iconSuccess } from "./icon-mail-success-C8dZWFGQ.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { T as Text, G as Group, B as Button, b as Box, c as createStyles, n as useDisclosure, J as PasswordInput, a1 as Progress, k as Tooltip, h as ActionIcon, z as useForm, a9 as zodResolver, V as LoadingOverlay } from "./vendor-mantine-h0WlM2Mj.js";
import { m as Dze } from "./vendor-icons-xWhXK_Ui.js";
import { u as useCopyToClipboard } from "./useCopyToClipboard-C-S2RacD.js";
import { u as usePasswordSchema } from "./useGeneratePolicy-B4rO5xUa.js";
import { u as useGenerateRules, R as RulesConditionList, d as defaultMailServerPasswordOptions } from "./passwordOptions-b-kjbiiT.js";
import { o as objectType, s as stringType } from "./types-espwwxTQ.js";
const SuccessResetContent = () => {
  const $ = compilerRuntimeExports.c(17);
  const {
    t
  } = useTranslation();
  const {
    classes
  } = useStyles$1();
  const t0 = classes.responseContent;
  let t1;
  if ($[0] !== classes.icon) {
    t1 = /* @__PURE__ */ jsxRuntimeExports.jsx("img", { src: iconSuccess, alt: "icon", className: classes.icon });
    $[0] = classes.icon;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  const t2 = classes.desc;
  let t3;
  if ($[2] !== t) {
    t3 = t("passwordReset.passwordChangedSuccessfully");
    $[2] = t;
    $[3] = t3;
  } else {
    t3 = $[3];
  }
  let t4;
  if ($[4] !== classes.desc || $[5] !== t3) {
    t4 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t2, ta: "center", children: t3 });
    $[4] = classes.desc;
    $[5] = t3;
    $[6] = t4;
  } else {
    t4 = $[6];
  }
  const t5 = classes.button;
  let t6;
  if ($[7] !== t) {
    t6 = t("backMain");
    $[7] = t;
    $[8] = t6;
  } else {
    t6 = $[8];
  }
  let t7;
  if ($[9] !== classes.button || $[10] !== t6) {
    t7 = /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { justify: "center", gap: "xs", w: "100%", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { className: t5, size: "lg", component: "a", href: "/", children: t6 }) });
    $[9] = classes.button;
    $[10] = t6;
    $[11] = t7;
  } else {
    t7 = $[11];
  }
  let t8;
  if ($[12] !== classes.responseContent || $[13] !== t1 || $[14] !== t4 || $[15] !== t7) {
    t8 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: t0, children: [
      t1,
      t4,
      t7
    ] });
    $[12] = classes.responseContent;
    $[13] = t1;
    $[14] = t4;
    $[15] = t7;
    $[16] = t8;
  } else {
    t8 = $[16];
  }
  return t8;
};
const useStyles = createStyles((theme) => ({
  container: {
    width: "100%"
  },
  input: {
    width: "100%",
    marginRight: theme.spacing.xs,
    "& .mantine-PasswordInput-label, .mantine-InputWrapper-label": {
      color: theme.colors.brandGray[0],
      fontSize: "12px"
    }
  },
  generateButton: {
    display: "flex",
    alignItems: "center",
    fontSize: "14px",
    color: theme.colors.gray[8],
    border: `1px solid #DEE2E6`,
    padding: "3px 10px",
    borderRadius: "4px",
    width: "100%"
  },
  icon: {
    flex: "0 0 160px",
    height: "36px",
    display: "flex",
    flexDirection: "row",
    justifyContent: "end",
    alignItems: "center",
    "&:hover": {
      backgroundColor: "transparent"
    },
    svg: {
      marginRight: "5px"
    }
  },
  field: {
    display: "flex",
    justifyContent: "space-between",
    alignItems: "start",
    marginTop: "8px"
  },
  passwordProgress: {},
  passwordStatus: {
    color: theme.colors.gray[8]
  },
  hint: {
    color: theme.colors.gray[6],
    margin: "5px 0 24px 0"
  },
  progress: {
    marginBottom: theme.spacing.xs,
    marginTop: theme.spacing.xs,
    width: "100%"
  }
}));
const getPasswordStrengthByRules = (password, rules) => {
  if (!password) return 10;
  const passedCount = rules.filter((rule) => rule.condition).length;
  const totalCount = rules.length;
  return Math.max(10, Math.round(passedCount / (totalCount || 1) * 100));
};
const PasswordInputStrengthMeter = ({
  value,
  label,
  summary,
  placeholder,
  onChange,
  rules,
  handleGeneratePassword,
  size = "sm",
  error = ""
}) => {
  const {
    t
  } = useTranslation();
  const {
    classes
  } = useStyles();
  const strength = getPasswordStrengthByRules(value, rules);
  const {
    color,
    strengthString
  } = getPasswordStrength(strength, t);
  const [visible, {
    toggle
  }] = useDisclosure(false);
  const passwordInputStrengthRef = reactExports.useRef(null);
  const fixCursorLocation = (ref) => {
    return () => {
      preserveCursorPosition(ref, toggle);
    };
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.container, children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(PasswordInput, { size, radius: "md", variant: "filled", "data-value": "password", label, placeholder, value, onChange, className: classes.input, withAsterisk: true, error, ref: passwordInputStrengthRef, visible, onVisibilityChange: fixCursorLocation(passwordInputStrengthRef) }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.field, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.passwordProgress, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.passwordStatus, fz: "sm", children: `${t("passwordReset.passwordStrength")}: ${strengthString}` }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Progress, { color, value: strength, size: 5, className: classes.progress })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { label: t("requestResetPassword.generateStrongPassword"), children: /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { className: classes.icon, onClick: handleGeneratePassword, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.generateButton, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Dze, {}),
        t("passwordReset.create")
      ] }) }) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { fz: "xs", className: classes.hint, children: summary })
  ] });
};
const ResetPasswordForm = ({
  passwordRegexp,
  handleUpdatePassword,
  passwordStrengthOptions
}) => {
  const {
    t
  } = useTranslation();
  const {
    classes
  } = useStyles$1();
  const [visible, {
    toggle
  }] = useDisclosure(false);
  const {
    copy
  } = useCopyToClipboard();
  const passwordInputRef = reactExports.useRef(null);
  const passwordRulesZ = usePasswordSchema(passwordStrengthOptions || defaultMailServerPasswordOptions, t);
  const schema = objectType({
    Password: passwordRulesZ,
    ConfirmPassword: stringType()
  }).refine((data) => {
    var _a;
    return !((_a = data == null ? void 0 : data.ConfirmPassword) == null ? void 0 : _a.length) || (data == null ? void 0 : data.Password) === (data == null ? void 0 : data.ConfirmPassword);
  }, {
    message: t("passwordReset.passwordsDoNotMatch"),
    path: ["ConfirmPassword"]
  });
  const form = useForm({
    initialValues: {
      Password: "",
      ConfirmPassword: ""
    },
    validateInputOnChange: true,
    validate: zodResolver(schema)
  });
  const {
    Password,
    ConfirmPassword
  } = form.values;
  const onChange = reactExports.useCallback((event) => {
    const type = event.currentTarget.dataset.value ?? "";
    const value = event.currentTarget.value.replace(/\s+/g, "");
    switch (type) {
      case "password":
        form.setFieldValue("Password", value);
        break;
      case "confirm-password":
        form.setFieldValue("ConfirmPassword", value);
        break;
    }
    form.validate();
  }, [Password, ConfirmPassword, passwordRegexp]);
  const password = Password;
  const rules = useGenerateRules(password, passwordStrengthOptions);
  const summary = `${t("register.passwordAllowedStart")} ${rules.map((i) => i.shortLabel).join(", ")}.`;
  const handleSubmit = async () => {
    const values = form.values;
    handleUpdatePassword(values);
  };
  const handleGeneratePassword = () => {
    const pass = generatePassword(passwordRegexp, passwordStrengthOptions == null ? void 0 : passwordStrengthOptions.MinLength);
    if (pass && passwordRegexp.test(pass)) {
      form.setFieldValue("Password", pass);
      form.setFieldValue("ConfirmPassword", pass);
    }
    copy(pass);
  };
  reactExports.useEffect(() => {
    if (Password.length) {
      if (passwordRegexp.test(Password)) {
        form.clearFieldError("Password");
      }
    }
  }, [Password, passwordRegexp]);
  reactExports.useEffect(() => {
    const validatePasswordMatch = () => {
      if (ConfirmPassword.length) {
        if (Password === ConfirmPassword) {
          form.clearFieldError("ConfirmPassword");
        } else {
          form.setFieldError("ConfirmPassword", t("passwordReset.passwordsDoNotMatch"));
        }
      } else {
        form.clearFieldError("ConfirmPassword");
      }
    };
    validatePasswordMatch();
  }, [Password, ConfirmPassword]);
  reactExports.useEffect(() => {
    if (passwordRegexp && Password) {
      form.validateField("Password");
    }
  }, [passwordRegexp, Password]);
  const fixCursorLocation = () => {
    preserveCursorPosition(passwordInputRef, toggle);
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit: form.onSubmit(handleSubmit), children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(RulesConditionList, { rules, check: password.length > 0 }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(PasswordInputStrengthMeter, { rules, summary, size: "lg", radius: "md", variant: "filled", placeholder: t("passwordReset.newPassword"), label: "", value: form.getInputProps("Password").value, onChange, handleGeneratePassword, error: form.getInputProps("Password").error }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(PasswordInput, { size: "lg", variant: "filled", placeholder: t("passwordReset.confirmNewPassword"), radius: "md", "data-value": "confirm-password", label: "", withAsterisk: true, visible, onVisibilityChange: fixCursorLocation, onChange, ref: passwordInputRef, value: form.getInputProps("ConfirmPassword").value, error: form.getInputProps("ConfirmPassword").error, className: classes.input }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { size: "lg", disabled: !form.isValid() || Password !== ConfirmPassword, type: "submit", fullWidth: true, mt: "xl", children: t("save") })
  ] });
};
const ResetPassword = () => {
  const $ = compilerRuntimeExports.c(37);
  const {
    t
  } = useTranslation();
  const {
    code: t0
  } = useParams();
  const code = t0 === void 0 ? "" : t0;
  const {
    classes
  } = useStyles$1();
  const {
    Settings
  } = useAppSelector(_temp);
  const fetchToCrypt = useFetchToCrypt();
  const [updatePasswordWithCode, t1] = useLazyUpdatePasswordWithCodeQuery();
  const {
    data,
    isSuccess,
    isError
  } = t1;
  const [checkUserWithCode, t2] = useLazyCheckUserWithCodeQuery();
  const {
    data: checkUserCodeData
  } = t2;
  const [isCodeValid, setIsCodeValid] = reactExports.useState(null);
  const [isRequestCompleted, setIsRequestCompleted] = reactExports.useState(false);
  let t3;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t3 = {};
    $[0] = t3;
  } else {
    t3 = $[0];
  }
  const [passwordStrengthOptions, setPasswordStrengthOptions] = reactExports.useState(t3);
  let t4;
  if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
    t4 = generatePasswordRegExp({});
    $[1] = t4;
  } else {
    t4 = $[1];
  }
  const [passwordRegexp, setPasswordRegexp] = reactExports.useState(t4);
  let t5;
  if ($[2] !== Settings || $[3] !== code || $[4] !== fetchToCrypt || $[5] !== updatePasswordWithCode) {
    t5 = async (values) => {
      if (isCrypt(Settings)) {
        await fetchToCrypt(values.Password, (encryptedBase64) => {
          updatePasswordWithCode({
            UserPassword: "",
            CryptedPassword: encryptedBase64,
            Code: code
          });
        });
      } else {
        updatePasswordWithCode({
          UserPassword: values.Password,
          Code: code
        });
      }
    };
    $[2] = Settings;
    $[3] = code;
    $[4] = fetchToCrypt;
    $[5] = updatePasswordWithCode;
    $[6] = t5;
  } else {
    t5 = $[6];
  }
  const handleUpdatePassword = t5;
  let t6;
  let t7;
  if ($[7] !== checkUserWithCode || $[8] !== code) {
    t6 = () => {
      checkUserWithCode({
        Code: code
      }).then(() => {
        setIsRequestCompleted(true);
      });
    };
    t7 = [code, checkUserWithCode];
    $[7] = checkUserWithCode;
    $[8] = code;
    $[9] = t6;
    $[10] = t7;
  } else {
    t6 = $[9];
    t7 = $[10];
  }
  reactExports.useEffect(t6, t7);
  let t8;
  let t9;
  if ($[11] !== checkUserCodeData) {
    t8 = () => {
      if (checkUserCodeData) {
        setIsCodeValid(checkUserCodeData == null ? void 0 : checkUserCodeData.IsActive);
        setPasswordStrengthOptions({
          MinLength: checkUserCodeData == null ? void 0 : checkUserCodeData.MinLength,
          UseCapitalLetters: checkUserCodeData.UseCapitalLetters,
          UseDigital: checkUserCodeData == null ? void 0 : checkUserCodeData.UseDigital,
          UseSpecialCharacters: checkUserCodeData.UseSpecialCharacters,
          UseAllowedSpecialCharacters: checkUserCodeData.UseAllowedSpecialCharacters,
          UseLowLetter: checkUserCodeData.UseLowLetter
        });
      }
    };
    t9 = [checkUserCodeData];
    $[11] = checkUserCodeData;
    $[12] = t8;
    $[13] = t9;
  } else {
    t8 = $[12];
    t9 = $[13];
  }
  reactExports.useEffect(t8, t9);
  let t10;
  let t11;
  if ($[14] !== passwordStrengthOptions) {
    t10 = () => {
      if (passwordStrengthOptions) {
        const regexp = generatePasswordRegExp({
          options: passwordStrengthOptions
        });
        setPasswordRegexp(regexp);
      }
    };
    t11 = [passwordStrengthOptions];
    $[14] = passwordStrengthOptions;
    $[15] = t10;
    $[16] = t11;
  } else {
    t10 = $[15];
    t11 = $[16];
  }
  reactExports.useEffect(t10, t11);
  let t12;
  if ($[17] !== isCodeValid || $[18] !== isError || $[19] !== isRequestCompleted || $[20] !== isSuccess || $[21] !== t) {
    t12 = !isRequestCompleted || !isCodeValid || isSuccess || isError ? "" : t("passwordReset.passwordRecovery");
    $[17] = isCodeValid;
    $[18] = isError;
    $[19] = isRequestCompleted;
    $[20] = isSuccess;
    $[21] = t;
    $[22] = t12;
  } else {
    t12 = $[22];
  }
  let t13;
  if ($[23] !== (data == null ? void 0 : data.Status) || $[24] !== handleUpdatePassword || $[25] !== isCodeValid || $[26] !== isRequestCompleted || $[27] !== isSuccess || $[28] !== passwordRegexp || $[29] !== passwordStrengthOptions) {
    t13 = !isRequestCompleted ? /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: true, overlayProps: {
      blur: 10
    } }) : !isCodeValid ? /* @__PURE__ */ jsxRuntimeExports.jsx(InvalidLinkContent, {}) : isCodeValid && isSuccess ? (data == null ? void 0 : data.Status) === IUpdatePasswordWithCodeEnum.Changed ? /* @__PURE__ */ jsxRuntimeExports.jsx(SuccessResetContent, {}) : null : /* @__PURE__ */ jsxRuntimeExports.jsx(ResetPasswordForm, { passwordRegexp, handleUpdatePassword, passwordStrengthOptions });
    $[23] = data == null ? void 0 : data.Status;
    $[24] = handleUpdatePassword;
    $[25] = isCodeValid;
    $[26] = isRequestCompleted;
    $[27] = isSuccess;
    $[28] = passwordRegexp;
    $[29] = passwordStrengthOptions;
    $[30] = t13;
  } else {
    t13 = $[30];
  }
  let t14;
  if ($[31] !== classes.controls || $[32] !== t13) {
    t14 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.controls, children: t13 });
    $[31] = classes.controls;
    $[32] = t13;
    $[33] = t14;
  } else {
    t14 = $[33];
  }
  let t15;
  if ($[34] !== t12 || $[35] !== t14) {
    t15 = /* @__PURE__ */ jsxRuntimeExports.jsx(LoginWraper, { title: t12, children: t14 });
    $[34] = t12;
    $[35] = t14;
    $[36] = t15;
  } else {
    t15 = $[36];
  }
  return t15;
};
function _temp(state) {
  return state.userInfo;
}
export {
  ResetPassword
};
//# sourceMappingURL=index-7NP-GXLT.js.map
