import { N as L, af as g, d as A, a as O, Y as T, b as u, ag as M, e as m, H as k, j as s, M as N, T as B, J as F, s as w } from "./index-c65ca4a4.mjs";
import { R as H } from "./Radio-155d4c50.mjs";
const z = L.create(() => {
  var p, b;
  const h = g(), v = g({
    searchParams: { limit: "all", permissions: "assign" }
  }), a = A(), { updateRoute: R } = O(), c = T(null), [o, y] = u(""), [t, n] = u(""), [i, x] = u("contributor"), [r, l] = u({}), { mutateAsync: j } = M();
  if (m(() => {
    c.current && c.current.focus();
  }, []), m(() => {
    t === "saved" && setTimeout(() => {
      n("");
    }, 2e3);
  }, [t]), m(() => {
    i !== "contributor" && (a != null && a.isLimited("staff")) ? a.errorIfWouldGoOverLimit("staff").then(() => {
      l((e) => ({ ...e, role: void 0 }));
    }).catch((e) => {
      if (e instanceof k) {
        l((f) => ({ ...f, role: e.message }));
        return;
      } else
        throw e;
    }) : l((e) => ({ ...e, role: void 0 }));
  }, [a, i]), !((p = h.data) != null && p.roles) || !((b = v.data) != null && b.roles))
    return null;
  const I = h.data.roles, E = v.data.roles;
  let d = "Send invitation now";
  t === "saving" ? d = "Sending..." : t === "saved" ? d = "Invite sent!" : t === "error" && (d = "Retry");
  const S = async () => {
    if (t !== "saving" && !Object.values(r).some((e) => e)) {
      if (!F.isEmail(o)) {
        l({
          email: "Please enter a valid email address."
        });
        return;
      }
      n("saving");
      try {
        await j({
          email: o,
          roleId: I.find(({ name: e }) => e.toLowerCase() === i.toLowerCase()).id
        }), n("saved"), w({
          message: `Invitation successfully sent to ${o}`,
          type: "success"
        });
      } catch {
        n("error"), w({
          message: `Failed to send invitation to ${o}`,
          type: "error"
        });
        return;
      }
    }
  }, C = [
    {
      hint: "Can create and edit their own posts, but cannot publish. An Editor needs to approve and publish for them.",
      label: "Contributor",
      value: "contributor"
    },
    {
      hint: "A trusted user who can create, edit and publish their own posts, but can’t modify others.",
      label: "Author",
      value: "author"
    },
    {
      hint: "Can invite and manage other Authors and Contributors, as well as edit and publish any posts on the site.",
      label: "Editor",
      value: "editor"
    },
    {
      hint: "Trusted staff user who should be able to manage all content and users, as well as site settings and options.",
      label: "Administrator",
      value: "administrator"
    }
  ].filter((e) => E.some((f) => f.name === e.label));
  return /* @__PURE__ */ s.jsx(
    N,
    {
      afterClose: () => {
        R("users");
      },
      cancelLabel: "",
      okLabel: d,
      size: 540,
      testId: "invite-user-modal",
      title: "Invite a new staff user",
      onOk: S,
      children: /* @__PURE__ */ s.jsxs("div", { className: "flex flex-col gap-6 py-4", children: [
        /* @__PURE__ */ s.jsx("p", { children: "Send an invitation for a new person to create a staff account on your site, and select a role that matches what you’d like them to be able to do." }),
        /* @__PURE__ */ s.jsx(
          B,
          {
            clearBg: !0,
            error: !!r.email,
            hint: r.email,
            inputRef: c,
            placeholder: "jamie@example.com",
            title: "Email address",
            value: o,
            onChange: (e) => {
              y(e.target.value);
            }
          }
        ),
        /* @__PURE__ */ s.jsx("div", { children: /* @__PURE__ */ s.jsx(
          H,
          {
            error: !!r.role,
            hint: r.role,
            id: "role",
            options: C,
            selectedOption: i,
            separator: !0,
            title: "Role",
            onSelect: (e) => {
              x(e);
            }
          }
        ) })
      ] })
    }
  );
});
export {
  z as default
};
//# sourceMappingURL=InviteUserModal-26e16672.mjs.map
