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