import { jsxs as C, Fragment as P, jsx as i } from "react/jsx-runtime";
import { S as d, A as ie } from "./AppCard-CQEg94Y3.js";
import { useMemo as H, useState as x } from "react";
import { at as u, au as w, av as L, o as q, n as W, I as z, S as v, B as F, ar as X, p as le, q as ne, W as ae, a8 as se, t as te } from "./index-C62tPk5p.js";
import { u as oe } from "./useIsAppEnabled-DnQeiNdg.js";
var M = /* @__PURE__ */ ((o) => (o.ON_BOOKING = "on_booking", o.ON_CANCEL = "on_cancel", o))(M || {});
const k = u.DATE, B = u.CHECKBOX, ce = {
  [u.TEXT]: "text",
  [u.STRING]: "text",
  [u.DATE]: "date",
  [u.DATETIME]: "datetime",
  [u.PHONE]: "phone",
  [u.CHECKBOX]: "checkbox",
  [u.PICKLIST]: "picklist",
  [u.CUSTOM]: "custom",
  [u.TEXTAREA]: "textarea"
}, re = {
  [w.BOOKING_CANCEL_DATE]: "booking_cancel_date",
  [w.BOOKING_START_DATE]: "booking_start_date",
  [w.BOOKING_CREATED_DATE]: "booking_created_date"
}, de = (o) => ({
  [L.EVERY_BOOKING]: o === M.ON_CANCEL ? "salesforce_on_every_cancellation" : "on_every_booking",
  [L.FIELD_EMPTY]: "only_if_field_is_empty"
}), ue = (o, p) => o.map((h) => ({
  label: p(ce[h]),
  value: h
})), fe = (o, p, h) => {
  const N = o === M.ON_CANCEL ? [w.BOOKING_CANCEL_DATE, w.BOOKING_START_DATE, w.BOOKING_CREATED_DATE] : [w.BOOKING_START_DATE, w.BOOKING_CREATED_DATE];
  return (p ?? N).map((f) => ({ label: h(re[f]), value: f }));
}, me = (o, p, h) => {
  const N = de(p);
  return o.map((t) => ({ label: h(N[t]), value: t }));
}, he = (o) => [
  { label: o("true"), value: !0 },
  { label: o("false"), value: !1 }
], ve = ({
  bookingAction: o,
  optionLabel: p,
  optionEnabled: h,
  optionSwitchOnChange: N,
  writeToObjectData: t,
  updateWriteToObjectData: f,
  supportedFieldTypes: _,
  supportedDateFields: m,
  supportedWriteTriggers: R = [L.EVERY_BOOKING, L.FIELD_EMPTY]
}) => {
  const { t: r } = q(), O = H(
    () => ue(_, r),
    [_, r]
  ), g = H(
    () => fe(o, m, r),
    [m, o, r]
  ), E = H(
    () => me(R, o, r),
    [R, o, r]
  ), I = E.length > 1, S = H(() => he(r), [r]), [G, y] = x(O[0]), [K, D] = x(g[0]), [s, J] = x(
    S[0]
  ), [j, Q] = x(E[0]), [Y, U] = x({}), [$, b] = x({}), [c, A] = x({
    field: "",
    fieldType: G.value,
    value: "",
    whenToWrite: j.value
  }), Z = (e) => {
    Object.keys(Y).forEach((a) => {
      Y[a] && a !== e && V(a);
    }), U((a) => ({ ...a, [e]: !0 })), b((a) => ({
      ...a,
      [e]: {
        field: e,
        fieldType: t[e].fieldType,
        value: t[e].value,
        whenToWrite: t[e].whenToWrite
      }
    }));
  }, V = (e) => {
    U((a) => ({ ...a, [e]: !1 })), b((a) => {
      const n = { ...a };
      return delete n[e], n;
    });
  }, ee = (e) => {
    const a = $[e];
    if (!a) return;
    if (!a.field.trim()) {
      X(r("field_name_cannot_be_empty"), "error");
      return;
    }
    if (a.field !== e && Object.keys(t).includes(a.field.trim())) {
      X(r("field_already_exists"), "error");
      return;
    }
    const n = { ...t };
    a.field !== e && delete n[e], n[a.field.trim()] = {
      fieldType: a.fieldType,
      value: a.value,
      whenToWrite: a.whenToWrite
    }, f(n), V(e);
  };
  return /* @__PURE__ */ C(P, { children: [
    /* @__PURE__ */ i(d.SubSectionHeader, { icon: "star", labelFor: "write-to-object-settings", title: p, children: /* @__PURE__ */ i(
      W,
      {
        checked: h,
        onCheckedChange: N,
        id: "write-to-object-settings",
        size: "sm"
      }
    ) }),
    h ? /* @__PURE__ */ C(d.SubSectionContent, { children: [
      /* @__PURE__ */ C("div", { className: "text-subtle flex gap-3 px-3 py-[6px] text-sm font-medium", children: [
        /* @__PURE__ */ i("div", { className: "flex-1", children: r("field_name") }),
        /* @__PURE__ */ i("div", { className: "flex-1", children: r("field_type") }),
        /* @__PURE__ */ i("div", { className: "flex-1", children: r("value") }),
        I && /* @__PURE__ */ i("div", { className: "flex-1", children: r("when_to_write") }),
        /* @__PURE__ */ i("div", { className: "w-20" })
      ] }),
      /* @__PURE__ */ C(d.SubSectionNested, { children: [
        Object.keys(t).map((e) => {
          const a = Y[e], n = $[e];
          return /* @__PURE__ */ C("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ i("div", { className: "flex-1", children: a ? /* @__PURE__ */ i(
              z,
              {
                value: (n == null ? void 0 : n.field) || e,
                onChange: (l) => b((T) => ({
                  ...T,
                  [e]: { ...n, field: l.target.value }
                })),
                size: "sm",
                className: "w-full"
              }
            ) : /* @__PURE__ */ i(z, { value: e, readOnly: !0, size: "sm", className: "w-full" }) }),
            /* @__PURE__ */ i("div", { className: "flex-1", children: a ? /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                options: O,
                value: O.find((l) => l.value === (n == null ? void 0 : n.fieldType)),
                onChange: (l) => {
                  l && b((T) => ({
                    ...T,
                    [e]: {
                      ...n,
                      fieldType: l.value,
                      ...l.value === k && {
                        value: g[0].value
                      },
                      ...l.value === B && {
                        value: S[0].value
                      }
                    }
                  }));
                }
              }
            ) : /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                value: O.find(
                  (l) => l.value === t[e].fieldType
                ),
                isDisabled: !0
              }
            ) }),
            /* @__PURE__ */ i("div", { className: "flex-1", children: a ? (n == null ? void 0 : n.fieldType) === k ? /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                options: g,
                value: g.find((l) => l.value === n.value),
                onChange: (l) => {
                  l && b((T) => ({
                    ...T,
                    [e]: { ...n, value: l.value }
                  }));
                }
              }
            ) : (n == null ? void 0 : n.fieldType) === B ? /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                options: S,
                value: S.find((l) => l.value === n.value),
                onChange: (l) => {
                  l && b((T) => ({
                    ...T,
                    [e]: { ...n, value: l.value }
                  }));
                }
              }
            ) : /* @__PURE__ */ i(
              z,
              {
                value: (n == null ? void 0 : n.value) || "",
                onChange: (l) => b((T) => ({
                  ...T,
                  [e]: { ...n, value: l.target.value }
                })),
                size: "sm",
                className: "w-full"
              }
            ) : t[e].fieldType === k ? /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                value: g.find(
                  (l) => l.value === t[e].value
                ),
                isDisabled: !0
              }
            ) : t[e].fieldType === B ? /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                value: S.find(
                  (l) => l.value === t[e].value
                ),
                isDisabled: !0
              }
            ) : /* @__PURE__ */ i(
              z,
              {
                value: t[e].value,
                readOnly: !0,
                size: "sm",
                className: "w-full"
              }
            ) }),
            I && /* @__PURE__ */ i("div", { className: "flex-1", children: a ? /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                options: E,
                value: E.find((l) => l.value === (n == null ? void 0 : n.whenToWrite)),
                onChange: (l) => {
                  l && b((T) => ({
                    ...T,
                    [e]: { ...n, whenToWrite: l.value }
                  }));
                }
              }
            ) : /* @__PURE__ */ i(
              v,
              {
                size: "sm",
                className: "w-full",
                value: E.find(
                  (l) => l.value === t[e].whenToWrite
                ),
                isDisabled: !0
              }
            ) }),
            /* @__PURE__ */ i("div", { className: "flex w-20 justify-center gap-1", children: a ? /* @__PURE__ */ C(P, { children: [
              /* @__PURE__ */ i(
                F,
                {
                  size: "sm",
                  StartIcon: "check",
                  variant: "icon",
                  color: "primary",
                  onClick: () => ee(e)
                }
              ),
              /* @__PURE__ */ i(
                F,
                {
                  size: "sm",
                  StartIcon: "x",
                  variant: "icon",
                  color: "secondary",
                  onClick: () => V(e)
                }
              )
            ] }) : /* @__PURE__ */ C(P, { children: [
              /* @__PURE__ */ i(
                F,
                {
                  size: "sm",
                  StartIcon: "pencil",
                  variant: "icon",
                  color: "minimal",
                  onClick: () => Z(e)
                }
              ),
              /* @__PURE__ */ i(
                F,
                {
                  size: "sm",
                  StartIcon: "x",
                  variant: "icon",
                  color: "minimal",
                  onClick: () => {
                    const l = { ...t };
                    delete l[e], f(l);
                  }
                }
              )
            ] }) })
          ] }, e);
        }),
        /* @__PURE__ */ C("div", { className: "flex gap-2", children: [
          /* @__PURE__ */ i("div", { className: "flex-1", children: /* @__PURE__ */ i(
            z,
            {
              size: "sm",
              className: "w-full",
              value: c.field,
              onChange: (e) => A({
                ...c,
                field: e.target.value
              })
            }
          ) }),
          /* @__PURE__ */ i("div", { className: "flex-1", children: /* @__PURE__ */ i(
            v,
            {
              size: "sm",
              className: "w-full",
              options: O,
              value: G,
              onChange: (e) => {
                e && (y(e), A({
                  ...c,
                  fieldType: e.value,
                  ...e.value === k && { value: K.value },
                  ...e.value === B && {
                    value: s.value
                  }
                }));
              }
            }
          ) }),
          /* @__PURE__ */ i("div", { className: "flex-1", children: c.fieldType === k ? /* @__PURE__ */ i(
            v,
            {
              size: "sm",
              className: "w-full",
              options: g,
              value: K,
              onChange: (e) => {
                e && (D(e), A({
                  ...c,
                  value: e.value
                }));
              }
            }
          ) : c.fieldType === B ? /* @__PURE__ */ i(
            v,
            {
              size: "sm",
              className: "w-full",
              options: S,
              value: s,
              onChange: (e) => {
                e && (J(e), A({
                  ...c,
                  value: e.value
                }));
              }
            }
          ) : /* @__PURE__ */ i(
            z,
            {
              size: "sm",
              className: "w-full",
              value: c.value,
              onChange: (e) => A({
                ...c,
                value: e.target.value
              })
            }
          ) }),
          I && /* @__PURE__ */ i("div", { className: "flex-1", children: /* @__PURE__ */ i(
            v,
            {
              size: "sm",
              className: "w-full",
              options: E,
              value: j,
              onChange: (e) => {
                e && (Q(e), A({
                  ...c,
                  whenToWrite: e.value
                }));
              }
            }
          ) }),
          /* @__PURE__ */ i("div", { className: "w-20" })
        ] })
      ] }),
      /* @__PURE__ */ i(
        F,
        {
          className: "text-subtle mt-2 w-fit",
          StartIcon: "plus",
          color: "minimal",
          size: "sm",
          disabled: !(c.field && c.fieldType && c.value !== "" && c.whenToWrite),
          onClick: () => {
            if (Object.keys(t).includes(c.field.trim())) {
              X(r("field_already_exists"), "error");
              return;
            }
            f({
              ...t,
              [c.field.trim()]: {
                fieldType: c.fieldType,
                value: c.value,
                whenToWrite: c.whenToWrite
              }
            }), A({
              field: "",
              fieldType: O[0].value,
              value: "",
              whenToWrite: E[0].value
            });
          },
          children: r("add_new_field")
        }
      )
    ] }) : null
  ] });
}, Oe = function({
  app: p,
  eventType: h,
  onAppInstallSuccess: N
}) {
  var D;
  const t = le.usePathname(), { t: f } = q(), { getAppData: _, setAppData: m } = ne(), { enabled: R, updateEnabled: r } = oe(p), O = _("ignoreGuests") ?? !1, g = _("skipContactCreation") ?? !1, E = _("setOrganizerAsOwner") ?? !1, I = _("overwriteContactOwner") ?? !1, S = _("onBookingWriteToEventObject") ?? !1, G = _("onBookingWriteToEventObjectFields") ?? {}, y = _("roundRobinLeadSkip") ?? !1, K = _("ifFreeEmailDomainSkipOwnerCheck") ?? !1;
  return /* @__PURE__ */ i(
    ie,
    {
      onAppInstallSuccess: N,
      returnTo: `${ae}${t}?tabName=apps`,
      app: p,
      teamId: ((D = h.team) == null ? void 0 : D.id) || void 0,
      switchOnClick: (s) => {
        r(s);
      },
      switchChecked: R,
      hideSettingsIcon: !0,
      children: /* @__PURE__ */ C(d.Content, { children: [
        /* @__PURE__ */ i(d.SubSection, { children: /* @__PURE__ */ i(
          d.SubSectionHeader,
          {
            icon: "user-plus",
            title: f("hubspot_ignore_guests"),
            labelFor: "ignore-guests",
            children: /* @__PURE__ */ i(
              W,
              {
                size: "sm",
                labelOnLeading: !0,
                checked: O,
                onCheckedChange: (s) => {
                  m("ignoreGuests", s);
                }
              }
            )
          }
        ) }),
        /* @__PURE__ */ i(d.SubSection, { children: /* @__PURE__ */ i(
          d.SubSectionHeader,
          {
            icon: "user-plus",
            title: f("skip_contact_creation", { appName: "HubSpot" }),
            labelFor: "skip-contact-creation",
            children: /* @__PURE__ */ i(
              W,
              {
                size: "sm",
                labelOnLeading: !0,
                checked: g,
                onCheckedChange: (s) => {
                  m("skipContactCreation", s);
                }
              }
            )
          }
        ) }),
        /* @__PURE__ */ i(d.SubSection, { children: /* @__PURE__ */ i(
          d.SubSectionHeader,
          {
            icon: "user-check",
            title: f("set_organizer_as_contact_owner"),
            labelFor: "set-organizer-as-owner",
            children: /* @__PURE__ */ i(
              W,
              {
                size: "sm",
                labelOnLeading: !0,
                checked: E,
                onCheckedChange: (s) => {
                  m("setOrganizerAsOwner", s), s || m("overwriteContactOwner", !1);
                }
              }
            )
          }
        ) }),
        E && /* @__PURE__ */ i(d.SubSection, { children: /* @__PURE__ */ i(
          d.SubSectionHeader,
          {
            icon: "refresh-cw",
            title: f("overwrite_existing_contact_owner"),
            labelFor: "overwrite-contact-owner",
            children: /* @__PURE__ */ i(
              W,
              {
                size: "sm",
                labelOnLeading: !0,
                checked: I,
                onCheckedChange: (s) => {
                  m("overwriteContactOwner", s);
                }
              }
            )
          }
        ) }),
        /* @__PURE__ */ i(d.SubSection, { children: /* @__PURE__ */ i(
          ve,
          {
            bookingAction: M.ON_BOOKING,
            optionLabel: f("on_booking_write_to_event_object"),
            optionEnabled: S,
            writeToObjectData: G,
            optionSwitchOnChange: (s) => {
              m("onBookingWriteToEventObject", s);
            },
            updateWriteToObjectData: (s) => m("onBookingWriteToEventObjectFields", s),
            supportedFieldTypes: [
              u.TEXT,
              u.DATE,
              u.PHONE,
              u.CHECKBOX,
              u.CUSTOM
            ],
            supportedWriteTriggers: [L.EVERY_BOOKING]
          }
        ) }),
        h.schedulingType === se.ROUND_ROBIN ? /* @__PURE__ */ C(P, { children: [
          /* @__PURE__ */ i(d.SubSection, { children: /* @__PURE__ */ i(
            d.SubSectionHeader,
            {
              icon: "users",
              title: f("crm_book_directly_with_attendee_owner", { appName: p.name }),
              labelFor: "book-directly-with-attendee-owner",
              children: /* @__PURE__ */ i(
                W,
                {
                  size: "sm",
                  id: "book-directly-with-attendee-owner",
                  checked: y,
                  onCheckedChange: (s) => {
                    m("roundRobinLeadSkip", s), s && m("enabled", s);
                  }
                }
              )
            }
          ) }),
          y && /* @__PURE__ */ C(d.SubSection, { children: [
            /* @__PURE__ */ i(
              d.SubSectionHeader,
              {
                icon: "users",
                title: f("crm_if_free_email_domain_skip_owner_check"),
                labelFor: "if-free-email-domain-skip-owner-check",
                children: /* @__PURE__ */ i(
                  W,
                  {
                    size: "sm",
                    id: "if-free-email-domain-skip-owner-check",
                    checked: K,
                    onCheckedChange: (s) => {
                      m("ifFreeEmailDomainSkipOwnerCheck", s);
                    }
                  }
                )
              }
            ),
            /* @__PURE__ */ i(te, { severity: "info", title: f("skip_rr_description") })
          ] })
        ] }) : null
      ] })
    }
  );
};
export {
  Oe as default
};
