import { jsxs as u, jsx as a } from "react/jsx-runtime";
import { useRef as ye, useState as r, useEffect as be, useCallback as fa, useMemo as q, Suspense as M, createElement as va, lazy as U } from "react";
import { api as m } from "@/common/interceptors/AxiosInterceptor";
import { execute as ya, executeQuery as j } from "@/common/services/GlobalApis";
import { showToast as v } from "@/common/toaster/toaster";
import c from "@mui/material/Grid2";
import { getAssignedFeatures as ba } from "@/utils/feature";
import { FormControl as H, Typography as C, Select as xa, MenuItem as wa, Autocomplete as ka, TextField as xe, Chip as Na, Box as we, Button as A, FormGroup as Ca, FormControlLabel as Aa, Checkbox as Sa } from "@mui/material";
import Ta from "@/hooks/usePagination";
import La from "@/views/custom-components/CustomConfirmationModal";
import { useTranslateProvider as _a } from "@/common/languageTranslator/TranslationContext";
import g from "prop-types";
import { useSelector as F } from "react-redux";
import { getUseSkeleton as E } from "@/store/reducers/SkeletonReducer";
import Ia from "@/views/custom-components/CustomizedTable/CustomizedTableV2";
import { faPen as za, faBars as Ma } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as ke } from "@fortawesome/react-fontawesome";
import './ManageTranslation-BTxvSYKb.css';const rl = (n) => m.post("/translation/languages", n), sl = (n) => m.put("/translation/languages", n), ja = () => m.get("/translation/languages"), ol = (n) => m.post("/translation/apps", n), dl = (n) => m.post("/translation/apps", n), Fa = () => m.get("/translation/apps"), cl = (n) => m.post("/translation/labels", n), ul = (n) => m.post("/translation/map", n), pl = (n) => m.post("/translation/mapUpdate", n), Ea = (n, o, h) => {
  const O = new URLSearchParams({
    key: n,
    isActive: o,
    app: h
  }).toString();
  return m.put(`/translation/toggleActive?${O}`, { key: n, isActive: o, app: h });
}, ml = (n, o) => m.get("/translation/translate", { params: { targetLangs: n, englishText: o } }), Ne = ({ children: n, isLoading: o }) => !F(E) || !o ? n : /* @__PURE__ */ u("div", { className: "flex flex-col mt-6 gap-2 w-full", children: [
  /* @__PURE__ */ u("div", { className: "flex w-full items-center justify-between space-x-2", children: [
    /* @__PURE__ */ u("div", { className: "flex space-x-2", children: [
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-40 h-10" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-40 h-10" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-60 h-10" })
    ] }),
    /* @__PURE__ */ u("div", { className: "flex space-x-2", children: [
      /* @__PURE__ */ a("div", { className: "bg-gray-300 rounded-sm animate-pulse w-40 h-10" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 rounded-sm animate-pulse w-20 h-10" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 rounded-sm animate-pulse w-20 h-10" }),
      /* @__PURE__ */ a("div", { className: "bg-gray-300 rounded-sm animate-pulse w-20 h-10" })
    ] })
  ] }),
  /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm w-full h-100" })
] });
Ne.propTypes = {
  children: g.any,
  isLoading: g.any
};
const Oa = ({ children: n, isLoading: o = !0 }) => !F(E) || !o ? n : /* @__PURE__ */ u("div", { className: "flex flex-col absolute top-10 left-1/2  -translate-x-1/2 w-[95%] sm:w-[600px] md:w-[700px] lg:w-[800px] bg-white shadow-2xl border border-gray-300 rounded-sm h-[60vh]", children: [
  /* @__PURE__ */ a("div", { className: "h-15 w-full bg-gray-200 rounded mb-6" }),
  /* @__PURE__ */ a("div", { className: "h-10 w-[90%] bg-gray-200 rounded m-4" }),
  /* @__PURE__ */ a("div", { className: "h-10 w-[90%] bg-gray-200 rounded m-4" }),
  /* @__PURE__ */ a("div", { className: "h-10 w-[90%] bg-gray-200 rounded m-4 " }),
  /* @__PURE__ */ a("div", { className: "h-9 w-1/3 bg-gray-200 rounded mt-auto ml-auto mb-2 mr-3" })
] });
Oa.propTypes = {
  children: g.any,
  isLoading: g.any
};
const Pa = ({ children: n, isLoading: o = !0 }) => !F(E) || !o ? n : /* @__PURE__ */ a("div", { className: "w-full flex justify-end pb-[10px]", children: /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm h-[32px] w-[100px]" }) });
Pa.propTypes = {
  children: g.any,
  isLoading: g.any
};
const Va = ({ children: n, isLoading: o = !0 }) => !F(E) || !o ? n : /* @__PURE__ */ a("div", { className: "flex justify-end w-full", children: /* @__PURE__ */ a("div", { className: "bg-gray-300 animate-pulse rounded-sm h-[32px] w-[120px]" }) });
Va.propTypes = {
  children: g.any,
  isLoading: g.any
};
g.any, g.any;
const Wa = U(() => import("./AppModal-BgZyfrd--BO3wW4ma.js")), Ba = U(() => import("./LanguagesModal-rk6IGQwY-B0Jtpivu.js")), Ra = U(() => import("./AddLabelModal-DE3ploGD-OOyaGC02.js"));
function $a() {
  const { translate: n } = _a(), o = ye(null), [h, O] = r([]), S = ye(!1), [Ce, G] = r([]), [Ae, J] = r(!1), [Se, P] = r(null), [Te, V] = r(!1), [X, Le] = r([]), [Y, Z] = r(!1), [ee, ae] = r(!1), [le, _e] = r(!0), [f, W] = r(""), [T, Ie] = r([]), [te, k] = r(!1), [ze, B] = r(null), [N, ne] = r(""), [y, ie] = r(""), [x, re] = r(!1), [L, Me] = r({}), [je, R] = r(!1), [Fe, Ee] = r(""), [Oe, Pe] = r(""), [d, Ve] = r({}), [We, se] = r(!1), [Be, oe] = r(null), [de, $] = r(!1), [Re, ce] = r(!1), [_, $e] = r(!1), [De, D] = r(!1), [ue, Ke] = r([]), [b, pe] = r([]), Qe = async () => {
    var e;
    try {
      const l = await ya({
        code: "get_distinct_namespce",
        parameters: {}
      });
      Ke((e = l == null ? void 0 : l.data) == null ? void 0 : e.result);
    } catch (l) {
      console.error(l);
    }
  };
  be(() => {
    qe(), Qe();
  }, []);
  const qe = async () => {
    try {
      await K();
      const { data: e } = await ba(
        "techo.manage.manageTranslation"
      );
      Me(JSON.parse(e));
    } catch (e) {
      console.error(e), v({ message: "Error in manage Translation", type: "error" });
    }
  }, K = async () => {
    var e, l;
    try {
      let t = await Fa();
      const s = (e = t == null ? void 0 : t.data) == null ? void 0 : e.filter((p) => p.isActive === !0);
      O(s), t = await ja();
      const i = (l = t == null ? void 0 : t.data) == null ? void 0 : l.filter((p) => p.active === !0);
      Ie(i);
    } catch (t) {
      console.error(t), v({ message: "Error in manage Translation", type: "error" });
    }
  }, He = (e) => {
    var l;
    W((l = e == null ? void 0 : e.target) == null ? void 0 : l.value);
  }, me = fa((e) => {
    const l = (e.match(/{/g) || []).length, t = (e.match(/}/g) || []).length;
    l > t || ne(e);
  }, []), Ue = (e, l) => {
    let t;
    return (...s) => {
      t && clearTimeout(t), t = setTimeout(() => {
        e(...s);
      }, l);
    };
  }, Ge = q(
    () => Ue(me, 300),
    [me]
  ), Je = (e) => {
    const { value: l } = e.target;
    Ge(l);
  }, ge = (e) => {
    ie(e);
  }, he = [
    { field: "Sr. No.", label: "Sr.No", align: "left" },
    { field: "Actions", label: "Actions" },
    { field: "app", label: "App", align: "left" },
    { field: "state", label: "State", align: "left" },
    { field: "key", label: "Key", align: "left" }
  ];
  T == null || T.map(
    (e) => {
      var l;
      return he.push({
        field: (l = e == null ? void 0 : e.languageValue) == null ? void 0 : l.toLowerCase(),
        label: e == null ? void 0 : e.languageValue,
        align: "left"
      });
    }
  );
  const Xe = (e) => {
    Ee(
      (e == null ? void 0 : e.state) === "Active" ? "Mark as Inactive" : "Mark as Active"
    ), Pe(
      (e == null ? void 0 : e.state) === "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?"
    ), Ve(e);
  }, Ye = () => {
    R((e) => !e);
  }, Ze = async () => {
    try {
      P({ key: d == null ? void 0 : d.key }), ce(!0), k((e) => !e);
    } catch (e) {
      console.error(e), v({ message: "Error in opening Add App Modal", type: "error" });
    }
  }, ea = async () => {
    var e, l, t;
    try {
      let s = {
        code: "labels_selected_apps_for_editing",
        parameters: { key: d == null ? void 0 : d.key.replace(/'/g, "''") }
      };
      const i = await j(s);
      let p = (e = i == null ? void 0 : i.data) == null ? void 0 : e.result;
      B((l = i == null ? void 0 : i.data) == null ? void 0 : l.result);
      let ma = {
        code: "labels_app_data_for_prepopulating",
        parameters: {
          key: d == null ? void 0 : d.key,
          appValue: d == null ? void 0 : d.app
        }
      }, ga = p.map((w) => w.app), ve = h.filter((w) => ga.indexOf(w.id) === -1);
      if (ve.length > 0) {
        const w = await j(ma);
        P((t = w == null ? void 0 : w.data) == null ? void 0 : t.result), G(ve), J(!0), k((ha) => !ha);
      } else
        v({
          message: "Label already present in all apps",
          type: "info"
        });
    } catch (s) {
      console.error(s), v({ message: "Error in opening Add App Modal", type: "error" });
    }
  }, aa = async () => {
    try {
      let e = (d == null ? void 0 : d.state) === "Active";
      await Ea(d == null ? void 0 : d.key, !e, d == null ? void 0 : d.appId), z(), R((l) => !l);
    } catch (e) {
      console.error(e), v({ message: "Error in Toggle State", type: "error" });
    }
  }, la = async (e) => {
    var l;
    try {
      V(!0), k((i) => !i), se((i) => !i);
      let t = e == null ? void 0 : e.key.replace(/'/g, "''");
      oe(t);
      const s = await j({
        code: "labels_selected_apps_for_editing",
        parameters: { key: t }
      });
      B((l = s == null ? void 0 : s.data) == null ? void 0 : l.result), V(!1);
    } catch (t) {
      console.error(t), v({ message: "Error in Editing label", type: "error" }), V(!1);
    }
  }, ta = [
    { label: n(Fe), onClick: Ye },
    { label: n("Add app"), onClick: ea },
    { label: n("Add Namespace"), onClick: Ze }
  ], na = [
    {
      label: "",
      icon: /* @__PURE__ */ a(ke, { icon: za }),
      onClick: la,
      condition: (e) => (e == null ? void 0 : e.state) !== "Inactive",
      dataTestId: "manage-translation-edit-icon"
    },
    {
      label: "",
      icon: /* @__PURE__ */ a(ke, { icon: Ma }),
      menuClick: Xe,
      dataTestId: "manage-translation-menu-icon",
      menuItems: ta
    }
  ], ia = () => {
    Z((e) => !e);
  }, ra = () => {
    ae((e) => !e);
  }, sa = () => {
    k((e) => !e), G(h);
  }, Q = (e) => e == null ? e : e.replace(/'/g, "''"), oa = async (e, l) => {
    var t;
    try {
      l && !_ ? (S.current = !0, $(!0)) : l && _ && D(!0);
      let s = {
        code: "fetch_labels_translation_translation_master",
        parameters: {
          appId: f || null,
          searchString: Q(N || ""),
          startsWith: Q(y || ""),
          namespaceArray: b.length > 0 ? b.map((p) => Q(p)).join(",") : "",
          showInactive: x || !1,
          limit: (e == null ? void 0 : e.limit) || 100,
          offset: (e == null ? void 0 : e.offset) || 0
        }
      };
      const i = await j(s);
      return (t = i == null ? void 0 : i.data) == null ? void 0 : t.result;
    } catch (s) {
      S.current = !1, console.error(s), D(!1), $(!1), v({ message: "Error in loading data", type: "error" });
    }
  }, fe = q(() => N === "" && y === "" && x === !1 && f === "" && b.length === 0, [N, y, x, f, b]), da = q(
    () => [
      "translation",
      f,
      N,
      y,
      x,
      b
    ],
    [f, N, y, x, b]
  ), {
    data: I,
    fetchNextPage: ca,
    hasNextPage: ua,
    refetch: z
  } = Ta({
    key: da,
    queryFn: ({ pageParam: e }) => {
      const l = !_, t = (!(e != null && e.offset) || (e == null ? void 0 : e.offset) === 0) && l;
      return oa(e, t);
    },
    limit: 100
  });
  be(() => {
    var e;
    if (I) {
      let l = ((e = I == null ? void 0 : I.pages) == null ? void 0 : e.flat()) || [];
      const t = Object.values(
        l == null ? void 0 : l.reduce((s, i) => {
          const p = (i == null ? void 0 : i.key) + "-" + (i == null ? void 0 : i.app);
          return s[p] || (s[p] = {
            appId: i == null ? void 0 : i.appid,
            key: i == null ? void 0 : i.key,
            app: i == null ? void 0 : i.app,
            state: i != null && i.is_active ? "Active" : "Inactive"
          }), s[p][i == null ? void 0 : i.language.toLowerCase()] = i == null ? void 0 : i.value, i != null && i.is_active && (s[p].state = "Active"), s;
        }, {})
      );
      _e(ua), Le(t), S.current && (_ ? D(!1) : ($(!1), $e(!0)), S.current = !1);
    }
  }, [I]);
  const pa = () => {
    ne(""), ie(""), re(!1), W(""), pe([]), o.current.value = "";
  };
  return /* @__PURE__ */ u(c, { container: !0, mt: 3, spacing: 2, children: [
    /* @__PURE__ */ a(M, { children: Y && /* @__PURE__ */ a(
      Wa,
      {
        open: Y,
        handleClose: () => Z((e) => !e),
        languages: T,
        refresh: K,
        refetch: z,
        selectedApp: f,
        setSelectedApp: W
      }
    ) }),
    /* @__PURE__ */ a(M, { children: ee && /* @__PURE__ */ a(
      Ba,
      {
        open: ee,
        handleClose: () => {
          ae((e) => !e);
        },
        refresh: K,
        refetch: z
      }
    ) }),
    /* @__PURE__ */ a(M, { children: te && /* @__PURE__ */ a(
      Ra,
      {
        open: te,
        handleClose: () => {
          k((e) => !e), J(!1), ce(!1), B(null), P(null), se(!1), oe(null);
        },
        apps: Ce,
        addAppFlag: Ae,
        addNameSpaceFlag: Re,
        languageList: T,
        editLabel: ze,
        fieldsData: Se,
        editLabelFlag: We,
        labelKey: Be,
        refetch: z,
        isLoading: Te
      }
    ) }),
    /* @__PURE__ */ a(M, {}),
    /* @__PURE__ */ a(
      La,
      {
        description: Oe,
        title: "CONFIRM ACTION",
        handleClick: aa,
        open: je,
        handleClose: () => {
          R((e) => !e);
        },
        "data-testid": "manageTranslation-modal-customconfirmation"
      }
    ),
    /* @__PURE__ */ a(c, { container: !0, spacing: 1, className: "grow", children: /* @__PURE__ */ u(Ne, { isLoading: de, children: [
      /* @__PURE__ */ a(c, { size: { xs: 6, sm: 6, md: 3 }, children: /* @__PURE__ */ u(H, { variant: "outlined", fullWidth: !0, children: [
        /* @__PURE__ */ a(C, { className: "pl-2", children: /* @__PURE__ */ a("span", { children: n("App") }) }),
        /* @__PURE__ */ a(
          xa,
          {
            value: f,
            onChange: He,
            size: "small",
            displayEmpty: !0,
            sx: {
              bgcolor: "background.paper",
              color: f ? "text.primary" : "text.disabled"
              // gray when empty
            },
            renderValue: (e) => {
              var l;
              return e ? (l = h.find((t) => t.id === e)) == null ? void 0 : l.appValue : "Select App";
            },
            children: h == null ? void 0 : h.map((e) => /* @__PURE__ */ a(wa, { value: e == null ? void 0 : e.id, children: e == null ? void 0 : e.appValue }, e == null ? void 0 : e.id))
          }
        )
      ] }) }),
      /* @__PURE__ */ a(c, { size: { xs: 6, sm: 6, md: 3 }, children: /* @__PURE__ */ u(H, { variant: "outlined", fullWidth: !0, children: [
        /* @__PURE__ */ a(C, { className: "", children: /* @__PURE__ */ a("span", { children: n("Name Spaces") }) }),
        /* @__PURE__ */ a(
          ka,
          {
            multiple: !0,
            options: ue ?? [],
            getOptionLabel: (e) => (e == null ? void 0 : e.label) ?? "",
            value: ue.filter(
              (e) => b.includes(e.value)
            ),
            onChange: (e, l) => {
              pe(l.map((t) => t.value));
            },
            filterSelectedOptions: !0,
            disableCloseOnSelect: !0,
            size: "small",
            renderTags: (e, l) => e.map((t, s) => /* @__PURE__ */ va(
              Na,
              {
                variant: "outlined",
                size: "small",
                label: t.label,
                ...l({ index: s }),
                key: t.label,
                sx: {
                  bgcolor: "action.hover"
                }
              }
            )),
            renderInput: (e) => /* @__PURE__ */ a(
              xe,
              {
                ...e,
                placeholder: "Select Name Space",
                sx: { bgcolor: "background.paper" },
                "data-testid": "autocomplete-namespace"
              }
            )
          }
        )
      ] }) }),
      /* @__PURE__ */ a(c, { size: { xs: 6, sm: 6, md: 3 }, children: /* @__PURE__ */ u(H, { variant: "outlined", fullWidth: !0, children: [
        /* @__PURE__ */ a(C, { sx: { pl: 1 }, children: /* @__PURE__ */ u("span", { children: [
          n("Search For"),
          ":"
        ] }) }),
        /* @__PURE__ */ a(
          xe,
          {
            inputRef: o,
            name: "searchQuery",
            id: "searchQuery",
            placeholder: "Enter Search Value",
            size: "small",
            onChange: Je,
            sx: { bgcolor: "background.paper" },
            "data-testid": "managetranslation-search-textfield"
          }
        )
      ] }) }),
      /* @__PURE__ */ a(
        c,
        {
          size: { xs: 12, sm: 12, md: 2 },
          sx: {
            display: "flex",
            mt: 3,
            justifyContent: "end",
            alignItems: "center"
          },
          children: /* @__PURE__ */ a(we, { children: /* @__PURE__ */ a(
            A,
            {
              variant: "contained",
              disabled: fe,
              sx: {
                lineHeight: "35px",
                ml: 1,
                mt: 1,
                display: "inline-block",
                ...fe ? {} : {
                  bgcolor: "error.main",
                  color: "error.contrastText",
                  "&:hover": { bgcolor: "error.dark" }
                }
              },
              onClick: pa,
              children: "Clear All Filters"
            }
          ) })
        }
      ),
      /* @__PURE__ */ a(
        c,
        {
          size: { xs: 12, sm: 12, md: 6.5 },
          sx: {
            pl: 1,
            display: "flex",
            justifyContent: "start",
            alignItems: "center"
          },
          children: /* @__PURE__ */ u(we, { className: "overflow-x-auto whitespace-nowrap", children: [
            /* @__PURE__ */ a(C, { className: "pb-[7px]", children: /* @__PURE__ */ u("span", { children: [
              n("Starts With"),
              ":"
            ] }) }),
            Array.from({ length: 26 }, (e, l) => {
              const t = String.fromCharCode(65 + l);
              return /* @__PURE__ */ a(
                C,
                {
                  component: "span",
                  className: `${y === t ? "selected-letter" : "letter-style"} inline-block cursor-pointer`,
                  onClick: () => ge(t),
                  children: t
                },
                t
              );
            }),
            /* @__PURE__ */ a(
              A,
              {
                variant: "contained",
                color: "secondary",
                disabled: y.length === 0,
                className: "x-button ml-2 inline-block",
                onClick: () => ge(""),
                children: "×"
              }
            )
          ] })
        }
      ),
      /* @__PURE__ */ a(
        c,
        {
          size: { xs: 12, sm: 12, md: 5.5 },
          display: "flex",
          alignItems: "center",
          children: /* @__PURE__ */ u(
            c,
            {
              container: !0,
              justifyContent: "flex-end",
              alignItems: "center",
              spacing: 1,
              children: [
                /* @__PURE__ */ a(c, { item: "true", children: /* @__PURE__ */ a(Ca, { children: /* @__PURE__ */ a(
                  Aa,
                  {
                    control: /* @__PURE__ */ a(
                      Sa,
                      {
                        checked: x,
                        onChange: (e) => re(e.target.checked)
                      }
                    ),
                    label: `${n("Show Inactive")}`,
                    labelPlacement: "end",
                    className: "translation-inactive"
                  }
                ) }) }),
                /* @__PURE__ */ a(c, { item: "true", children: (L == null ? void 0 : L.canManage) && /* @__PURE__ */ a(
                  A,
                  {
                    size: "small",
                    variant: "contained",
                    "data-testid": "manageTranslation-manage-apps-button",
                    color: "primary",
                    onClick: ia,
                    children: n("Manage Apps")
                  }
                ) }),
                /* @__PURE__ */ a(c, { item: "true", children: (L == null ? void 0 : L.canManage) && /* @__PURE__ */ a(
                  A,
                  {
                    size: "small",
                    variant: "contained",
                    "data-testid": "managetranslation-manage-language-button",
                    onClick: ra,
                    children: n("Manage Language")
                  }
                ) }),
                /* @__PURE__ */ a(c, { item: "true", children: /* @__PURE__ */ a(
                  A,
                  {
                    size: "small",
                    variant: "contained",
                    "data-testid": "manageTranalation-add-label-button",
                    onClick: sa,
                    children: n("Add Label")
                  }
                ) })
              ]
            }
          )
        }
      ),
      /* @__PURE__ */ a(c, { className: "mt-2", size: { xs: 12 }, children: /* @__PURE__ */ a(
        Ia,
        {
          columns: he,
          tableData: X,
          actions: na,
          paginationConfig: {
            pagination: !0,
            paginationType: "infinite-scroll",
            loadMore: () => le && ca({ cancelRefetch: !1 }),
            hasMore: le
          },
          dataTestId: "manageTranslation-table",
          isLoading: X ? de || De : !1,
          styleConfig: { tableStyle: { maxHeight: 120 } }
        }
      ) })
    ] }) })
  ] });
}
const gl = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  default: $a
}, Symbol.toStringTag, { value: "Module" }));
export {
  Pa as A,
  Va as L,
  gl as M,
  ja as a,
  sl as b,
  ol as c,
  rl as d,
  Oa as e,
  cl as f,
  Fa as g,
  ul as h,
  pl as i,
  ml as t,
  dl as u
};
//# sourceMappingURL=ManageTranslation-BTxvSYKb-CdO4Fk7f.js.map
