import { jsx as e, jsxs as r } from "react/jsx-runtime";
import { yupResolver as j } from "@hookform/resolvers/yup";
import m from "prop-types";
import { Modal as q, Paper as R, Card as k, CardHeader as A, Typography as l, CardContent as B, Button as g } from "@mui/material";
import s from "@mui/material/Grid2";
import * as n from "yup";
import { useRef as y, useEffect as F } from "react";
import { useForm as I } from "react-hook-form";
import { P, a as M } from "./ManageUser-Dr3ptCYp.js";
import W from "@/hooks/useAESEncryption.js";
import { o as _ } from "./UserListSkeleton-cglK2SaU.js";
import { showToast as b } from "@/common/toaster/toaster.jsx";
import $ from "@/hooks/useTrackEvent";
import { useTranslateProvider as G } from "@/common/languageTranslator/TranslationContext";
const H = n.object().shape({
  password: n.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: n.string().required("Confirm Password is required").oneOf([n.ref("password"), null], "Passwords do not match")
}), O = ({ open: x, onClose: d, userId: C, ...v }) => {
  const {
    control: p,
    formState: { errors: h },
    handleSubmit: N,
    watch: S
  } = I({ resolver: j(H), mode: "onChange" }), i = y(null), w = y(!1), z = $(), E = W(), { translate: a } = G(), T = async ({ password: t }) => {
    var u, f;
    try {
      const c = await E.encrypt(t);
      try {
        await _({
          oldPassword: c,
          userId: C
        }), d(), b({
          message: "Password Changed Successfully",
          type: "success"
        }), w.current = !0;
      } catch (o) {
        console.error(o), b({
          message: ((f = (u = o == null ? void 0 : o.response) == null ? void 0 : u.data) == null ? void 0 : f.message) || "Something Went Wrong",
          type: "error"
        });
      }
    } catch (c) {
      console.error(c);
    }
  };
  return F(() => {
    let t = i.current;
    return () => {
      t && z("conversion_event", {
        form: t,
        form_name: "Change Password",
        submit: w.current
      });
    };
  }, [i.current]), /* @__PURE__ */ e(q, { open: x, onClose: d, ...v, children: /* @__PURE__ */ e(R, { 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(
    k,
    {
      className: "m-0",
      elevation: 5,
      component: "form",
      ref: i,
      onSubmit: N(T),
      noValidate: !0,
      children: [
        /* @__PURE__ */ e(
          A,
          {
            className: "",
            subheader: /* @__PURE__ */ r(l, { className: "subheader-style text-customBlue text-lg", children: [
              a("Change Password"),
              " "
            ] }),
            "data-testid": "changepasswordmodal-cardheader-subheader"
          }
        ),
        /* @__PURE__ */ r(B, { children: [
          /* @__PURE__ */ r(s, { container: !0, spacing: 2, alignItems: "space-between", children: [
            /* @__PURE__ */ e(s, { size: { xs: 10 }, children: /* @__PURE__ */ r(l, { 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(
                  P,
                  {
                    control: p,
                    name: "password",
                    errors: h
                  }
                )
              }
            ),
            /* @__PURE__ */ e(
              s,
              {
                size: { xs: 12 },
                "data-testid": "changepasswordmodal-grid-passwordstrengthbar",
                children: /* @__PURE__ */ e(M, { password: S("password") })
              }
            ),
            /* @__PURE__ */ e(
              s,
              {
                size: { xs: 12 },
                "data-testid": "changepasswordmodal-grid-confirmpasswordlabel",
                children: /* @__PURE__ */ r(l, { 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(
                  P,
                  {
                    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: d,
                className: "mt-[1rem]",
                color: "secondary",
                size: "small",
                "data-testid": "changepasswordmodal-button-cancel",
                children: a("Cancel")
              }
            )
          ] })
        ] })
      ]
    }
  ) }) });
};
O.propTypes = {
  open: m.any,
  onClose: m.any,
  userId: m.any
};
export {
  O as default
};
//# sourceMappingURL=ChangePasswordModalCore-DIe4rzW-.js.map
