import { jsx as a, jsxs as c, Fragment as Te } from "react/jsx-runtime";
import { useState as l, useCallback as Se, useMemo as ke, useEffect as U, Suspense as j, lazy as q } from "react";
import Ne from "moment";
import { showToast as v } from "@/common/toaster/toaster";
import { r as Ie, E as Ae, t as We, a as Oe } from "./EventsSkeleton-DCXwwPbb.js";
import u from "@/common/services/util/GeneralUtil";
import { Box as y, Card as De, Typography as z, FormControlLabel as Y, Checkbox as H, Button as J, TextField as Fe } from "@mui/material";
import S from "@mui/material/Grid2";
import _e from "@/views/custom-components/CustomConfirmationModal";
import Me from "@/common/syncWithServer/SyncWithServerDialog";
import { getAssignedFeatures as Re } from "@/utils/feature.js";
import { useNavigate as Be } from "react-router";
import Qe from "@/hooks/useTrackEvent";
import { useTranslateProvider as Le } from "@/common/languageTranslator/TranslationContext";
import Pe from "@/views/custom-components/CustomizedTable/CustomizedTableV2";
import { faRotateRight as $e, faPen as Ue, faBars as je } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as k } from "@fortawesome/react-fontawesome";
import './EventConfigurations.css';const ze = q(() => import("./ShowFlywayQuery-DVRCd384.js")), Ye = q(() => import("./EventsExceptionModal-DsvngLi8.js"));
function ct() {
  const N = Be(), C = Qe(), [I, A] = l(null), [w, V] = l(null), [f, G] = l(null), [g, X] = l(!1), [W, K] = l(!1), [E, Z] = l(""), [O, p] = l(!1), [ee, te] = l(""), [D, b] = l(!1), [ne, oe] = l(""), [s, ae] = l({}), [ie, m] = l(!1), [le, F] = l(!1), [se, re] = l(""), [_, x] = l(null), [M, R] = l(!1), { translate: r } = Le(), [T, h] = l(!1), [i, ce] = l({}), ue = async (e) => {
    X(e.target.checked);
    let n = !e.target.checked;
    await d(n);
  }, fe = (e) => {
    K(e.target.checked);
  }, me = async () => {
    C("button_click", {
      button_name: "refresh-events-button",
      button_type: "reload",
      button_id: "eventconfiguration-refresh-button"
    }), d(!g);
  }, B = Se(
    (e) => {
      const n = f == null ? void 0 : f.filter(
        (t) => t.name.toLowerCase().includes(e.toLowerCase())
      ).map((t) => ({
        ...t,
        completionTime: t.completionTime ? Ne(t.completionTime).format("DD-MM-YYYY HH:mm") : "NA"
      }));
      A(n), C("search_tracking", {
        searchTerm: e,
        results_count: n == null ? void 0 : n.length,
        search_type: "Page Search"
      });
    },
    [f]
  ), he = (e, n) => {
    let t;
    return (...o) => {
      t && clearTimeout(t), t = setTimeout(() => {
        e(...o);
      }, n);
    };
  }, Q = ke(
    () => he(B, 500),
    [B]
  ), de = (e) => {
    const { value: n } = e.target;
    Z(n), Q(n);
  }, L = async () => {
    p(!0), m((e) => !e);
  }, ge = [
    {
      field: "name",
      label: "Name",
      align: "left"
    },
    {
      field: "eventType",
      label: "Event Type",
      align: "left"
    },
    {
      field: "trigerWhen",
      label: "Trigger When",
      align: "left"
    },
    {
      field: "state",
      label: "State",
      align: "left"
    },
    {
      field: "status",
      label: "Status",
      align: "left"
    },
    {
      field: "completionTime",
      label: "Completion Time",
      align: "left"
    },
    {
      field: "Actions",
      label: "Actions",
      align: "left"
    }
  ], pe = (e) => {
    te(
      (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?"
    ), oe(
      `Are you sure you want to run  ${e == null ? void 0 : e.name} event config now?`
    );
    const n = w == null ? void 0 : w.filter(
      (t) => t.uuid === e.uuid
    );
    ae(n[0]);
  }, ve = () => {
    b(!0), m(!0);
  }, ye = () => {
    R((e) => !e);
  }, Ce = async () => {
    try {
      if (O) {
        let e = !g;
        m(!1), h(!0), p(!1), await We(s), v({
          message: "Event configured status changed",
          type: "success"
        }), await d(e);
      } else if (D) {
        let e = !g;
        m(!1), b(!1), p(!1), h(!0), await Oe(s == null ? void 0 : s.uuid), v({ message: "Event configured for run now", type: "success" }), await d(e);
      }
    } catch {
      v({ message: "Error in toggle state", type: "error" });
    }
  }, P = () => {
    re(s), F(!0);
  }, we = () => x(() => null), $ = () => {
    x(s == null ? void 0 : s.uuid);
  }, Ee = (e) => {
    N(`/ui/medplat/views/config/${e == null ? void 0 : e.uuid}`);
  }, be = (e) => {
    let n = [], t = (e == null ? void 0 : e.state) === "ACTIVE" ? "Mark as Inactive" : "Mark as Active";
    return (e == null ? void 0 : e.eventType) === "TIMMER_BASED" || (e == null ? void 0 : e.eventType) === "RECURSION" ? n = [
      (!u.isTecho || (i == null ? void 0 : i["update.manage.event"])) && {
        label: r(t),
        onClick: L
      },
      {
        label: r("Sync With Server"),
        onClick: $
      },
      (!u.isTecho || (i == null ? void 0 : i["view.manage.event.runNow"])) && {
        label: r("Run Event Now"),
        onClick: ve
      },
      {
        label: r("ShowExceptions"),
        onClick: ye
      },
      {
        label: r("Show Flyway Query"),
        onClick: P
      }
    ].filter(Boolean) : n = [
      (!u.isTecho || (i == null ? void 0 : i["update.manage.event"])) && {
        label: t,
        onClick: L
      },
      {
        label: r("Sync With Server"),
        onClick: $
      },
      {
        label: r("Show Flyway Query"),
        onClick: P
      }
    ].filter(Boolean), [
      (!u.isTecho || (i == null ? void 0 : i["update.manage.event"])) && {
        label: "",
        icon: /* @__PURE__ */ a(
          k,
          {
            icon: Ue,
            "data-testid": "event-configuration-edit-button"
          }
        ),
        onClick: Ee
      },
      {
        label: "",
        icon: /* @__PURE__ */ a(
          k,
          {
            icon: je,
            "data-testid": "event-configuration-menu-button"
          }
        ),
        menuClick: pe,
        menuItems: n
      }
    ].filter(Boolean);
  };
  U(() => {
    (async () => {
      const { data: n } = await Re("techo.notification.all");
      n && ce(JSON.parse(n));
    })(), d(!0);
  }, []);
  const xe = (e) => {
    e.sort((t, o) => t.trigerWhen === null && o.trigerWhen !== null ? -1 : o.trigerWhen === null && t.trigerWhen !== null ? 1 : o.trigerWhen < t.trigerWhen ? -1 : o.trigerWhen > t.trigerWhen ? 1 : t.day === null && o.day !== null ? -1 : o.day === null && t.day !== null || o.day < t.day ? 1 : o.day > t.day || t.hour === null && o.hour !== null ? -1 : o.hour === null && t.hour !== null || o.hour < t.hour ? 1 : o.hour > t.hour || t.minute === null && o.minute !== null ? -1 : o.minute === null && t.minute !== null || o.minute < t.minute ? 1 : o.minute > t.minute ? -1 : 0);
    let n = e == null ? void 0 : e.map((t) => {
      let o = "";
      return t.trigerWhen && (o += t.trigerWhen + " "), t.day && (o += `<strong>Day:${t.day} </strong>`), t.hour && (o += `<strong>Hour:${t.hour} </strong>`), t.minute && (o += `<strong>Minute:${t.minute}</strong>`), t.trigerWhen = o, t;
    });
    !E && A(n), G(n);
  }, d = async (e) => {
    try {
      h(!0);
      const n = await Ie(e), t = JSON.parse(JSON.stringify(n == null ? void 0 : n.data));
      V(n == null ? void 0 : n.data), xe(t), h(!1);
    } catch {
      v({ message: "Error in event loading", type: "error" }), h(!1);
    }
  };
  return U(() => {
    Q(E);
  }, [f]), u.isTecho && !T && !(i != null && i["view.manage.event"]) ? /* @__PURE__ */ a(y, { className: "flex justify-center items-center w-full h-[80vh]", children: /* @__PURE__ */ a(
    De,
    {
      className: "h-80 w-150 flex justify-center items-center",
      children: /* @__PURE__ */ a(z, { variant: "h5", className: "text-customBlue", children: "You dont have permission to view Events" })
    }
  ) }) : /* @__PURE__ */ c(S, { className: "event-configuration w-[99%] my-4", children: [
    _ && /* @__PURE__ */ a(
      Me,
      {
        isOpen: !!x,
        handleClose: we,
        featureUUID: _,
        dataTestId: "eventconfiguration-dialog-syncwithserver"
      }
    ),
    /* @__PURE__ */ a(j, { children: M && /* @__PURE__ */ a(
      Ye,
      {
        id: s == null ? void 0 : s.id,
        title: "VIEW EXCEPTION",
        open: M,
        handleClose: () => R(!1),
        dataTestId: "eventconfiguration-modal-viewexception"
      }
    ) }),
    /* @__PURE__ */ a(j, { children: /* @__PURE__ */ a(
      ze,
      {
        configuredQuery: se,
        open: le,
        onClose: () => {
          F(!1);
        },
        dataTestId: "eventconfiguration-query-showflywayquery"
      }
    ) }),
    /* @__PURE__ */ a(
      _e,
      {
        description: O ? ee : D ? ne : null,
        title: "CONFIRM ACTION",
        handleClick: Ce,
        open: ie,
        handleClose: () => {
          m((e) => !e), p(!1), b(!1);
        },
        dataTestId: "eventconfiguration-modal-customconfirmation"
      }
    ),
    T ? /* @__PURE__ */ a(Ae, {}) : /* @__PURE__ */ c(Te, { children: [
      /* @__PURE__ */ c(
        S,
        {
          container: !0,
          justifyContent: "space-between",
          alignItems: "center",
          className: "mb-2",
          children: [
            /* @__PURE__ */ a(S, { children: /* @__PURE__ */ a(
              z,
              {
                variant: "body2",
                className: "font-bold font-sans text-lg text-customBlue uppercase",
                "data-testid": "notification-list-typography-title",
                children: r("Event List")
              }
            ) }),
            /* @__PURE__ */ c(y, { className: "flex flex-col md:flex-row md:items-center gap-2 md:space-x-2 w-full md:w-auto", children: [
              /* @__PURE__ */ c(y, { className: "flex items-center gap-2", children: [
                /* @__PURE__ */ a(
                  Y,
                  {
                    control: /* @__PURE__ */ a(
                      H,
                      {
                        checked: g,
                        "data-testid": "eventconfiguration-inactive-checkbox",
                        onChange: ue,
                        className: "text-xl"
                      }
                    ),
                    label: `${r("Show Inactive")}`,
                    labelPlacement: "end",
                    className: "m-0"
                  }
                ),
                /* @__PURE__ */ a(
                  Y,
                  {
                    control: /* @__PURE__ */ a(
                      H,
                      {
                        checked: W,
                        "data-testid": "eventconfiguration-Exception-checkbox",
                        onChange: fe,
                        className: "text-xl"
                      }
                    ),
                    label: `${r("Show Exception")}`,
                    labelPlacement: "end",
                    className: "m-0"
                  }
                )
              ] }),
              /* @__PURE__ */ c(y, { className: "flex items-center gap-2 w-full md:w-auto", children: [
                /* @__PURE__ */ a(
                  J,
                  {
                    size: "small",
                    variant: "contained",
                    "data-testid": "eventconfiguration-refresh-button",
                    color: "primary",
                    onClick: me,
                    className: "text-xl h-9 min-w-[50px]",
                    children: /* @__PURE__ */ a(k, { icon: $e })
                  }
                ),
                /* @__PURE__ */ a(
                  Fe,
                  {
                    name: "searchQuery",
                    id: "searchQuery",
                    placeholder: "Search Code",
                    size: "small",
                    value: E,
                    onChange: de,
                    sx: {
                      width: "100%",
                      backgroundColor: "white"
                    },
                    "data-testid": "eventconfiguration-textfield-searchquery"
                  }
                )
              ] }),
              (!u.isTecho || (i == null ? void 0 : i["create.manage.event"])) && /* @__PURE__ */ a(
                J,
                {
                  size: "small",
                  variant: "contained",
                  className: "w-full md:w-auto whitespace-nowrap",
                  "data-testid": "eventconfiguration-configurenew-button",
                  onClick: () => {
                    C("button_click", {
                      button_name: "configure-new-event-button",
                      button_type: "navigate",
                      button_id: "eventconfiguration-configurenew-button"
                    }), N("/ui/medplat/views/config");
                  },
                  children: r("Configure New")
                }
              )
            ] })
          ]
        }
      ),
      /* @__PURE__ */ a(
        Pe,
        {
          columns: ge,
          tableData: W ? I.filter(
            (e) => e.status === "EXCEPTION"
          ) : I,
          actions: be,
          paginationConfig: { pagination: !1 },
          dataTestId: "eventconfiguration-table-configuredevents",
          isLoading: T,
          customHeight: 10
        }
      )
    ] })
  ] });
}
export {
  ct as default
};
//# sourceMappingURL=EventConfigurations-DZvSJ5EZ.js.map
