import { jsx as a, jsxs as o } from "react/jsx-runtime";
import { useRef as de, useState as r, useEffect as _ } from "react";
import n from "prop-types";
import { useForm as pe } from "react-hook-form";
import { yupResolver as ce } from "@hookform/resolvers/yup";
import t from "@mui/material/Grid2";
import * as b from "yup";
import { Modal as me, Typography as ue, IconButton as fe, Button as v, FormControl as z, Box as s } from "@mui/material";
import K from "@/views/custom-components/CustomTextField";
import { showToast as p } from "@/common/toaster/toaster";
import { g as he, A as ye, u as W, c as ge } from "./ManageTranslation-BTxvSYKb-CdO4Fk7f.js";
import be from "@/views/custom-components/CustomConfirmationModal";
import ve from "@/hooks/useTrackEvent";
import { useTranslateProvider as xe } from "@/common/languageTranslator/TranslationContext";
import { faXmark as Ae, faPen as we, faBars as Ce } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as x } from "@fortawesome/react-fontawesome";
import ke from "@/views/custom-components/CustomizedTable/CustomizedTableV2";
const Ne = ({
  open: D,
  handleClose: F,
  languages: A,
  refresh: w,
  refetch: C,
  selectedApp: P,
  setSelectedApp: R
}) => {
  const k = de(null), h = ve(), [X, q] = r([]), [c, H] = r([]), [N, m] = r(!1), [O, S] = r(!1), { translate: i } = xe(), [$, y] = r(!1), [G, L] = r(""), [U, Y] = r(""), [d, J] = r({}), [V, I] = r(!1), Q = b.object().shape({
    appValue: b.string().required("Select App Value"),
    appKey: b.string().required("Enter App key")
  }), {
    control: j,
    handleSubmit: Z,
    reset: T,
    setValue: u,
    formState: { errors: M }
  } = pe({
    defaultValues: {},
    resolver: ce(Q),
    mode: "onChange"
  }), ee = [
    { field: "Sr. No.", label: "Sr.No", align: "left" },
    { field: "appValue", label: "App", align: "left" },
    { field: "stateValue", label: "State", align: "left" },
    { field: "Actions", label: "Actions" }
  ], ae = (e) => {
    u("appKey", e == null ? void 0 : e.appKey), u("appValue", e == null ? void 0 : e.appValue), u("id", e == null ? void 0 : e.id), u("isActive", e == null ? void 0 : e.isActive), S(!0), m(!0);
  }, te = (e) => {
    L(
      (e == null ? void 0 : e.stateValue) === "Active" ? "Mark as Inactive" : "Mark as Active"
    ), Y(
      (e == null ? void 0 : e.stateValue) === "Active" ? "Are you sure you want to change the state from active to inactive?" : "Are you sure you want to change the state from inactive to active?"
    ), J(e);
  }, le = () => {
    y((e) => !e);
  }, oe = async () => {
    try {
      d.isActive = !d.isActive, await W(d), g(), P === (d == null ? void 0 : d.id) && R(null), await w(), C(), y((e) => !e);
    } catch (e) {
      console.error(e), p({ message: "Error in Toggle State", type: "error" });
    }
  }, ie = [
    { label: i(G), onClick: le }
  ], re = [
    {
      label: "",
      icon: /* @__PURE__ */ a(x, { icon: we }),
      onClick: ae,
      dataTestId: "app-modal-edit-icon"
    },
    {
      label: "",
      icon: /* @__PURE__ */ a(x, { icon: Ce }),
      menuClick: te,
      menuItems: ie,
      dataTestId: "app-modal-menu-icon"
    }
  ], ne = () => {
    T(), S(!1), m(!0);
  };
  _(() => {
    if (c) {
      const e = [];
      c == null || c.forEach((l) => {
        let f = { ...l };
        l != null && l.isActive ? f.stateValue = "Active" : f.stateValue = "Inactive", A == null || A.forEach((E) => {
          l.language === E.id && (f.languagevalue = E.languageValue);
        }), e.push(f);
      }), q(e);
    }
  }, [c]), _(() => {
    g();
  }, []);
  const g = async () => {
    I((e) => !e);
    try {
      const e = await he();
      H(e == null ? void 0 : e.data);
    } catch (e) {
      console.error(e), p({ message: "Error in loading apps", type: "error" });
    } finally {
      I((e) => !e);
    }
  }, B = () => {
    F();
  }, se = async (e) => {
    try {
      h("button_click", {
        button_name: "add-app-add-button",
        button_type: "submit",
        button_id: "add-app-submit-button"
      });
      let l = { ...e };
      O ? (await W(l), p({ message: "App Updated Succesfully", type: "success" })) : (l.isActive = !0, await ge(l), p({ message: "App added Succesfully", type: "success" })), h("conversion_event", {
        form: k.current,
        form_name: "Add app Configuration",
        submit: !0
      }), g(), await w(), C(), m(!1);
    } catch (l) {
      console.error(l), p({ message: "Error in Adding App", type: "error" });
    }
  };
  return /* @__PURE__ */ a(
    me,
    {
      "data-testid": "appModal-modal",
      open: D,
      onClose: () => {
        B(), T();
      },
      className: "translation-modal",
      children: /* @__PURE__ */ o(
        t,
        {
          container: !0,
          sx: {
            display: "flex",
            flexDirection: "column",
            position: "absolute",
            top: 20,
            left: "50%",
            transform: "translateX(-50%)",
            width: "95%",
            maxW: "100%",
            maxWidth: { sm: 600, md: 700, lg: 800 },
            bgcolor: "background.paper",
            boxShadow: 24,
            border: 1,
            borderColor: "divider",
            borderRadius: 0.5
          },
          children: [
            /* @__PURE__ */ a(
              be,
              {
                description: U,
                title: "CONFIRM ACTION",
                handleClick: oe,
                open: $,
                handleClose: () => {
                  y((e) => !e);
                },
                "data-testid": "app-modal-customconfirmation"
              }
            ),
            /* @__PURE__ */ o(
              t,
              {
                sx: {
                  p: 1.25,
                  px: 3,
                  display: "flex",
                  justifyContent: "space-between",
                  alignItems: "center",
                  borderBottom: 1,
                  borderColor: "divider",
                  flexWrap: "wrap",
                  gap: 1
                },
                "data-testid": "appModal-header",
                children: [
                  /* @__PURE__ */ a(
                    ue,
                    {
                      variant: "body1",
                      className: "text-customBlue",
                      sx: { fontWeight: "medium", fontSize: "1.125rem" },
                      children: i("Manage Apps")
                    }
                  ),
                  /* @__PURE__ */ a(
                    fe,
                    {
                      size: "small",
                      onClick: () => {
                        B();
                      },
                      "data-testid": "appModal-close-button",
                      children: /* @__PURE__ */ a(x, { icon: Ae })
                    }
                  )
                ]
              }
            ),
            /* @__PURE__ */ a(
              t,
              {
                item: !0,
                sx: {
                  overflowY: "auto",
                  maxHeight: "80vh",
                  width: "100%",
                  overflowX: "auto"
                },
                children: /* @__PURE__ */ a(
                  t,
                  {
                    sx: {
                      p: 1.25,
                      px: 3,
                      display: "flex",
                      justifyContent: "space-between",
                      alignItems: "center",
                      borderBottom: 1,
                      borderColor: "divider",
                      flexWrap: "wrap",
                      gap: 1
                    },
                    children: /* @__PURE__ */ o(t, { item: !0, className: "w-full", children: [
                      !N && /* @__PURE__ */ a(t, { container: !0, justifyContent: "flex-end", className: "pb-[10px]", children: /* @__PURE__ */ a(ye, { isLoading: V, children: /* @__PURE__ */ a(t, { item: !0, children: /* @__PURE__ */ o(
                        v,
                        {
                          size: "small",
                          variant: "contained",
                          "data-testid": "add-app-button",
                          color: "primary",
                          onClick: ne,
                          children: [
                            i("Add"),
                            " ",
                            i("App")
                          ]
                        }
                      ) }) }) }),
                      N && /* @__PURE__ */ a(t, { container: !0, justifyContent: "flex-start", sx: { pb: 1.25 }, children: /* @__PURE__ */ o("form", { onSubmit: Z(se), className: "w-full", children: [
                        /* @__PURE__ */ a(t, { item: !0, className: "pb-[5px]", children: /* @__PURE__ */ a(
                          z,
                          {
                            variant: "outlined",
                            className: "w-full",
                            "data-testid": "app-value-field",
                            children: /* @__PURE__ */ o(
                              s,
                              {
                                sx: {
                                  display: "flex",
                                  alignItems: "center",
                                  gap: 2,
                                  mb: 1,
                                  width: "100%"
                                },
                                children: [
                                  /* @__PURE__ */ o(s, { sx: { width: "100%" }, children: [
                                    i("App"),
                                    /* @__PURE__ */ a(
                                      s,
                                      {
                                        component: "span",
                                        sx: { color: "error.main", ml: 0.5 },
                                        children: "*"
                                      }
                                    )
                                  ] }),
                                  /* @__PURE__ */ a(
                                    K,
                                    {
                                      control: j,
                                      id: "appValue",
                                      name: "appValue",
                                      errors: M,
                                      label: "",
                                      size: "small",
                                      className: "w-full",
                                      "data-testid": "app-value-input"
                                    }
                                  )
                                ]
                              }
                            )
                          }
                        ) }),
                        /* @__PURE__ */ a(t, { item: !0, className: "pb-[5px]", children: /* @__PURE__ */ a(
                          z,
                          {
                            variant: "outlined",
                            className: "w-full",
                            "data-testid": "app-key-field",
                            children: /* @__PURE__ */ o(
                              s,
                              {
                                sx: {
                                  display: "flex",
                                  alignItems: "center",
                                  gap: 2,
                                  mb: 1,
                                  width: "100%"
                                },
                                children: [
                                  /* @__PURE__ */ o(s, { sx: { width: "100%" }, children: [
                                    i("App"),
                                    " ",
                                    i("Key"),
                                    /* @__PURE__ */ a(
                                      s,
                                      {
                                        component: "span",
                                        sx: { color: "error.main", ml: 0.5 },
                                        children: "*"
                                      }
                                    )
                                  ] }),
                                  /* @__PURE__ */ a(
                                    K,
                                    {
                                      control: j,
                                      id: "appKey",
                                      name: "appKey",
                                      errors: M,
                                      label: "",
                                      size: "small",
                                      className: "w-full",
                                      "data-testid": "app-key-input"
                                    }
                                  )
                                ]
                              }
                            )
                          }
                        ) }),
                        /* @__PURE__ */ a(
                          t,
                          {
                            item: !0,
                            sx: {
                              pb: 0.625,
                              display: "flex",
                              justifyContent: "flex-end",
                              width: "100%"
                            },
                            children: /* @__PURE__ */ o(
                              s,
                              {
                                sx: {
                                  display: "flex",
                                  flexDirection: { xs: "column", sm: "row" },
                                  gap: 1,
                                  width: { xs: "100%", sm: "auto" },
                                  justifyContent: "flex-end"
                                },
                                children: [
                                  /* @__PURE__ */ a(
                                    v,
                                    {
                                      size: "small",
                                      variant: "contained",
                                      "data-testid": "add-app-cancel",
                                      color: "secondary",
                                      onClick: () => {
                                        m(!1), h("conversion_event", {
                                          form: k.current,
                                          form_name: "Add App Configuration",
                                          submit: !1
                                        });
                                      },
                                      children: i("Cancel")
                                    }
                                  ),
                                  /* @__PURE__ */ a(
                                    v,
                                    {
                                      type: "submit",
                                      size: "small",
                                      variant: "contained",
                                      "data-testid": "add-app-save",
                                      color: "primary",
                                      children: i("Save")
                                    }
                                  )
                                ]
                              }
                            )
                          }
                        )
                      ] }) }),
                      /* @__PURE__ */ a(t, { className: "w-full block", children: /* @__PURE__ */ a(
                        ke,
                        {
                          columns: ee,
                          tableData: X,
                          actions: re,
                          paginationConfig: { pagination: !1 },
                          styleConfig: { tableStyle: { maxHeight: 120 } },
                          isLoading: V
                        }
                      ) })
                    ] })
                  }
                )
              }
            )
          ]
        }
      )
    }
  );
};
Ne.propTypes = {
  open: n.any,
  handleClose: n.any,
  languages: n.any,
  refresh: n.any,
  refetch: n.any,
  selectedApp: n.any,
  setSelectedApp: n.any
};
export {
  Ne as default
};
//# sourceMappingURL=AppModal-BgZyfrd--BO3wW4ma.js.map
