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