import { useState, useEffect, useCallback, createElement, memo, Fragment } from "react";
import { useProjectSlug } from "@contember/react-client";
import { useShowToast, Stack, Button, LayoutPage } from "@contember/ui";
import { EditMembership } from "./EditMembership.js";
import { useProjectMembershipsQuery } from "../../queries/projectMemberships.js";
import { useUpdateProjectMembership } from "../../mutations/membership/updateMembership.js";
import { useRedirect } from "../../../routing/binding/useRedirect.js";
import { NavigateBackButton } from "../../../routing/binding/NavigateBackButton.js";
const EditIdentity = ({ project, rolesConfig, identityId, userListLink }) => {
  const { state: previousMembershipsState } = useProjectMembershipsQuery(project, identityId);
  const [memberships, setMemberships] = useState([void 0]);
  const updateMembership = useUpdateProjectMembership();
  useEffect(() => {
    setMemberships((currentMemberships) => {
      if (previousMembershipsState.state === "success") {
        return previousMembershipsState.data.memberships;
      }
      return currentMemberships;
    });
  }, [previousMembershipsState]);
  const redirect = useRedirect();
  const addToast = useShowToast();
  const [isSubmitting, setSubmitting] = useState(false);
  const submit = useCallback(async (e) => {
    e.preventDefault();
    setSubmitting(true);
    const membershipsToSave = memberships.filter((it) => it !== void 0);
    if (membershipsToSave.length === 0) {
      return;
    }
    const result = await updateMembership({ projectSlug: project, identityId, memberships: membershipsToSave });
    setSubmitting(false);
    if (result.ok) {
      addToast({
        type: "success",
        message: `Updated user's roles successfully.`,
        dismiss: true
      });
      redirect(userListLink);
    } else {
      switch (result.error.code) {
        case "NOT_MEMBER":
          return addToast({ message: `Project member not found`, 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, updateMembership, project, identityId, redirect, userListLink, addToast]);
  const editUserMembershipProps = { project, rolesConfig, memberships, setMemberships };
  return /* @__PURE__ */ createElement("form", {
    onSubmit: submit
  }, /* @__PURE__ */ createElement(Stack, {
    direction: "vertical",
    gap: "large"
  }, /* @__PURE__ */ createElement(EditMembership, {
    ...editUserMembershipProps
  }), /* @__PURE__ */ createElement(Button, {
    distinction: "primary",
    size: "large",
    type: "submit",
    disabled: isSubmitting
  }, "Save")));
};
const EditUserInProject = memo(({ rolesConfig, identityId }) => {
  const project = useProjectSlug();
  if (!project) {
    return /* @__PURE__ */ createElement(Fragment, null, "Not in project.");
  }
  return /* @__PURE__ */ createElement(LayoutPage, {
    title: "Edit user",
    navigation: /* @__PURE__ */ createElement(NavigateBackButton, {
      to: "tenantUsers"
    }, "Back to list of users")
  }, /* @__PURE__ */ createElement(EditIdentity, {
    project,
    rolesConfig,
    identityId,
    userListLink: "tenantUsers"
  }));
});
export { EditIdentity, EditUserInProject };
//# sourceMappingURL=EditIdentity.js.map
