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