"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const ui = require("@contember/ui");
const react = require("react");
const useForm = require("../../lib/useForm.cjs");
const createApiKey = require("../../mutations/apiKey/createApiKey.cjs");
const EditMembership = require("../member/EditMembership.cjs");
const reactRouting = require("@contember/react-routing");
const initialValues = {
  description: ""
};
const CreateApiKeyForm = ({ project, rolesConfig, apiKeyListLink }) => {
  const { register, values } = useForm.useForm(initialValues);
  const redirect = reactRouting.useRedirect();
  const addToast = ui.useShowToast();
  const [isSubmitting, setSubmitting] = react.useState(false);
  const [memberships, setMemberships] = react.useState([void 0]);
  const createApiKey$1 = createApiKey.useCreateApiKey();
  const submit = react.useCallback(
    async (e) => {
      e.preventDefault();
      setSubmitting(true);
      const membershipsToSave = memberships.filter((it) => it !== void 0);
      const response = await createApiKey$1({
        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$1, values.description, project, addToast, redirect, apiKeyListLink]
  );
  const editUserMembershipProps = { project, rolesConfig, memberships, setMemberships };
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit: submit, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Description", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.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__ */ jsxDevRuntime.jsxDEV(EditMembership.EditMembership, { ...editUserMembershipProps }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/apiKey/CreateApiKeyForm.tsx",
      lineNumber: 69,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.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);
};
exports.CreateApiKeyForm = CreateApiKeyForm;
//# sourceMappingURL=CreateApiKeyForm.cjs.map
