import { jsx as a, jsxs as r, Fragment as z } from "react/jsx-runtime";
import { useState as m, useEffect as se, Suspense as K, lazy as X } from "react";
import { Box as p, Card as _, Typography as d, Button as N, TextField as Q, Switch as oe, Chip as k } from "@mui/material";
import { alpha as w } from "@mui/material/styles";
import { useTranslateProvider as ne } from "@/common/languageTranslator/TranslationContext";
import { api as g } from "@/common/interceptors/AxiosInterceptor";
import { getRoleList as L } from "@/common/services/GlobalApis.js";
import { showToast as h } from "@/common/toaster/toaster";
import { FontAwesomeIcon as de } from "@fortawesome/react-fontawesome";
import { faTrash as ce } from "@fortawesome/free-solid-svg-icons";
import me from "@/hooks/useTrackEvent";
import $ from "prop-types";
import { useSelector as Z } from "react-redux";
import { getUseSkeleton as ue } from "@/store/reducers/SkeletonReducer";
const j = () => g.get("/role"), fe = (o) => g.delete(`/menuconfig/usermenuitem/${o}`), pe = (o) => g.get(`/role/${o}`), Le = (o) => g.post("/role", { ...o }), he = (o) => g.put(`role/?is_active=${o.state === "INACTIVE" ? "true" : "false"}`, {
  ...o
}), $e = (o, c) => g.post(`/menuconfig/assignnewfeature?roleId=${c}`, { ...o }), q = ({ children: o, isLoading: c = !0 }) => !Z(ue) || !c ? o : /* @__PURE__ */ r("div", { className: "pt-6 relative h-[90vh]", children: [
  /* @__PURE__ */ r("div", { className: "flex flex-row  h-[80vh] ", children: [
    /* @__PURE__ */ r("div", { className: "flex flex-col border border-gray-300 w-[20%] p-2 mr-3 rounded-md", children: [
      " ",
      /* @__PURE__ */ r("div", { className: "flex justify-between", children: [
        /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-23 h-[40px] mr-3" }),
        /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-40 h-[40px]" })
      ] }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm mt-2 w-full h-[50px]" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm mt-2 w-full h-full" })
    ] }),
    /* @__PURE__ */ r("div", { className: "flex flex-col border border-gray-300 w-[80%] p-2 rounded-md", children: [
      /* @__PURE__ */ r("div", { className: "flex justify-between", children: [
        /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-24 h-[40px]" }),
        /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-21 h-[40px]" })
      ] }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm mt-2 w-full h-[50px]" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm mt-2 w-full h-full" })
    ] })
  ] }),
  /* @__PURE__ */ a("div", { className: "absolute bottom-20 right-[10px] ", children: /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-full w-[40px] h-[40px]" }) })
] });
q.propTypes = {
  children: $.any,
  loading: $.any,
  isLoading: $.any
};
const ge = X(() => import("./AddFeatures-CkTu7sOI-BGplSFp6.js")), ve = X(() => import("./ManageRole-CuXmWQyp-Cy15Mlax.js"));
function xe() {
  var o;
  const { translate: c } = ne(), [n, C] = m(""), [s, x] = m(n[0]), [I, G] = m(!1), [H, u] = m(!1), [V, R] = m(!1), [T, M] = m(0), [O, A] = m(""), [S, W] = m(""), B = me(), D = Z((e) => e.Authenticate.user.roleId), [E, J] = m([]), b = async (e = !1) => {
    var t;
    try {
      u(!0);
      const l = I ? j() : L();
      let i, f = [];
      if (D) {
        const [{ data: v }, Y] = await Promise.all([
          l,
          pe(D)
        ]);
        i = v, f = ((t = Y == null ? void 0 : Y.data) == null ? void 0 : t.roleIds) || [];
      } else {
        const { data: v } = await l;
        i = v;
      }
      J(f);
      const F = f.length > 0 ? i.filter((v) => f.includes(v.id)) : i;
      C(F), !e && F.length > 0 && x(F[0]);
    } catch (l) {
      console.error(l), h({ message: "Error in getting role data", type: "error" });
    } finally {
      u(!1);
    }
  };
  se(function() {
    b();
  }, []);
  const ee = async (e) => {
    let t = e.target.checked;
    G(e.target.checked);
    try {
      u((i) => !i);
      let l;
      t ? { data: l } = await j() : { data: l } = await L(), E && E.length > 0 && (l = l.filter(
        (i) => E.includes(i.id)
      )), C(l), x(l[0]), u((i) => !i), A("");
    } catch (l) {
      console.error(l), h({ message: "Error in getting role data", type: "error" }), u((i) => !i);
    }
  }, P = n ? n == null ? void 0 : n.filter(
    (e) => e.name.toLowerCase().includes(O.toLowerCase())
  ) : [], ae = (e) => {
    if (!e) return "";
    const t = new Date(Number(e)), l = String(t.getDate()).padStart(2, "0"), i = String(t.getMonth() + 1).padStart(2, "0"), f = t.getFullYear();
    return `${l}/${i}/${f}`;
  }, te = async () => {
    try {
      const e = n == null ? void 0 : n.find((l) => l.id === s.id);
      await he(e), h({
        message: `State succesfully changed from  ${(e == null ? void 0 : e.state) === "INACTIVE" ? "inactive" : "active"} to ${(e == null ? void 0 : e.state) === "INACTIVE" ? "active" : "inactive"}`,
        type: "success"
      });
      let t;
      u((l) => !l), I ? t = await j() : t = await L(), C(t.data), x(t.data[0]), u((l) => !l);
    } catch (e) {
      console.error(e), h({ message: "Error in handle active", type: "error" }), u((t) => !t);
    }
  }, y = n ? (o = n.find((e) => e.id === s.id)) == null ? void 0 : o.assignedFeatureList.filter(
    (e) => {
      var t;
      return (t = e.featureName) == null ? void 0 : t.toLowerCase().includes(S == null ? void 0 : S.toLowerCase());
    }
  ).sort((e, t) => {
    var l;
    return (l = e.featureName) == null ? void 0 : l.localeCompare(t.featureName);
  }) : [], le = async (e) => {
    try {
      await fe(e.userMenuItemId), await b(!0), W(""), h({ message: "Feature deleted successfully!", type: "success" });
    } catch (t) {
      h({ message: t, type: "error" });
    }
  }, U = (e) => {
    M(e), B("button_click", {
      button_name: `${e === 1 ? "add" : "edit"} role modal button`,
      button_type: "navigate",
      button_id: `roles-${e === 1 ? "add" : "edit"}-button`
    });
  }, re = () => {
    b(), A("");
  }, ie = async () => {
    try {
      R(!0), await B("button_click", {
        button_name: "add feature button",
        button_type: "navigate",
        button_id: "roles-addfeatures-button-add"
      });
    } catch (e) {
      console.error(e);
    }
  };
  return /* @__PURE__ */ a(q, { isLoading: H, children: /* @__PURE__ */ r(p, { className: "flex flex-col gap-4 p-4 min-[900px]:flex-row min-[900px]:h-[calc(100vh-80px)]", children: [
    /* @__PURE__ */ r(
      _,
      {
        variant: "outlined",
        className: "w-full min-[900px]:w-[300px] flex flex-col p-4",
        children: [
          /* @__PURE__ */ r("div", { className: "flex justify-between items-center mb-3", children: [
            /* @__PURE__ */ a(
              d,
              {
                variant: "h6",
                fontWeight: 600,
                className: "text-customBlue",
                children: "User Roles"
              }
            ),
            /* @__PURE__ */ a(
              N,
              {
                size: "small",
                variant: "contained",
                color: "primary",
                onClick: () => U(1),
                "data-testid": "managerole-add-button",
                children: c("+ Add Role")
              }
            )
          ] }),
          /* @__PURE__ */ a(
            Q,
            {
              size: "small",
              variant: "outlined",
              placeholder: "Search roles...",
              fullWidth: !0,
              value: O,
              onChange: (e) => A(e.target.value),
              "data-testid": "listofroles-search-input"
            }
          ),
          /* @__PURE__ */ r("div", { className: "flex items-center gap-2 mt-3", children: [
            /* @__PURE__ */ a(
              oe,
              {
                size: "small",
                checked: I,
                onChange: ee
              }
            ),
            /* @__PURE__ */ a(d, { variant: "body2", children: "Include Inactive" })
          ] }),
          /* @__PURE__ */ a("div", { className: "flex-1 overflow-y-auto mt-4", children: P.length > 0 && /* @__PURE__ */ a(z, { children: P.map((e) => /* @__PURE__ */ r(
            _,
            {
              variant: "outlined",
              onClick: () => x(e),
              className: `p-3 cursor-pointer transition
                  ${s.id === e.id ? "border-b-2" : "border-b-2 border-0"}`,
              sx: s.id === e.id ? {
                bgcolor: (t) => w(t.palette.primary.main, 0.08)
              } : {},
              children: [
                /* @__PURE__ */ r(d, { fontWeight: 600, children: [
                  e.name,
                  "     ",
                  (e == null ? void 0 : e.state) === "INACTIVE" && /* @__PURE__ */ a(k, { label: "INACTIVE", color: "disabled", size: "small" })
                ] }),
                /* @__PURE__ */ r(d, { variant: "body2", color: "text.secondary", children: [
                  e.assignedFeatureList.length,
                  " Features"
                ] })
              ]
            },
            e.id
          )) }) })
        ]
      }
    ),
    /* @__PURE__ */ r(
      _,
      {
        variant: "outlined",
        className: "w-full min-[900px]:flex-1 flex flex-col p-5",
        children: [
          /* @__PURE__ */ r("div", { className: "flex flex-col sm:flex-row justify-between items-start border-b pb-4 gap-4", children: [
            /* @__PURE__ */ r("div", { className: "flex flex-col gap-1 w-full sm:w-auto", children: [
              /* @__PURE__ */ r("div", { className: "flex items-center gap-2", children: [
                /* @__PURE__ */ a(
                  d,
                  {
                    variant: "h5",
                    fontWeight: 600,
                    className: "text-customBlue",
                    children: (s == null ? void 0 : s.name) || "Select Role"
                  }
                ),
                (s == null ? void 0 : s.state) === "ACTIVE" && /* @__PURE__ */ a(k, { label: "ACTIVE", color: "success", size: "small" }),
                (s == null ? void 0 : s.state) === "INACTIVE" && /* @__PURE__ */ a(k, { label: "INACTIVE", color: "disabled", size: "small" })
              ] }),
              /* @__PURE__ */ r(
                d,
                {
                  variant: "body2",
                  color: "text.secondary",
                  className: "mt-1",
                  children: [
                    "Created on ",
                    ae(s == null ? void 0 : s.createdOn)
                  ]
                }
              )
            ] }),
            /* @__PURE__ */ r("div", { className: "flex gap-2", children: [
              /* @__PURE__ */ a(
                N,
                {
                  variant: "outlined",
                  size: "small",
                  onClick: () => te(),
                  children: (s == null ? void 0 : s.state) === "ACTIVE" ? "Deactivate" : "Activate"
                }
              ),
              /* @__PURE__ */ a(
                N,
                {
                  variant: "contained",
                  size: "small",
                  onClick: () => U(2),
                  children: "✏️ Edit Role"
                }
              )
            ] })
          ] }),
          /* @__PURE__ */ r("div", { className: "mt-5 flex flex-col flex-1 overflow-hidden", children: [
            /* @__PURE__ */ r("div", { className: "flex flex-col sm:flex-row justify-between items-start sm:items-center mb-4 gap-4", children: [
              /* @__PURE__ */ r("div", { className: "flex flex-row items-center gap-2 w-full sm:w-auto", children: [
                /* @__PURE__ */ a(d, { fontWeight: 600, children: c("Assigned Features") }),
                /* @__PURE__ */ r(d, { variant: "body2", color: "text.secondary", children: [
                  "(",
                  y.length,
                  " ",
                  c("features active for this role"),
                  ")"
                ] })
              ] }),
              /* @__PURE__ */ a(
                Q,
                {
                  size: "small",
                  sx: { width: { xs: "100%", sm: "60%" } },
                  placeholder: "Search Feature Name",
                  variant: "outlined",
                  value: S,
                  onChange: (e) => W(e.target.value),
                  "data-testid": "assignedfeatures-search-input"
                }
              )
            ] }),
            /* @__PURE__ */ r(p, { className: "overflow-x-auto w-full", children: [
              /* @__PURE__ */ r(
                p,
                {
                  className: "grid grid-cols-[minmax(340px,1.5fr)_120px] px-4 py-2 text-[15px] font-semibold border-b min-w-[500px]",
                  sx: {
                    color: "text.secondary",
                    bgcolor: (e) => w(e.palette.action.hover, 0.04)
                  },
                  children: [
                    /* @__PURE__ */ a("div", { children: c("FEATURE NAME") }),
                    /* @__PURE__ */ a("div", { className: "text-center", children: c("ACTIONS") })
                  ]
                }
              ),
              /* @__PURE__ */ r(
                p,
                {
                  className: "h-[calc(100vh-440px)] overflow-y-auto min-w-[500px]",
                  sx: {
                    bgcolor: "background.paper"
                  },
                  children: [
                    y.length > 0 && /* @__PURE__ */ a(z, { children: y.map((e, t) => /* @__PURE__ */ r(
                      p,
                      {
                        className: "grid grid-cols-[minmax(340px,1.5fr)_120px] px-4 py-3 items-center border-b last:border-b-0 transition",
                        sx: {
                          "&:hover": {
                            bgcolor: (l) => w(l.palette.action.hover, 0.04)
                          }
                        },
                        children: [
                          /* @__PURE__ */ a("div", { className: "flex flex-col", children: /* @__PURE__ */ a(d, { sx: { fontSize: "13px", fontWeight: 600 }, children: e.featureName }) }),
                          /* @__PURE__ */ a("div", { className: "flex justify-center", children: /* @__PURE__ */ a(
                            de,
                            {
                              icon: ce,
                              onClick: () => {
                                le(e);
                              },
                              color: "error",
                              className: "text-red-400 cursor-pointer",
                              "data-testid": `assignedfeatures-delete-icon-${t}`
                            }
                          ) })
                        ]
                      },
                      e.id
                    )) }),
                    y.length === 0 && /* @__PURE__ */ a(z, { children: /* @__PURE__ */ a(
                      d,
                      {
                        className: "p-4",
                        sx: { fontSize: "13px", fontStyle: "italic" },
                        children: "No Features assigned yet!"
                      }
                    ) })
                  ]
                }
              )
            ] })
          ] }),
          /* @__PURE__ */ a(
            p,
            {
              className: "border-t p-4",
              sx: {
                bgcolor: (e) => w(e.palette.primary.main, 0.06)
              },
              children: /* @__PURE__ */ a(N, { variant: "outlined", size: "small", onClick: ie, children: "+ Add More Features" })
            }
          )
        ]
      }
    ),
    /* @__PURE__ */ a(K, { children: /* @__PURE__ */ a(
      ve,
      {
        isUpdate: T !== 1,
        selectedId: T === 2 ? s.id : null,
        "data-testid": "roles-managerole-add-button",
        open: T,
        setOpen: M,
        isRoleListModified: re
      }
    ) }),
    /* @__PURE__ */ a(K, { fallback: /* @__PURE__ */ a("div", { className: "animate-pulse w-80 h-20 " }), children: V === !0 && /* @__PURE__ */ a(
      ge,
      {
        open: V,
        setOpenModal: R,
        selectedId: s.id,
        setData: () => b(!0),
        assignedFeatureList: n ? (() => {
          var e, t, l;
          return (e = n == null ? void 0 : n.find((i) => (i == null ? void 0 : i.id) === s.id)) != null && e.assignedFeatureList ? (t = n == null ? void 0 : n.find((i) => (i == null ? void 0 : i.id) === s.id)) == null ? void 0 : t.assignedFeatureList : (l = n[0]) == null ? void 0 : l.assignedFeatureList;
        })() : [],
        "data-testid": "roles-addfeatures-component"
      }
    ) })
  ] }) });
}
const je = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  default: xe
}, Symbol.toStringTag, { value: "Module" }));
export {
  je as R,
  Le as a,
  pe as g,
  $e as p
};
//# sourceMappingURL=RoleManagement-C0feJCuq-DIGwGqOD.js.map
