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