import { jsx as e, jsxs as r } from "react/jsx-runtime";
import { yupResolver as j } from "@hookform/resolvers/yup";
import c from "prop-types";
import { Modal as q, Paper as W, Card as B, CardHeader as E, Typography as m, CardContent as F, Button as g } from "@mui/material";
import s from "@mui/material/Grid2";
import * as t from "yup";
import { useRef as y, useEffect as H } from "react";
import { useForm as O } from "react-hook-form";
import { P as b, a as R } from "./ManageUser-Dr3ptCYp-CzLX1rCi.js";
import _ from "@/hooks/useAESEncryption.js";
import { F as $ } from "./UserListSkeleton-cglK2SaU-B_Tj5gUp.js";
import { showToast as C } from "@/common/toaster/toaster.jsx";
import k from "@/hooks/useTrackEvent";
import { useTranslateProvider as A } from "@/common/languageTranslator/TranslationContext";
const G = t.object().shape({
  password: t.string().required("Password is required").matches(
    /^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&#]).{8,}$/,
    "Password must be at least 8 characters long with one uppercase letter and one special character"
  ),
  confirmPassword: t.string().required("Confirm Password is required").oneOf([t.ref("password"), null], "Passwords do not match")
}), M = ({ open: P, onClose: n, userId: x, ...v }) => {
  const {
    control: p,
    formState: { errors: h },
    handleSubmit: N,
    watch: z
  } = O({ resolver: j(G), mode: "onChange" }), d = y(null), u = y(!1), S = k(), T = _(), { translate: a } = A(), I = async ({ password: o }) => {
    var w, f;
    try {
      const i = await T.encrypt(o);
      try {
        await $({
          oldPassword: i,
          userId: x
        }), n(), C({
          message: "Password Changed Successfully",
          type: "success"
        }), u.current = !0;
      } catch (l) {
        console.error(l), C({
          message: ((f = (w = l == null ? void 0 : l.response) == null ? void 0 : w.data) == null ? void 0 : f.message) || "Something Went Wrong",
          type: "error"
        });
      }
    } catch (i) {
      console.error(i);
    }
  };
  return H(() => {
    let o = d.current;
    return () => {
      o && S("conversion_event", {
        form: o,
        form_name: "Change Password",
        submit: u.current
      });
    };
  }, [d.current]), /* @__PURE__ */ e(q, { open: P, onClose: n, ...v, children: /* @__PURE__ */ e(W, { className: "absolute mt-[12rem] top-[10%] left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-[90%] md:w-[60%]", children: /* @__PURE__ */ r(
    B,
    {
      className: "m-0",
      elevation: 5,
      component: "form",
      ref: d,
      onSubmit: N(I),
      noValidate: !0,
      children: [
        /* @__PURE__ */ e(
          E,
          {
            className: "",
            subheader: /* @__PURE__ */ r(m, { className: "subheader-style text-customBlue text-lg", children: [
              a("Change Password"),
              " "
            ] }),
            "data-testid": "changepasswordmodal-cardheader-subheader"
          }
        ),
        /* @__PURE__ */ r(F, { children: [
          /* @__PURE__ */ r(s, { container: !0, spacing: 2, alignItems: "space-between", children: [
            /* @__PURE__ */ e(s, { size: { xs: 10 }, children: /* @__PURE__ */ r(m, { variant: "body1", className: "flex items-center", children: [
              a("New Password"),
              " ",
              /* @__PURE__ */ e("span", { className: "text-red-500 ml-1", children: "*" })
            ] }) }),
            /* @__PURE__ */ e(
              s,
              {
                size: { xs: 12 },
                "data-testid": "changepasswordmodal-grid-newpasswordfield",
                children: /* @__PURE__ */ e(
                  b,
                  {
                    control: p,
                    name: "password",
                    errors: h
                  }
                )
              }
            ),
            /* @__PURE__ */ e(
              s,
              {
                size: { xs: 12 },
                "data-testid": "changepasswordmodal-grid-passwordstrengthbar",
                children: /* @__PURE__ */ e(R, { password: z("password") })
              }
            ),
            /* @__PURE__ */ e(
              s,
              {
                size: { xs: 12 },
                "data-testid": "changepasswordmodal-grid-confirmpasswordlabel",
                children: /* @__PURE__ */ r(m, { variant: "body1", className: "flex items-center", children: [
                  a("Confirm Password"),
                  " ",
                  /* @__PURE__ */ e("span", { className: "text-red-500 ml-1", children: "*" })
                ] })
              }
            ),
            /* @__PURE__ */ e(
              s,
              {
                size: { xs: 12 },
                "data-testid": "changepaConfirm Passwordsswordmodal-grid-confirmpasswordfield",
                children: /* @__PURE__ */ e(
                  b,
                  {
                    control: p,
                    name: "confirmPassword",
                    errors: h
                  }
                )
              }
            )
          ] }),
          /* @__PURE__ */ r(s, { container: !0, wrap: "nowrap", gap: 1, justifyContent: "flex-end", children: [
            /* @__PURE__ */ e(
              g,
              {
                type: "submit",
                variant: "contained",
                size: "small",
                color: "primary",
                className: "mt-[1rem]",
                "data-testid": "changepasswordmodal-button-submit",
                children: a("Change Password")
              }
            ),
            /* @__PURE__ */ e(
              g,
              {
                variant: "contained",
                onClick: n,
                className: "mt-[1rem]",
                color: "secondary",
                size: "small",
                "data-testid": "changepasswordmodal-button-cancel",
                children: a("Cancel")
              }
            )
          ] })
        ] })
      ]
    }
  ) }) });
};
M.propTypes = {
  open: c.any,
  onClose: c.any,
  userId: c.any
};
export {
  M as default
};
//# sourceMappingURL=ChangePasswordModalCore-DIe4rzW--Dz71RCI6.js.map
