import { jsx as t, jsxs as s } from "react/jsx-runtime";
import { p as D } from "./RoleManagement-C0feJCuq-DIGwGqOD.js";
import { yupResolver as M } from "@hookform/resolvers/yup";
import { Modal as O, Box as y, Typography as f, List as R, ListItem as $, Stack as V, Button as v } from "@mui/material";
import i from "@mui/material/Grid2";
import n from "prop-types";
import { useRef as g, useState as z, useEffect as Y } from "react";
import { useForm as q } from "react-hook-form";
import * as x from "yup";
import { showToast as p } from "@/common/toaster/toaster";
import { useTranslateProvider as H } from "@/common/languageTranslator/TranslationContext";
import J from "@/hooks/useTrackEvent";
import K from "@/views/custom-components/CustomAutoComplete";
import { getFeaturesV2 as Q } from "@/common/services/AuthenticateService";
function U({
  assignedFeatureList: c,
  selectedId: w,
  setData: _,
  open: C,
  setOpenModal: F
}) {
  const I = g(null), h = g(!1), { translate: u } = H(), [N, j] = z([]), T = x.object().shape({
    values: x.array().min(1, "Please select at least one feature.")
  }), {
    formState: { errors: k },
    handleSubmit: B,
    reset: E,
    control: L,
    watch: S
  } = q({
    defaultValues: {
      values: []
    },
    resolver: M(T),
    mode: "onChange"
  }), b = S("values");
  function A(e) {
    const o = [];
    for (const a in e)
      e[a].forEach((r) => {
        r.subGroups ? r.subGroups.forEach((l) => o.push(l)) : o.push(r);
      });
    return o;
  }
  const d = J();
  function m() {
    E(), F(!1), d("conversion_event", {
      form: I.current,
      form_name: "Add Feature",
      submit: h.current
    }), d("button_click", {
      button_name: "add feature cancel button",
      button_type: "cancel",
      button_id: "assignfeatures-button-cancel"
    });
  }
  const G = async (e) => {
    try {
      d("button_click", {
        button_name: "add feature save button",
        button_type: "submit",
        button_id: "assignfeatures-button-save"
      });
      const o = {
        featureId: e.values.map((a) => a.id).join(",")
      };
      await D(o, w), p({
        message: "Feature successfully assigned to selected role",
        type: "success"
      }), await _(), h.current = !0, m();
    } catch (o) {
      p({
        message: `Error : ${o == null ? void 0 : o.message}`,
        type: "error"
      });
    }
  }, P = async () => {
    try {
      const { data: e } = await Q(), o = A(e).sort(
        (a, r) => {
          var l;
          return (l = a.name) == null ? void 0 : l.localeCompare(r.name);
        }
      ).filter(
        (a) => !(c != null && c.some(
          (r) => r.featureId === a.id
        ))
      );
      j(o);
    } catch (e) {
      p({
        message: `Error : ${e == null ? void 0 : e.message}`,
        type: "error"
      });
    }
  };
  return Y(() => {
    P();
  }, [c]), /* @__PURE__ */ t(i, { children: /* @__PURE__ */ t(
    O,
    {
      open: C,
      onClose: m,
      "data-testid": "assignfeatures-modal",
      children: /* @__PURE__ */ t(
        i,
        {
          container: !0,
          className: "absolute top-[10%] left-[50%] transform -translate-x-1/2 w-[95%] sm:w-[37.5rem] shadow-2xl rounded-md border flex flex-col h-auto max-h-[80vh] overflow-y-auto",
          sx: { bgcolor: "background.paper" },
          children: /* @__PURE__ */ s(i, { className: "flex flex-col gap-2", children: [
            /* @__PURE__ */ t(
              y,
              {
                className: "p-2.5 px-5 flex w-full justify-start",
                sx: { borderBottom: 1, borderColor: "divider" },
                children: /* @__PURE__ */ t(
                  f,
                  {
                    className: "font-medium text-lg text-customBlue",
                    sx: { color: "text.primary" },
                    children: u("Assign New Feature")
                  }
                )
              }
            ),
            /* @__PURE__ */ s(
              y,
              {
                className: "p-2.5 max-h-100 overflow-auto",
                sx: { borderBottom: 1, borderColor: "divider" },
                children: [
                  /* @__PURE__ */ t(
                    K,
                    {
                      options: N,
                      getOptionLabel: (e) => e.name,
                      control: L,
                      name: "values",
                      errors: k,
                      dataTestId: "assignfeatures-autocomplete"
                    }
                  ),
                  b.length > 0 && /* @__PURE__ */ t(i, { className: "overflow-y-auto flex flex-col max-h-40 h-auto", children: /* @__PURE__ */ t(R, { children: b.filter((e) => e.description).map((e) => /* @__PURE__ */ t($, { children: /* @__PURE__ */ s(f, { children: [
                    /* @__PURE__ */ s("strong", { children: [
                      e == null ? void 0 : e.name,
                      ":"
                    ] }),
                    " ",
                    e == null ? void 0 : e.description
                  ] }) }, e == null ? void 0 : e.id)) }) })
                ]
              }
            ),
            /* @__PURE__ */ s(
              V,
              {
                direction: "row",
                spacing: 2,
                justifyContent: "flex-end",
                mt: 2,
                mb: 1,
                className: "px-4",
                children: [
                  /* @__PURE__ */ t(
                    v,
                    {
                      variant: "contained",
                      color: "secondary",
                      onClick: m,
                      "data-testid": "assignfeatures-button-cancel",
                      children: /* @__PURE__ */ t(
                        f,
                        {
                          variant: "caption",
                          "data-testid": "assignfeatures-typography-cancel",
                          children: u("Cancel")
                        }
                      )
                    }
                  ),
                  /* @__PURE__ */ t(
                    v,
                    {
                      size: "small",
                      variant: "contained",
                      color: "primary",
                      onClick: B(G),
                      "data-testid": "assignfeatures-button-save",
                      children: u("Add")
                    }
                  )
                ]
              }
            )
          ] })
        }
      )
    }
  ) });
}
U.propTypes = {
  assignedFeatureList: n.any,
  selectedId: n.any,
  open: n.any,
  setOpenModal: n.any,
  setData: n.any
};
export {
  U as default
};
//# sourceMappingURL=AddFeatures-CkTu7sOI-BGplSFp6.js.map
