"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ui = require("@contember/ui");
const react = require("react");
const invite = require("../../mutations/person/invite.cjs");
const reactRouting = require("@contember/react-routing");
const EditMembership = require("../member/EditMembership.cjs");
const InviteUser = ({ project, rolesConfig, userListLink, method, mailVariant }) => {
  const [email, setEmailInner] = react.useState("");
  const redirect = reactRouting.useRedirect();
  const addToast = ui.useShowToast();
  const [isSubmitting, setSubmitting] = react.useState(false);
  const [emailNotValidError, setEmailNotValidError] = react.useState(false);
  const setEmail = react.useCallback((email2) => {
    setEmailNotValidError(false);
    setEmailInner(email2);
  }, []);
  const [memberships, setMemberships] = react.useState([void 0]);
  const invite$1 = invite.useInvite();
  const submit = react.useCallback(
    async (e) => {
      e.preventDefault();
      setEmailNotValidError(false);
      setSubmitting(true);
      const membershipsToSave = memberships.filter((it) => it !== void 0);
      if (!email.match(/^.+@.+$/)) {
        return setEmailNotValidError(true);
      }
      const inviteResult = await invite$1({ email, memberships: membershipsToSave, projectSlug: project, method, mailVariant });
      setSubmitting(false);
      if (inviteResult.ok) {
        addToast({
          type: "success",
          message: `User has been invited to this project and credentials have been sent to the given email.`,
          dismiss: true
        });
        redirect(userListLink);
      } else {
        switch (inviteResult.error.code) {
          case "ALREADY_MEMBER":
            return addToast({ message: `User is already member `, type: "error", dismiss: true });
          case "INVALID_MEMBERSHIP":
            return addToast({ message: `Invalid membership definition`, type: "error", dismiss: true });
          case "PROJECT_NOT_FOUND":
            return addToast({ message: `Project not found`, type: "error", dismiss: true });
        }
      }
    },
    [memberships, email, invite$1, project, method, mailVariant, addToast, redirect, userListLink]
  );
  const editUserMembershipProps = { project, rolesConfig, memberships, setMemberships };
  const emailInput = react.useRef(null);
  return /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit: submit, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "E-mail", errors: emailNotValidError ? [{ message: "Email is not valid." }] : void 0, children: /* @__PURE__ */ jsxRuntime.jsx(
      ui.TextInput,
      {
        ref: emailInput,
        validationState: emailNotValidError ? "invalid" : "default",
        value: email,
        onChange: react.useCallback((value) => setEmail?.(value ?? ""), [setEmail])
      }
    ) }),
    /* @__PURE__ */ jsxRuntime.jsx(EditMembership.EditMembership, { ...editUserMembershipProps }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Invite" })
  ] }) });
};
exports.InviteUser = InviteUser;
//# sourceMappingURL=InviteUser.cjs.map
