import { useState, useCallback, useRef, createElement, memo, Fragment } from "react";
import { useProjectSlug } from "@contember/react-client";
import { useShowToast, Stack, FieldContainer, TextInput, Button, LayoutPage } from "@contember/ui";
import { useInvite } from "../../mutations/person/invite.js";
import { useRedirect } from "../../../routing/binding/useRedirect.js";
import { EditMembership } from "../member/EditMembership.js";
import { NavigateBackButton } from "../../../routing/binding/NavigateBackButton.js";
const InviteUser = ({ project, rolesConfig, userListLink, method }) => {
  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 });
    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, redirect, userListLink, addToast]);
  const editUserMembershipProps = { project, rolesConfig, memberships, setMemberships };
  const emailInput = useRef(null);
  return /* @__PURE__ */ createElement("form", {
    onSubmit: submit
  }, /* @__PURE__ */ createElement(Stack, {
    direction: "vertical",
    gap: "large"
  }, /* @__PURE__ */ createElement(FieldContainer, {
    label: "E-mail",
    errors: emailNotValidError ? [{ message: "Email is not valid." }] : void 0
  }, /* @__PURE__ */ createElement(TextInput, {
    ref: emailInput,
    validationState: emailNotValidError ? "invalid" : "default",
    value: email,
    onChange: useCallback((value) => setEmail?.(value), [setEmail])
  })), /* @__PURE__ */ createElement(EditMembership, {
    ...editUserMembershipProps
  }), /* @__PURE__ */ createElement(Button, {
    distinction: "primary",
    type: "submit",
    disabled: isSubmitting
  }, "Invite")));
};
const InviteUserToProject = memo(({ rolesConfig }) => {
  const project = useProjectSlug();
  if (!project) {
    return /* @__PURE__ */ createElement(Fragment, null, "Not in project.");
  }
  return /* @__PURE__ */ createElement(LayoutPage, {
    title: "Invite user",
    navigation: /* @__PURE__ */ createElement(NavigateBackButton, {
      to: "tenantUsers"
    }, "Back to list of users")
  }, /* @__PURE__ */ createElement(InviteUser, {
    project,
    rolesConfig,
    userListLink: "tenantUsers"
  }));
});
export { InviteUser, InviteUserToProject };
//# sourceMappingURL=InviteUser.js.map
