import { jsx as t, jsxs as l } from "react/jsx-runtime";
import { useTranslateProvider as le } from "@/common/languageTranslator/TranslationContext";
import { execute as re } from "@/common/services/GlobalApis";
import { a as ie, L as B, b as se, d as ce } from "./ManageTranslation-D9IScGhk.js";
import { showToast as m } from "@/common/toaster/toaster";
import de from "@/hooks/useTrackEvent";
import ue from "@/views/custom-components/CustomConfirmationModal";
import me from "@/views/custom-components/CustomizedTable/CustomizedTableV2";
import { faXmark as ge, faBars as fe } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as _ } from "@fortawesome/react-fontawesome";
import { yupResolver as pe } from "@hookform/resolvers/yup";
import { Modal as he, Typography as ye, IconButton as be, Button as x, FormControl as ve, Box as g, Select as xe, MenuItem as M } from "@mui/material";
import n from "@mui/material/Grid2";
import d from "prop-types";
import { useRef as we, useState as r, useEffect as E } from "react";
import { useForm as Ce, Controller as Ae } from "react-hook-form";
import * as D from "yup";
const Te = ({ open: R, handleClose: V, refresh: w, refetch: C }) => {
  var N;
  const f = we(null), A = de(), [O, j] = r([]), [u, z] = r([]), [T, p] = r(!1), [c, F] = r([]), [q, h] = r(!1), [P, W] = r(""), [X, H] = r(""), { translate: i } = le(), [y, K] = r({}), [S, k] = r(!1), $ = D.object().shape({
    languageToBeAdded: D.string().required("Select Language")
  }), {
    control: G,
    handleSubmit: J,
    reset: L,
    formState: { errors: s }
  } = Ce({
    defaultValues: {},
    resolver: pe($),
    mode: "onChange"
  }), Y = [
    { field: "languageValue", label: "Language", align: "left" },
    { field: "orientation", label: "Orientation", align: "left" },
    { field: "stateValue", label: "State", align: "left" },
    { field: "Actions", label: "Actions" }
  ], Q = (e) => {
    W(
      (e == null ? void 0 : e.stateValue) === "Active" ? "Mark as Inactive" : "Mark as Active"
    ), H(
      (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?"
    ), K(e);
  }, U = () => {
    h((e) => !e);
  }, Z = async () => {
    try {
      y.active = !y.active, await se(y), b(), await w(), C(), h((e) => !e);
    } catch (e) {
      console.error(e), m({ message: "Error in Toggle State", type: "error" });
    }
  }, ee = [
    { label: i(P), onClick: U }
  ], te = [
    {
      label: "",
      icon: /* @__PURE__ */ t(_, { icon: fe }),
      menuClick: Q,
      menuItems: ee,
      condition: (e) => (e == null ? void 0 : e.languageKey) !== "en",
      dataTestId: "languages-modal-menu"
    }
  ], ae = async () => {
    var e;
    try {
      L();
      const o = await re({
        code: "labels_get_all_ibm_languages"
      });
      F((e = o == null ? void 0 : o.data) == null ? void 0 : e.result), p(!0);
    } catch (a) {
      console.error(a), m({ message: "Problem in laoding language list", type: "error" });
    }
  };
  E(() => {
    if (u) {
      const e = [];
      u == null || u.forEach((a) => {
        let o = { ...a };
        a != null && a.is_active ? o.stateValue = "Active" : o.stateValue = "Inactive", a != null && a.is_ltr ? o.orientation = "Left to Right" : o.orientation = "Right to Left", e.push(o);
      }), j(e);
    }
  }, [u]), E(() => {
    b();
  }, []);
  const b = async () => {
    k((e) => !e);
    try {
      const e = await ie();
      z(e == null ? void 0 : e.data);
    } catch (e) {
      console.error(e), m({ message: "Error in loading languages", type: "error" });
    } finally {
      k((e) => !e);
    }
  }, I = () => {
    V();
  }, oe = async (e) => {
    try {
      const a = c == null ? void 0 : c.find(
        (v) => (v == null ? void 0 : v.id) === parseInt(e == null ? void 0 : e.languageToBeAdded)
      );
      let o = JSON.parse(a == null ? void 0 : a.additional_detail).Orientation === "left_to_right", ne = {
        languageKey: a == null ? void 0 : a.code,
        languageValue: a == null ? void 0 : a.value,
        isLtr: o,
        active: !0
      };
      await ce(ne), m({ message: "Language Added successfully", type: "success" }), b(), A("conversion_event", {
        form: f.current,
        form_name: "Add app Configuration",
        submit: !0
      }), await w(), C(), p(!1);
    } catch (a) {
      console.error(a), m({ message: "Error in Adding Language", type: "error" });
    }
  };
  return /* @__PURE__ */ t(
    he,
    {
      "data-testid": "languagesModal-modal",
      open: R,
      onClose: () => {
        I(), L();
      },
      className: "translation-modal",
      children: /* @__PURE__ */ l(
        n,
        {
          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__ */ t(
              ue,
              {
                description: X,
                title: "CONFIRM ACTION",
                handleClick: Z,
                open: q,
                handleClose: () => {
                  h((e) => !e);
                },
                "data-testid": "languages-customconfirmation"
              }
            ),
            /* @__PURE__ */ l(
              n,
              {
                sx: {
                  p: 1.25,
                  display: "flex",
                  justifyContent: "space-between",
                  alignItems: "center",
                  borderBottom: 1,
                  borderColor: "divider",
                  gap: 1
                },
                className: "text-customBlue",
                "data-testid": "languagesModal-header",
                children: [
                  /* @__PURE__ */ t(ye, { sx: { fontWeight: "medium", fontSize: "1.125rem" }, children: i("Manage Language") }),
                  /* @__PURE__ */ t(
                    be,
                    {
                      size: "small",
                      onClick: () => {
                        I();
                      },
                      "data-testid": "languagesModal-close-button",
                      children: /* @__PURE__ */ t(_, { icon: ge })
                    }
                  )
                ]
              }
            ),
            /* @__PURE__ */ t(
              n,
              {
                item: !0,
                sx: {
                  overflowY: "auto",
                  maxHeight: "80vh",
                  width: "100%",
                  overflowX: "auto"
                },
                children: /* @__PURE__ */ t(
                  n,
                  {
                    container: !0,
                    className: "translational-modal-body px-2 sm:px-4 py-2 gap-y-2",
                    children: /* @__PURE__ */ l(n, { item: !0, className: "w-full", children: [
                      !T && /* @__PURE__ */ t(n, { container: !0, justifyContent: "flex-end", className: "pb-[10px]", children: /* @__PURE__ */ t(B, { isLoading: S, children: /* @__PURE__ */ t(n, { item: !0, children: /* @__PURE__ */ t(
                        x,
                        {
                          size: "small",
                          variant: "contained",
                          "data-testid": "language-add-button",
                          color: "primary",
                          onClick: ae,
                          children: `${i("Add")} ${i("Language")}`
                        }
                      ) }) }) }),
                      T && /* @__PURE__ */ t(
                        n,
                        {
                          container: !0,
                          justifyContent: "flex-start",
                          className: "pb-[10px] flex-wrap gap-2",
                          children: /* @__PURE__ */ t(B, { isLoading: S, children: /* @__PURE__ */ l(
                            "form",
                            {
                              onSubmit: J(oe),
                              className: "w-full",
                              ref: f,
                              children: [
                                /* @__PURE__ */ t(n, { item: !0, className: "pb-[5px]", children: /* @__PURE__ */ t(
                                  ve,
                                  {
                                    variant: "outlined",
                                    sx: { width: "100%" },
                                    "data-testid": "languagemodal-field",
                                    children: /* @__PURE__ */ l(
                                      g,
                                      {
                                        sx: {
                                          display: "flex",
                                          alignItems: "center",
                                          gap: 2,
                                          mb: 1,
                                          width: "100%"
                                        },
                                        children: [
                                          /* @__PURE__ */ l(g, { sx: { width: "100%" }, children: [
                                            i("Language"),
                                            /* @__PURE__ */ t(
                                              g,
                                              {
                                                component: "span",
                                                sx: { color: "error.main", ml: 0.5 },
                                                children: "*"
                                              }
                                            )
                                          ] }),
                                          /* @__PURE__ */ l(g, { sx: { width: "100%" }, children: [
                                            /* @__PURE__ */ t(
                                              Ae,
                                              {
                                                name: "languageToBeAdded",
                                                control: G,
                                                defaultValue: "",
                                                className: "w-full",
                                                render: ({ field: e }) => /* @__PURE__ */ l(
                                                  xe,
                                                  {
                                                    className: "w-full",
                                                    labelId: "event-type-label",
                                                    id: "reportgroup",
                                                    ...e,
                                                    error: s.languageToBeAdded,
                                                    size: "small",
                                                    "data-testid": "languagemodal-select",
                                                    children: [
                                                      /* @__PURE__ */ t(M, { value: null, children: "Select" }),
                                                      c == null ? void 0 : c.map((a, o) => /* @__PURE__ */ t(
                                                        M,
                                                        {
                                                          value: a.id,
                                                          children: a.value
                                                        },
                                                        o + 1
                                                      ))
                                                    ]
                                                  }
                                                )
                                              }
                                            ),
                                            (s == null ? void 0 : s.languageToBeAdded) && /* @__PURE__ */ t(
                                              "p",
                                              {
                                                className: "error-helper-language",
                                                "data-testid": "manageeventconfiguration-languageToBeAdded-error",
                                                children: (N = s == null ? void 0 : s.languageToBeAdded) == null ? void 0 : N.message
                                              }
                                            )
                                          ] })
                                        ]
                                      }
                                    )
                                  }
                                ) }),
                                /* @__PURE__ */ t(
                                  n,
                                  {
                                    item: !0,
                                    sx: {
                                      pb: 0.625,
                                      display: "flex",
                                      justifyContent: "flex-end",
                                      width: "100%"
                                    },
                                    children: /* @__PURE__ */ l(
                                      g,
                                      {
                                        sx: {
                                          display: "flex",
                                          flexDirection: { xs: "column", sm: "row" },
                                          gap: 1,
                                          width: { xs: "100%", sm: "auto" }
                                        },
                                        children: [
                                          /* @__PURE__ */ t(
                                            x,
                                            {
                                              size: "small",
                                              variant: "contained",
                                              "data-testid": "languages-modal-cancel-button",
                                              color: "secondary",
                                              onClick: () => {
                                                p(!1), A("conversion_event", {
                                                  form: f.current,
                                                  form_name: "Add Language Configuration",
                                                  submit: !1
                                                });
                                              },
                                              children: i("Cancel")
                                            }
                                          ),
                                          /* @__PURE__ */ t(
                                            x,
                                            {
                                              type: "submit",
                                              size: "small",
                                              variant: "contained",
                                              "data-testid": "language-modal-save-button",
                                              color: "primary",
                                              children: i("Save")
                                            }
                                          )
                                        ]
                                      }
                                    )
                                  }
                                )
                              ]
                            }
                          ) })
                        }
                      ),
                      /* @__PURE__ */ t(n, { className: "w-full block", children: /* @__PURE__ */ t(
                        me,
                        {
                          columns: Y,
                          tableData: O,
                          actions: te,
                          paginationConfig: { pagination: !1 },
                          styleConfig: { tableStyle: { maxHeight: 120 } }
                        }
                      ) })
                    ] })
                  }
                )
              }
            )
          ]
        }
      )
    }
  );
};
Te.propTypes = {
  open: d.any,
  handleClose: d.any,
  handleClick: d.any,
  type: d.any,
  refetch: d.any,
  refresh: d.any
};
export {
  Te as default
};
//# sourceMappingURL=LanguagesModal-CX3oS8rk.js.map
