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