import { N as u, u as h, a as f, f as x, g as v, h as E, d as y, e as S, H as T, L as k, j as r, M as C, n as M, s as j, T as I, i as N } from "./index-c65ca4a4.mjs";
import { F as L } from "./Form-f4ced0f0.mjs";
import { T as A } from "./TextArea-65b41e1e.mjs";
const F = () => {
  var c, m;
  const i = h(), { updateRoute: o } = f(), { data: s } = x({
    searchParams: { filter: "newsletters.status:active+email_disabled:0", limit: "1", page: "1", include: "newsletters,labels" }
  }), { mutateAsync: p } = v(), { formState: t, updateForm: l, handleSave: b, errors: d, validate: w, clearError: g } = E({
    initialState: {
      name: "",
      description: "",
      optInExistingSubscribers: !0
    },
    onSave: async () => {
      const e = await p({
        name: t.name,
        description: t.description,
        opt_in_existing: t.optInExistingSubscribers
      });
      o({ route: `newsletters/show/${e.newsletters[0].id}` });
    },
    onValidate: () => {
      const e = {};
      return t.name || (e.name = "Please enter a name"), e;
    }
  }), n = y();
  return S(() => {
    n == null || n.errorIfWouldGoOverLimit("newsletters").catch((e) => {
      if (e instanceof T)
        u.show(k, {
          prompt: e.message || "Your current plan doesn't support more newsletters."
        }), i.remove(), o("newsletters");
      else
        throw e;
    });
  }, [n, i, o]), /* @__PURE__ */ r.jsx(
    C,
    {
      afterClose: () => {
        o("newsletters");
      },
      okColor: "black",
      okLabel: "Create",
      size: "sm",
      testId: "add-newsletter-modal",
      title: "Create newsletter",
      onOk: async () => {
        M.remove(), await b() ? i.remove() : j({
          type: "pageError",
          message: "Can't save newsletter! One or more fields have errors, please doublecheck you filled all mandatory fields"
        });
      },
      children: /* @__PURE__ */ r.jsxs(
        L,
        {
          marginBottom: !1,
          marginTop: !0,
          children: [
            /* @__PURE__ */ r.jsx(
              I,
              {
                error: !!d.name,
                hint: d.name,
                placeholder: "Weekly roundup",
                title: "Name",
                value: t.name,
                onBlur: w,
                onChange: (e) => l((a) => ({ ...a, name: e.target.value })),
                onKeyDown: () => g("name")
              }
            ),
            /* @__PURE__ */ r.jsx(
              A,
              {
                title: "Description",
                value: t.description,
                onChange: (e) => l((a) => ({ ...a, description: e.target.value }))
              }
            ),
            /* @__PURE__ */ r.jsx(
              N,
              {
                checked: t.optInExistingSubscribers,
                direction: "rtl",
                hint: t.optInExistingSubscribers ? `This newsletter will be available to all members. Your ${(c = s == null ? void 0 : s.meta) == null ? void 0 : c.pagination.total} existing subscriber${((m = s == null ? void 0 : s.meta) == null ? void 0 : m.pagination.total) === 1 ? "" : "s"} will also be opted-in to receive it.` : "The newsletter will be available to all new members. Existing members won’t be subscribed, but may visit their account area to opt-in to future emails.",
                label: "Opt-in existing subscribers",
                labelStyle: "heading",
                onChange: (e) => l((a) => ({ ...a, optInExistingSubscribers: e.target.checked }))
              }
            )
          ]
        }
      )
    }
  );
}, R = u.create(F);
export {
  R as default
};
//# sourceMappingURL=AddNewsletterModal-420f7077.mjs.map
