import { jsx, jsxs } from "react/jsx-runtime";
import { useShowToast, Stack, FieldContainer, TextInput, Button } from "@contember/ui";
import { useState, useCallback, useRef } from "react";
import { useInvite } from "../../mutations/person/invite.js";
import { useRedirect } from "@contember/react-routing";
import { EditMembership } from "../member/EditMembership.js";
const InviteUser = ({ project, rolesConfig, userListLink, method, mailVariant }) => {
  const [email, setEmailInner] = useState("");
  const redirect = useRedirect();
  const addToast = useShowToast();
  const [isSubmitting, setSubmitting] = useState(false);
  const [emailNotValidError, setEmailNotValidError] = useState(false);
  const setEmail = useCallback((email2) => {
    setEmailNotValidError(false);
    setEmailInner(email2);
  }, []);
  const [memberships, setMemberships] = useState([void 0]);
  const invite = useInvite();
  const submit = 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({ 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, project, method, mailVariant, addToast, redirect, userListLink]
  );
  const editUserMembershipProps = { project, rolesConfig, memberships, setMemberships };
  const emailInput = useRef(null);
  return /* @__PURE__ */ jsx("form", { onSubmit: submit, children: /* @__PURE__ */ jsxs(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsx(FieldContainer, { label: "E-mail", errors: emailNotValidError ? [{ message: "Email is not valid." }] : void 0, children: /* @__PURE__ */ jsx(
      TextInput,
      {
        ref: emailInput,
        validationState: emailNotValidError ? "invalid" : "default",
        value: email,
        onChange: useCallback((value) => setEmail?.(value ?? ""), [setEmail])
      }
    ) }),
    /* @__PURE__ */ jsx(EditMembership, { ...editUserMembershipProps }),
    /* @__PURE__ */ jsx(Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Invite" })
  ] }) });
};
export {
  InviteUser
};
//# sourceMappingURL=InviteUser.js.map
