import { jsxDEV } from "react/jsx-dev-runtime";
import { useShowToast, Stack, FieldContainer, TextInput, Button } from "@contember/ui";
import { useState, useCallback } from "react";
import { useForm } from "../../lib/useForm.js";
import { useCreateApiKey } from "../../mutations/apiKey/createApiKey.js";
import { EditMembership } from "../member/EditMembership.js";
import { useRedirect } from "@contember/react-routing";
const initialValues = {
  description: ""
};
const CreateApiKeyForm = ({ project, rolesConfig, apiKeyListLink }) => {
  const { register, values } = useForm(initialValues);
  const redirect = useRedirect();
  const addToast = useShowToast();
  const [isSubmitting, setSubmitting] = useState(false);
  const [memberships, setMemberships] = useState([void 0]);
  const createApiKey = useCreateApiKey();
  const submit = useCallback(
    async (e) => {
      e.preventDefault();
      setSubmitting(true);
      const membershipsToSave = memberships.filter((it) => it !== void 0);
      const response = await createApiKey({
        description: values.description,
        memberships: membershipsToSave,
        projectSlug: project
      });
      setSubmitting(false);
      if (response.ok) {
        addToast({
          type: "success",
          message: `API key has been created. Save following token: ${response.result.apiKey.token}`
        });
        redirect(apiKeyListLink);
      } else {
        switch (response.error.code) {
          case "INVALID_MEMBERSHIP":
            return addToast({ message: `Invalid membership definition`, type: "error" });
          case "PROJECT_NOT_FOUND":
            return addToast({ message: `Project not found`, type: "error" });
        }
      }
    },
    [memberships, createApiKey, values.description, project, addToast, redirect, apiKeyListLink]
  );
  const editUserMembershipProps = { project, rolesConfig, memberships, setMemberships };
  return /* @__PURE__ */ jsxDEV("form", { onSubmit: submit, children: /* @__PURE__ */ jsxDEV(Stack, { children: [
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Description", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("description") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
      lineNumber: 66,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
      lineNumber: 65,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(EditMembership, { ...editUserMembershipProps }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
      lineNumber: 69,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(Button, { display: "block", distinction: "primary", type: "submit", disabled: isSubmitting, children: "Create API key" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
      lineNumber: 71,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
    lineNumber: 64,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
    lineNumber: 63,
    columnNumber: 3
  }, void 0);
};
export {
  CreateApiKeyForm
};
//# sourceMappingURL=CreateApiKeyForm.js.map
