import { jsx as l, Fragment as O, jsxs as a } from "react/jsx-runtime";
import { ag as V, ah as v, M as u, N as C, ai as W, y as A, q as H, u as T, aj as q, r as j, _ as N, ak as L, t as F, ae as M, al as g, p as U } from "./index-ZQlxzdt5.js";
import * as c from "react";
var y = c.forwardRef((e, r) => {
  const { children: t, ...n } = e, s = c.Children.toArray(t), i = s.find(Z);
  if (i) {
    const o = i.props.children, f = s.map((d) => d === i ? c.Children.count(o) > 1 ? c.Children.only(null) : c.isValidElement(o) ? o.props.children : null : d);
    return /* @__PURE__ */ l(x, { ...n, ref: r, children: c.isValidElement(o) ? c.cloneElement(o, void 0, f) : null });
  }
  return /* @__PURE__ */ l(x, { ...n, ref: r, children: t });
});
y.displayName = "Slot";
var x = c.forwardRef((e, r) => {
  const { children: t, ...n } = e;
  if (c.isValidElement(t)) {
    const s = G(t);
    return c.cloneElement(t, {
      ...p(n, t.props),
      // @ts-ignore
      ref: r ? V(r, s) : s
    });
  }
  return c.Children.count(t) > 1 ? c.Children.only(null) : null;
});
x.displayName = "SlotClone";
var Q = ({ children: e }) => /* @__PURE__ */ l(O, { children: e });
function Z(e) {
  return c.isValidElement(e) && e.type === Q;
}
function p(e, r) {
  const t = { ...r };
  for (const n in r) {
    const s = e[n], i = r[n];
    /^on[A-Z]/.test(n) ? s && i ? t[n] = (...f) => {
      i(...f), s(...f);
    } : s && (t[n] = s) : n === "style" ? t[n] = { ...s, ...i } : n === "className" && (t[n] = [s, i].filter(Boolean).join(" "));
  }
  return { ...e, ...t };
}
function G(e) {
  var n, s;
  let r = (n = Object.getOwnPropertyDescriptor(e.props, "ref")) == null ? void 0 : n.get, t = r && "isReactWarning" in r && r.isReactWarning;
  return t ? e.ref : (r = (s = Object.getOwnPropertyDescriptor(e, "ref")) == null ? void 0 : s.get, t = r && "isReactWarning" in r && r.isReactWarning, t ? e.props.ref : e.props.ref || e.ref);
}
const J = ({
  children: e,
  ref: r,
  className: t
}) => {
  const [n] = v();
  return /* @__PURE__ */ l("div", { ref: n, className: u("bg-muted flex flex-col gap-4 rounded-2xl p-4", t), children: e });
}, R = ({
  children: e,
  ref: r,
  as: t
}) => /* @__PURE__ */ l(t ? y : "h2", { ref: r, className: "text-emphasis text-base font-semibold leading-none", children: e }), _ = ({
  children: e,
  ref: r,
  as: t
}) => /* @__PURE__ */ l(t ? y : "h3", { ref: r, className: "text-subtle line-clamp-1 text-sm", children: e }), b = ({
  ref: e,
  name: r,
  size: t = "md",
  iconSlot: n
}) => /* @__PURE__ */ l(
  "div",
  {
    ref: e,
    className: u(
      "bg-default border-subtle border-subtle flex items-center justify-center border",
      t === "sm" && "rounded-md p-1",
      t === "md" && "rounded-[10px] p-1.5"
    ),
    children: n || /* @__PURE__ */ l(C, { name: r, className: u(t === "sm" && "h-4 w-4", t === "md" && "h-6 w-6") })
  }
), K = ({
  children: e,
  ref: r,
  icon: t,
  title: n,
  description: s,
  iconSlot: i,
  rawHeading: o
}) => /* @__PURE__ */ a("div", { ref: r, className: "flex items-center justify-between gap-3", children: [
  /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
    t && !i && /* @__PURE__ */ l(b, { name: t }),
    i && /* @__PURE__ */ l(b, { iconSlot: i }),
    /* @__PURE__ */ a("div", { className: "flex flex-col gap-0.5", children: [
      n && /* @__PURE__ */ l(R, { children: n }),
      s && /* @__PURE__ */ l(_, { children: s }),
      o && o
    ] })
  ] }),
  e
] }), X = ({ children: e, ref: r }) => /* @__PURE__ */ l("div", { ref: r, className: "flex flex-col gap-4", children: e }), Y = ({ children: e, ref: r }) => {
  const [t] = v();
  return /* @__PURE__ */ l("div", { ref: t, className: "border-muted bg-default flex flex-col gap-4 rounded-xl border p-3", children: e });
}, z = ({
  children: e,
  ref: r,
  icon: t,
  title: n,
  classNames: s,
  justify: i = "between",
  labelFor: o
}) => /* @__PURE__ */ a(
  "div",
  {
    ref: r,
    className: u(
      "flex items-center gap-2",
      i === "between" ? "justify-between" : "justify-start",
      s == null ? void 0 : s.container
    ),
    children: [
      /* @__PURE__ */ a("div", { className: u("flex items-center gap-2", s == null ? void 0 : s.title), children: [
        t && /* @__PURE__ */ l(b, { name: t, size: "sm" }),
        /* @__PURE__ */ l("div", { className: u("flex", s == null ? void 0 : s.title), children: /* @__PURE__ */ l("h4", { className: "text-default text-sm font-medium leading-none", id: o, children: n }) })
      ] }),
      e
    ]
  }
), $ = ({
  children: e,
  ref: r,
  invert: t,
  classNames: n
}) => /* @__PURE__ */ l(
  "div",
  {
    ref: r,
    className: u(
      "bg-muted flex flex-col rounded-lg px-[6px] py-1",
      t && "bg-default border-subtle border",
      n == null ? void 0 : n.container
    ),
    children: e
  }
), ee = ({
  children: e,
  ref: r
}) => /* @__PURE__ */ l("div", { ref: r, className: "bg-default border-subtle flex flex-col gap-2 rounded-xl border p-2", children: e }), h = Object.assign(J, {
  Header: K,
  Content: X,
  Title: R,
  Description: _,
  SubSection: Y,
  SubSectionHeader: z,
  SubSectionContent: $,
  SubSectionNested: ee
});
function te(e) {
  const { status: r } = W.useSession();
  return A.viewer.apps.appById.useQuery(
    { appId: e },
    {
      enabled: r === "authenticated"
    }
  );
}
function ne({
  appId: e,
  className: r,
  returnTo: t,
  teamId: n
}) {
  const { t: s } = H(), { data: i } = te(e), o = A.useUtils(), f = T(null, {
    returnTo: t,
    onSuccess: (d) => {
      o.viewer.apps.appById.invalidate({ appId: e }), o.viewer.apps.integrations.invalidate({
        extendsFeature: "EventType",
        ...n && { teamId: n }
      }), !(d != null && d.setupPending) && N(s("app_successfully_installed"), "success");
    },
    onError: (d) => {
      d instanceof Error && N(d.message || s("app_could_not_be_installed"), "error");
    }
  });
  return i ? /* @__PURE__ */ l(
    q,
    {
      type: i.type,
      teamsPlanRequired: i.teamsPlanRequired,
      wrapperClassName: u("[@media(max-width:260px)]:w-full", r),
      render: ({ useDefaultComponent: d, ...m }) => (d && (m = {
        ...m,
        onClick: () => {
          f.mutate({
            type: i.type,
            variant: i.variant,
            slug: i.slug,
            ...n && { teamId: n }
          });
        }
      }), /* @__PURE__ */ l(
        j,
        {
          loading: f.isPending,
          color: "secondary",
          className: "[@media(max-width:260px)]:w-full [@media(max-width:260px)]:justify-center",
          StartIcon: "plus",
          ...m,
          children: s("add")
        }
      ))
    }
  ) : null;
}
function se({
  app: e,
  description: r,
  switchOnClick: t,
  switchChecked: n,
  children: s,
  returnTo: i,
  teamId: o,
  disableSwitch: f,
  switchTooltip: d,
  hideSettingsIcon: m = !1,
  hideAppCardOptions: E = !1
}) {
  const { t: w } = L(), [I] = v(), { setAppData: P, LockedIcon: k, disabled: B } = F(), D = M();
  return /* @__PURE__ */ a(h, { className: u(!(e != null && e.isInstalled) && "rounded-xl"), children: [
    /* @__PURE__ */ l(
      h.Header,
      {
        rawHeading: /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ a("div", { className: "flex w-full items-center gap-1", children: [
            /* @__PURE__ */ l(h.Title, { children: e == null ? void 0 : e.name }),
            !(e != null && e.isInstalled) && /* @__PURE__ */ l("span", { className: "bg-emphasis ml-1 rounded px-1 py-0.5 text-xs font-medium leading-3 tracking-[0.01em]", children: (e == null ? void 0 : e.categories[0].charAt(0).toUpperCase()) + (e == null ? void 0 : e.categories[0].slice(1)) })
          ] }),
          /* @__PURE__ */ l(h.Description, { children: e == null ? void 0 : e.description })
        ] }),
        iconSlot: /* @__PURE__ */ l(g, { href: `/apps/${e.slug}`, className: "flex h-8 w-8 items-center justify-center", children: /* @__PURE__ */ l(
          "img",
          {
            className: u(
              (e == null ? void 0 : e.logo.includes("-dark")) && "dark:invert",
              "max-h-full max-w-full object-contain"
            ),
            src: e == null ? void 0 : e.logo,
            alt: e == null ? void 0 : e.name
          }
        ) }),
        children: /* @__PURE__ */ l("div", { children: /* @__PURE__ */ l("div", { children: e != null && e.isInstalled || e.credentialOwner ? /* @__PURE__ */ l("div", { className: "ml-auto flex items-center", children: /* @__PURE__ */ l(
          U,
          {
            size: "sm",
            disabled: !e.enabled || B || f,
            onCheckedChange: (S) => {
              t && t(S), P("enabled", S);
            },
            checked: n,
            LockedIcon: k,
            "data-testid": `${e.slug}-app-switch`,
            tooltip: d
          }
        ) }) : /* @__PURE__ */ l(
          ne,
          {
            className: "ml-auto flex items-center",
            appId: e.slug,
            returnTo: i,
            teamId: o
          }
        ) }) })
      }
    ),
    E ? null : (e == null ? void 0 : e.isInstalled) && n && /* @__PURE__ */ l("div", { ref: I, children: e.isSetupAlready === void 0 || e.isSetupAlready ? /* @__PURE__ */ a("div", { className: "relative text-sm [&_input]:mb-0 [&_input]:leading-4", children: [
      !m && !D && /* @__PURE__ */ l(g, { href: `/apps/${e.slug}/setup`, className: "absolute right-4 top-4", children: /* @__PURE__ */ l(C, { name: "settings", className: "text-default h-4 w-4", "aria-hidden": "true" }) }),
      s
    ] }) : /* @__PURE__ */ a("div", { className: "flex h-64 w-full flex-col items-center justify-center gap-4 ", children: [
      /* @__PURE__ */ l("p", { children: w("this_app_is_not_setup_already") }),
      /* @__PURE__ */ l(g, { href: `/apps/${e.slug}/setup`, children: /* @__PURE__ */ l(j, { StartIcon: "settings", children: w("setup") }) })
    ] }) })
  ] });
}
export {
  se as A,
  h as S
};
