import { jsxDEV } from "react/jsx-dev-runtime";
import { useShowToast, Stack, Heading, FieldContainer, TextInput, Divider, Select, Button } from "@contember/ui";
import { useState } from "react";
import { useCreateProject } from "../../mutations/project/createProject.js";
import { useForm } from "../../lib/useForm.js";
import { useRedirect } from "@contember/react-routing";
const emptyForm = {
  slug: "",
  name: "",
  dbHost: "",
  dbName: "",
  dbUser: "",
  dbPassword: "",
  dbPort: "",
  dbSsl: ""
};
const CreateProjectForm = ({ projectListLink }) => {
  const { register, values } = useForm(emptyForm);
  const [isSubmitting, setSubmitting] = useState(false);
  const createProject = useCreateProject();
  const toaster = useShowToast();
  const redirect = useRedirect();
  const onSubmit = async (e) => {
    e.preventDefault();
    setSubmitting(true);
    const projectSlug = values.slug;
    const secrets = [];
    if (values.dbPassword) {
      secrets.push({ key: "db.password", value: values.dbPassword });
    }
    try {
      const result = await createProject({
        projectSlug,
        name: values.name || void 0,
        config: {
          db: {
            host: values.dbHost || void 0,
            port: values.dbPort ? Number(values.dbPort) : void 0,
            user: values.dbUser || void 0,
            ssl: values.dbSsl ? values.dbSsl === "yes" : void 0,
            database: values.dbName || void 0
          }
        },
        secrets
      });
      if (result.ok) {
        redirect(projectListLink);
        toaster({
          message: `Project ${projectSlug} created. Please save following deploy token: ${result.result.deployerApiKey.token}`,
          type: "success"
        });
      } else {
        switch (result.error.code) {
          case "ALREADY_EXISTS":
            toaster({ message: `Project ${projectSlug} already exists`, type: "error" });
            break;
          case "INIT_ERROR":
            toaster({
              message: `Project ${projectSlug} initialization has failed: ${result.error.developerMessage}`,
              type: "error"
            });
            break;
        }
      }
    } catch (e2) {
      console.error(e2);
      toaster({ message: `Request has failed. Please try later.`, type: "error" });
    } finally {
      setSubmitting(false);
    }
  };
  return /* @__PURE__ */ jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDEV(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDEV(Heading, { depth: 3, children: "New project" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 81,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Project slug", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("slug"), pattern: "[a-z][-a-z0-9]*", required: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 84,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 83,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Project name", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("name"), placeholder: values.slug }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 87,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 86,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(Divider, {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 90,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(Stack, { gap: "gap", children: [
      /* @__PURE__ */ jsxDEV(Heading, { depth: 3, children: "Database credentials" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
        lineNumber: 93,
        columnNumber: 6
      }, void 0),
      /* @__PURE__ */ jsxDEV("p", { children: "You can leave some of this fields empty to use default values." }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
        lineNumber: 94,
        columnNumber: 6
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 92,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Host", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("dbHost") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 98,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 97,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Port", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("dbPort") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 101,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 100,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Database name", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("dbName") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 104,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 103,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "User", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("dbUser") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 107,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 106,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("dbPassword") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 110,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 109,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "SSL", children: /* @__PURE__ */ jsxDEV(Select, { ...register("dbSsl"), options: [
      { value: "", label: "default" },
      { value: "yes", label: "yes" },
      { value: "no", label: "no" }
    ] }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 113,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 112,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(Divider, {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 120,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(Button, { type: "submit", distinction: "primary", disabled: isSubmitting, children: "Create new project" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
      lineNumber: 122,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
    lineNumber: 80,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/project/CreateProjectForm.tsx",
    lineNumber: 79,
    columnNumber: 3
  }, void 0);
};
export {
  CreateProjectForm
};
//# sourceMappingURL=CreateProjectForm.js.map
