import { x as z, j as e, z as Q, G as j, I as P, N as m, u as U, h as L, J as G, M as K, _ as H, s as C, T as k, K as V, O as J, P as A, B as R, Q as S, C as W, R as X, a as Y, U as q, V as Z, b as E, e as ee, n as te, W as ae, X as se, v as le } from "./index-c65ca4a4.mjs";
import { u as oe, A as ne } from "./apiKeys-03faf508.mjs";
import { F as B } from "./Form-f4ced0f0.mjs";
const f = ({ className: t, children: a, ...s }) => {
  const o = z(
    "!py-3 !pl-0 !pr-6 align-top",
    s.onClick && "hover:cursor-pointer",
    t
  );
  return /* @__PURE__ */ e.jsx("td", { className: o, ...s, children: /* @__PURE__ */ e.jsx(Q, { className: "whitespace-nowrap", level: 6, children: a }) });
}, O = [
  {
    label: "Global",
    options: [{ value: "site.changed", label: "Site changed (rebuild)" }]
  },
  {
    label: "Posts",
    options: [
      { value: "post.added", label: "Post created" },
      { value: "post.deleted", label: "Post deleted" },
      { value: "post.edited", label: "Post updated" },
      { value: "post.published", label: "Post published" },
      { value: "post.published.edited", label: "Published post updated" },
      { value: "post.unpublished", label: "Post unpublished" },
      { value: "post.scheduled", label: "Post scheduled" },
      { value: "post.unscheduled", label: "Post unscheduled" },
      { value: "post.tag.attached", label: "Tag added to post" },
      { value: "post.tag.detached", label: "Tag removed from post" }
    ]
  },
  {
    label: "Pages",
    options: [
      { value: "page.added", label: "Page created" },
      { value: "page.deleted", label: "Page deleted" },
      { value: "page.edited", label: "Page updated" },
      { value: "page.published", label: "Page published" },
      { value: "page.published.edited", label: "Published page updated" },
      { value: "page.unpublished", label: "Page unpublished" },
      { value: "page.tag.attached", label: "Tag added to page" },
      { value: "page.tag.detached", label: "Tag removed from page" }
    ]
  },
  {
    label: "Tags",
    options: [
      { value: "tag.added", label: "Tag created" },
      { value: "tag.edited", label: "Tag updated" },
      { value: "tag.deleted", label: "Tag deleted" }
    ]
  },
  {
    label: "Members",
    options: [
      { value: "member.added", label: "Member added" },
      { value: "member.edited", label: "Member updated" },
      { value: "member.deleted", label: "Member deleted" }
    ]
  }
], de = (t) => {
  const a = O.flatMap(({ options: s }) => s).find((s) => s.value === t);
  return a == null ? void 0 : a.label;
}, re = j({
  method: "POST",
  path: () => "/webhooks/",
  body: (t) => ({ webhooks: [t] }),
  updateQueries: {
    dataType: P,
    update: (t, a) => a && {
      ...a,
      integrations: a.integrations.map((s) => {
        const o = t.webhooks[0];
        return o.integration_id === s.id ? { ...s, webhooks: [...s.webhooks || [], o] } : s;
      })
    }
  }
}), ie = j({
  method: "PUT",
  path: (t) => `/webhooks/${t.id}/`,
  body: (t) => ({ webhooks: [t] }),
  updateQueries: {
    dataType: P,
    update: (t, a) => a && {
      ...a,
      integrations: a.integrations.map((s) => {
        var o;
        return {
          ...s,
          webhooks: (o = s.webhooks) == null ? void 0 : o.map((c) => c.id === t.webhooks[0].id ? t.webhooks[0] : c)
        };
      })
    }
  }
}), ce = j({
  method: "DELETE",
  path: (t) => `/webhooks/${t}/`,
  updateQueries: {
    dataType: P,
    update: (t, a, s) => ({
      ...a,
      integrations: a.integrations.map((o) => {
        var c;
        return {
          ...o,
          webhooks: (c = o.webhooks) == null ? void 0 : c.filter((n) => n.id !== s)
        };
      })
    })
  }
}), ue = ({ webhook: t, integrationId: a }) => {
  const s = U(), { mutateAsync: o } = re(), { mutateAsync: c } = ie(), { formState: n, updateForm: l, handleSave: i, errors: h, clearError: p, validate: b } = L({
    initialState: t || {},
    onSave: async () => {
      n.id ? await c(n) : await o({ ...n, integration_id: a });
    },
    onValidate: () => {
      const r = {};
      return n.name || (r.name = "Please enter a name"), n.event || (r.event = "Please select an event"), n.target_url || (r.target_url = "Please enter a target URL"), n.target_url && !G.isURL(n.target_url) && (r.target_url = "Please enter a valid URL"), r;
    }
  });
  return /* @__PURE__ */ e.jsx(
    K,
    {
      okColor: "black",
      okLabel: t ? "Update" : "Add",
      size: "sm",
      testId: "webhook-modal",
      title: "Add webhook",
      formSheet: !0,
      onOk: async () => {
        H.remove(), await i() ? s.remove() : C({
          type: "pageError",
          message: "Can't save webhook! One or more fields have errors, please doublecheck you filled all mandatory fields"
        });
      },
      children: /* @__PURE__ */ e.jsx("div", { className: "mt-5", children: /* @__PURE__ */ e.jsxs(
        B,
        {
          marginBottom: !1,
          marginTop: !1,
          children: [
            /* @__PURE__ */ e.jsx(
              k,
              {
                error: !!h.name,
                hint: h.name,
                placeholder: "Custom webhook",
                title: "Name",
                value: n.name,
                onBlur: b,
                onChange: (r) => l((u) => ({ ...u, name: r.target.value })),
                onKeyDown: () => p("name")
              }
            ),
            /* @__PURE__ */ e.jsx(
              V,
              {
                error: !!h.event,
                hint: h.event,
                options: O,
                prompt: "Select an event",
                selectedOption: n.event,
                title: "Event",
                hideTitle: !0,
                onSelect: (r) => {
                  l((u) => ({ ...u, event: r })), p("event");
                }
              }
            ),
            /* @__PURE__ */ e.jsx(
              k,
              {
                error: !!h.target_url,
                hint: h.target_url,
                placeholder: "https://example.com",
                title: "Target URL",
                type: "url",
                value: n.target_url,
                onBlur: b,
                onChange: (r) => l((u) => ({ ...u, target_url: r.target.value })),
                onKeyDown: () => p("target_url")
              }
            ),
            /* @__PURE__ */ e.jsx(
              k,
              {
                placeholder: "Psst...",
                title: "Secret",
                value: n.secret || void 0,
                onChange: (r) => l((u) => ({ ...u, secret: r.target.value }))
              }
            )
          ]
        }
      ) })
    }
  );
}, N = m.create(ue), he = ({ integration: t }) => {
  var o, c, n;
  const { mutateAsync: a } = ce(), s = (l) => {
    m.show(W, {
      title: "Are you sure?",
      prompt: "Deleting this webhook may prevent the integration from functioning.",
      okColor: "red",
      okLabel: "Delete Webhook",
      onOk: async (i) => {
        await a(l), i == null || i.remove(), C({
          message: "Webhook deleted",
          type: "success"
        });
      }
    });
  };
  return /* @__PURE__ */ e.jsxs("div", { children: [
    /* @__PURE__ */ e.jsxs(J, { children: [
      /* @__PURE__ */ e.jsxs(A, { bgOnHover: !1, children: [
        /* @__PURE__ */ e.jsxs(f, { children: [
          ((o = t.webhooks) == null ? void 0 : o.length) || 0,
          " ",
          ((c = t.webhooks) == null ? void 0 : c.length) === 1 ? "webhook" : "webhooks"
        ] }),
        /* @__PURE__ */ e.jsx(f, { children: "Last triggered" }),
        /* @__PURE__ */ e.jsx(f, {})
      ] }),
      (n = t.webhooks) == null ? void 0 : n.map((l) => /* @__PURE__ */ e.jsxs(
        A,
        {
          action: /* @__PURE__ */ e.jsx(R, { color: "red", label: "Delete", link: !0, onClick: (i) => {
            i == null || i.stopPropagation(), s(l.id);
          } }),
          hideActions: !0,
          onClick: () => {
            m.show(N, {
              webhook: l,
              integrationId: t.id
            });
          },
          children: [
            /* @__PURE__ */ e.jsxs(S, { className: "w-1/2", children: [
              /* @__PURE__ */ e.jsx("div", { className: "text-sm font-semibold", children: l.name }),
              /* @__PURE__ */ e.jsxs("div", { className: "grid grid-cols-[max-content_1fr] gap-x-1 text-xs leading-snug", children: [
                /* @__PURE__ */ e.jsx("span", { className: "text-grey-600", children: "Event:" }),
                /* @__PURE__ */ e.jsx("span", { children: de(l.event) }),
                /* @__PURE__ */ e.jsx("span", { className: "text-grey-600", children: "URL:" }),
                /* @__PURE__ */ e.jsx("span", { children: l.target_url })
              ] })
            ] }),
            /* @__PURE__ */ e.jsx(S, { className: "w-1/2 text-sm", children: l.last_triggered_at && new Date(l.last_triggered_at).toLocaleString("default", {
              weekday: "short",
              month: "short",
              day: "numeric",
              year: "numeric",
              hour: "2-digit",
              minute: "2-digit",
              second: "2-digit"
            }) })
          ]
        }
      ))
    ] }),
    /* @__PURE__ */ e.jsx("div", { className: "mt-5", children: /* @__PURE__ */ e.jsx(
      R,
      {
        color: "green",
        icon: "add",
        iconColorClass: "text-green",
        label: "Add webhook",
        size: "sm",
        link: !0,
        onClick: () => {
          m.show(N, {
            integrationId: t.id
          });
        }
      }
    ) })
  ] });
}, ge = ({ integration: t }) => {
  var T, _;
  const a = U(), { updateRoute: s } = Y(), { mutateAsync: o } = q(), { mutateAsync: c } = oe(), { mutateAsync: n } = Z(), { formState: l, updateForm: i, handleSave: h, saveState: p, errors: b, clearError: r, validate: u } = L({
    initialState: t,
    onSave: async () => {
      await o(l);
    },
    onValidate: () => {
      const d = {};
      return l.name || (d.name = "Please enter a name"), d;
    }
  }), v = (T = t.api_keys) == null ? void 0 : T.find((d) => d.type === "admin"), x = (_ = t.api_keys) == null ? void 0 : _.find((d) => d.type === "content"), [M, D] = E(!1), [F, $] = E(!1);
  ee(() => {
    t.type !== "custom" && (a.remove(), s("integrations"));
  }, [t.type, a, s]);
  const I = (d, g) => {
    g(!1);
    const y = d.type === "content" ? "Content" : "Admin";
    m.show(W, {
      title: `Regenerate ${y} API Key`,
      prompt: `You can regenerate ${y} API Key any time, but any scripts or applications using it will need to be updated.`,
      okLabel: `Regenerate ${y} API Key`,
      onOk: async (w) => {
        await c({ integrationId: t.id, apiKeyId: d.id }), g(!0), w == null || w.remove();
      }
    });
  };
  return /* @__PURE__ */ e.jsxs(
    K,
    {
      afterClose: () => {
        s("integrations");
      },
      dirty: p === "unsaved",
      okColor: "black",
      okLabel: "Save & close",
      size: "md",
      testId: "custom-integration-modal",
      title: l.name,
      stickyFooter: !0,
      onOk: async () => {
        te.remove(), await h() ? (a.remove(), s("integrations")) : C({
          type: "pageError",
          message: "Can't save integration! One or more fields have errors, please doublecheck you filled all mandatory fields"
        });
      },
      children: [
        /* @__PURE__ */ e.jsxs("div", { className: "mt-7 flex w-full flex-col gap-7 md:flex-row", children: [
          /* @__PURE__ */ e.jsx("div", { children: /* @__PURE__ */ e.jsx(
            ae,
            {
              height: "100px",
              id: "custom-integration-icon",
              imageURL: l.icon_image || void 0,
              width: "100px",
              onDelete: () => i((d) => ({ ...d, icon_image: null })),
              onUpload: async (d) => {
                const g = se(await n({ file: d }));
                i((y) => ({ ...y, icon_image: g }));
              },
              children: "Upload icon"
            }
          ) }),
          /* @__PURE__ */ e.jsx("div", { className: "flex grow flex-col", children: /* @__PURE__ */ e.jsxs(B, { children: [
            /* @__PURE__ */ e.jsx(
              k,
              {
                error: !!b.name,
                hint: b.name,
                title: "Title",
                value: l.name,
                onBlur: u,
                onChange: (d) => i((g) => ({ ...g, name: d.target.value })),
                onKeyDown: () => r("name")
              }
            ),
            /* @__PURE__ */ e.jsx(k, { title: "Description", value: l.description || "", onChange: (d) => i((g) => ({ ...g, description: d.target.value })) }),
            /* @__PURE__ */ e.jsx("div", { children: /* @__PURE__ */ e.jsx(ne, { keys: [
              {
                label: "Content API key",
                text: x == null ? void 0 : x.secret,
                hint: F ? /* @__PURE__ */ e.jsx("div", { className: "text-green", children: "Content API Key was successfully regenerated" }) : void 0,
                onRegenerate: () => x && I(x, $)
              },
              {
                label: "Admin API key",
                text: v == null ? void 0 : v.secret,
                hint: M ? /* @__PURE__ */ e.jsx("div", { className: "text-green", children: "Admin API Key was successfully regenerated" }) : void 0,
                onRegenerate: () => v && I(v, D)
              },
              {
                label: "API URL",
                text: window.location.origin + le().subdir
              }
            ] }) })
          ] }) })
        ] }),
        /* @__PURE__ */ e.jsx("div", { children: /* @__PURE__ */ e.jsx(he, { integration: t }) })
      ]
    }
  );
}, me = ({ params: t }) => {
  const { data: { integrations: a } = {} } = X(), s = a == null ? void 0 : a.find(({ id: o }) => o === (t == null ? void 0 : t.id));
  return s ? /* @__PURE__ */ e.jsx(ge, { integration: s }) : null;
}, xe = m.create(me);
export {
  xe as default
};
//# sourceMappingURL=CustomIntegrationModal-51a3aa8b.mjs.map
