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