import { Y as k, e as v, j as s, aa as _, N as w, u as $, a as b, b as p, ab as N, M as E, r as C, t as A, ac as T, ad as D, ae as F, B as L, a5 as P, i as S } from "./index-c65ca4a4.mjs";
import { P as H } from "./Popover-016ae0d0.mjs";
import { T as j } from "./ToggleGroup-c7d7d7e9.mjs";
const M = ({ offset: e, onTrigger: t }) => {
  const r = k(null);
  return v(() => {
    const o = new IntersectionObserver((n) => {
      n[0].intersectionRatio <= 0 || t();
    });
    return r.current && o.observe(r.current), () => o.disconnect();
  }, [t]), /* @__PURE__ */ s.jsx("div", { ref: r, className: "absolute w-full", style: { bottom: e } });
}, B = "ActionsResponseType", R = _({
  dataType: B,
  path: "/actions/",
  returnData: (e) => {
    const { pages: t } = e;
    let r = t.flatMap((n) => n.actions.map(
      ({ context: a, ...u }) => ({ ...u, context: JSON.parse(a) })
    ));
    r = r.reverse();
    let o = 1;
    return r.forEach((n, a) => {
      const u = r[a + 1];
      u && n.resource_id === u.resource_id && n.event === u.event ? (n.skip = !0, o += 1) : o > 1 && (n.count = o, o = 1);
    }), {
      actions: r.reverse(),
      meta: t.at(-1).meta,
      isEnd: t.at(-1).actions.length < t.at(-1).meta.pagination.limit
    };
  }
}), O = (e) => {
  var r, o;
  let t = e.resource_type;
  if (e.event !== "deleted")
    switch (e.resource_type) {
      case "page":
      case "post":
        return !e.resource || !e.resource.id ? void 0 : (t === "post" && (r = e.context) != null && r.type && (t = (o = e.context) == null ? void 0 : o.type), {
          isExternal: !0,
          route: "editor.edit",
          models: [t, e.resource.id]
        });
      case "integration":
        return !e.resource || !e.resource.id ? void 0 : { route: `integrations/show/${e.resource.id}` };
      case "offer":
        return !e.resource || !e.resource.id ? void 0 : {
          isExternal: !0,
          route: "offer",
          models: [e.resource.id]
        };
      case "tag":
        return !e.resource || !e.resource.slug ? void 0 : {
          isExternal: !0,
          route: "tag",
          models: [e.resource.slug]
        };
      case "product":
        return { route: "tiers" };
      case "user":
        return !e.resource || !e.resource.slug ? void 0 : { route: `users/show/${e.resource.slug}` };
    }
}, z = (e) => {
  var o, n;
  let t = e.resource_type;
  t === "api_key" ? t = "API key" : t === "setting" ? t = "settings" : t === "product" && (t = "tier"), t === "post" && (o = e.context) != null && o.type && (t = (n = e.context) == null ? void 0 : n.type);
  let r = e.event;
  return e.event === "edited" && e.context.action_name && (r = e.context.action_name), e.context.count && e.context.count > 1 ? `${e.context.count} ${t}s ${r}` : `${t.slice(0, 1).toUpperCase()}${t.slice(1)} ${r}`;
}, U = (e) => {
  var t, r;
  if (e.resource_type === "setting" && (t = e.context) != null && t.group && (r = e.context) != null && r.key)
    return {
      group: e.context.group,
      key: e.context.key
    };
}, y = (e) => typeof e.context.count == "number" && e.context.count > 1, G = ({ action: e }) => {
  let t = "pen";
  switch (e.event) {
    case "added":
      t = "add";
      break;
    case "deleted":
      t = "trash";
      break;
  }
  return /* @__PURE__ */ s.jsx(P, { name: t, size: "xs" });
}, J = ({ action: e }) => {
  var t, r, o, n, a;
  return /* @__PURE__ */ s.jsxs("div", { className: "relative shrink-0", children: [
    /* @__PURE__ */ s.jsx(
      T,
      {
        bgColor: D(((t = e.actor) == null ? void 0 : t.name) || ((r = e.actor) == null ? void 0 : r.slug) || ""),
        image: (o = e.actor) == null ? void 0 : o.image,
        label: F(((n = e.actor) == null ? void 0 : n.name) || ((a = e.actor) == null ? void 0 : a.slug)),
        labelColor: "white",
        size: "md"
      }
    ),
    /* @__PURE__ */ s.jsx("div", { className: "absolute -bottom-1 -right-1 flex items-center justify-center rounded-full border border-grey-100 bg-white p-1 shadow-sm", children: /* @__PURE__ */ s.jsx(G, { action: e }) })
  ] });
}, m = ({ label: e, item: t, excludedItems: r, toggleItem: o }) => /* @__PURE__ */ s.jsx(
  S,
  {
    checked: !r.includes(t),
    direction: "rtl",
    label: e,
    labelClasses: "text-sm",
    onChange: (n) => o(t, n.target.checked)
  }
), Q = ({ excludedEvents: e, excludedResources: t, toggleEventType: r, toggleResourceType: o }) => /* @__PURE__ */ s.jsx(H, { position: "right", trigger: /* @__PURE__ */ s.jsx(L, { label: "Filter", link: !0 }), children: /* @__PURE__ */ s.jsxs("div", { className: "flex w-[220px] flex-col gap-8 p-5", children: [
  /* @__PURE__ */ s.jsxs(j, { children: [
    /* @__PURE__ */ s.jsx(m, { excludedItems: e, item: "added", label: "Added", toggleItem: r }),
    /* @__PURE__ */ s.jsx(m, { excludedItems: e, item: "edited", label: "Edited", toggleItem: r }),
    /* @__PURE__ */ s.jsx(m, { excludedItems: e, item: "deleted", label: "Deleted", toggleItem: r })
  ] }),
  /* @__PURE__ */ s.jsxs(j, { children: [
    /* @__PURE__ */ s.jsx(m, { excludedItems: t, item: "post", label: "Posts", toggleItem: o }),
    /* @__PURE__ */ s.jsx(m, { excludedItems: t, item: "page", label: "Pages", toggleItem: o }),
    /* @__PURE__ */ s.jsx(m, { excludedItems: t, item: "tag", label: "Tags", toggleItem: o }),
    /* @__PURE__ */ s.jsx(m, { excludedItems: t, item: "offer,product", label: "Tiers & offers", toggleItem: o }),
    /* @__PURE__ */ s.jsx(m, { excludedItems: t, item: "api_key,integration,setting,user,webhook", label: "Settings & staff", toggleItem: o })
  ] })
] }) }), V = ({ action: e }) => {
  var o, n, a, u, l, x;
  const { updateRoute: t } = b(), r = U(e);
  if (r) {
    const { group: d, key: g } = r;
    return /* @__PURE__ */ s.jsxs(s.Fragment, { children: [
      d.slice(0, 1).toUpperCase(),
      d.slice(1),
      d !== g && /* @__PURE__ */ s.jsxs("span", { className: "text-xs", children: [
        " ",
        /* @__PURE__ */ s.jsxs("code", { className: "mb-1 bg-white text-grey-800", children: [
          "(",
          g,
          ")"
        ] })
      ] })
    ] });
  } else if ((o = e.resource) != null && o.title || (n = e.resource) != null && n.name || e.context.primary_name) {
    const d = O(e);
    return d ? /* @__PURE__ */ s.jsx("a", { className: "font-bold", href: "#", onClick: (g) => {
      g.preventDefault(), t(d);
    }, children: ((a = e.resource) == null ? void 0 : a.title) || ((u = e.resource) == null ? void 0 : u.name) }) : /* @__PURE__ */ s.jsx(s.Fragment, { children: ((l = e.resource) == null ? void 0 : l.title) || ((x = e.resource) == null ? void 0 : x.name) || e.context.primary_name });
  } else
    return /* @__PURE__ */ s.jsx("span", { className: "text-grey-500", children: "(unknown)" });
}, Y = (e) => {
  const r = new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit"
  }).formatToParts(e).reduce((o, { type: n, value: a }) => ({ ...o, [n]: a }), {});
  return `${r.year}-${r.month}-${r.day} ${r.hour}:${r.minute}:${r.second}`;
}, Z = 200, X = w.create(() => {
  const e = $(), { updateRoute: t } = b(), [r, o] = p([]), [n, a] = p(["label"]), [u] = p(), { data: l, fetchNextPage: x } = R({
    searchParams: {
      include: "actor,resource",
      limit: Z.toString(),
      filter: [
        r.length && `event:-[${r.join(",")}]`,
        n.length && `resource_type:-[${n.join(",")}]`,
        u && `actor_id:${u}`
      ].filter(Boolean).join("+")
    },
    getNextPageParams: (i, c) => ({
      ...c,
      filter: [c.filter, `created_at:<'${Y(new Date(i.actions[i.actions.length - 1].created_at))}'`].join("+")
    }),
    keepPreviousData: !0
  }), d = N(() => {
    l != null && l.isEnd || x();
  }, [l == null ? void 0 : l.isEnd, x]), g = (i, c, f) => {
    i((h) => f ? h.concat(c) : h.filter((I) => I !== c));
  };
  return /* @__PURE__ */ s.jsx(
    E,
    {
      afterClose: () => {
        t("history");
      },
      cancelLabel: "",
      okLabel: "Close",
      scrolling: !0,
      size: "md",
      stickyFooter: !0,
      testId: "history-modal",
      title: "History",
      topRightContent: /* @__PURE__ */ s.jsx(
        Q,
        {
          excludedEvents: r,
          excludedResources: n,
          toggleEventType: (i, c) => g(o, i, !c),
          toggleResourceType: (i, c) => g(a, i, !c)
        }
      ),
      onOk: () => {
        e.remove(), t("history");
      },
      children: /* @__PURE__ */ s.jsx("div", { className: "relative -mb-8 mt-6", children: /* @__PURE__ */ s.jsxs(C, { hint: l != null && l.isEnd ? "End of history log" : void 0, children: [
        /* @__PURE__ */ s.jsx(M, { offset: 250, onTrigger: d }),
        l == null ? void 0 : l.actions.map((i) => {
          var c, f;
          return !i.skip && /* @__PURE__ */ s.jsx(
            A,
            {
              avatar: /* @__PURE__ */ s.jsx(J, { action: i }),
              detail: [
                new Date(i.created_at).toLocaleDateString("default", { year: "numeric", month: "short", day: "2-digit" }),
                new Date(i.created_at).toLocaleTimeString("default", { hour: "2-digit", minute: "2-digit", second: "2-digit" })
              ].join(" | "),
              title: /* @__PURE__ */ s.jsxs("div", { className: "text-sm", children: [
                z(i),
                y(i) ? "" : ": ",
                !y(i) && /* @__PURE__ */ s.jsx(V, { action: i }),
                i.count ? /* @__PURE__ */ s.jsxs(s.Fragment, { children: [
                  " ",
                  i.count,
                  " times"
                ] }) : null,
                /* @__PURE__ */ s.jsxs("span", { children: [
                  " — by ",
                  ((c = i.actor) == null ? void 0 : c.name) || ((f = i.actor) == null ? void 0 : f.slug)
                ] })
              ] }),
              separator: !0
            }
          );
        })
      ] }) })
    }
  );
});
export {
  X as default
};
//# sourceMappingURL=HistoryModal-ea279749.mjs.map
