import { jsx, jsxs } from "react/jsx-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__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsx(Heading, { depth: 3, children: "New project" }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "Project slug", children: /* @__PURE__ */ jsx(TextInput, { ...register("slug"), pattern: "[a-z][-a-z0-9]*", required: true }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "Project name", children: /* @__PURE__ */ jsx(TextInput, { ...register("name"), placeholder: values.slug }) }),
    /* @__PURE__ */ jsx(Divider, {}),
    /* @__PURE__ */ jsxs(Stack, { gap: "gap", children: [
      /* @__PURE__ */ jsx(Heading, { depth: 3, children: "Database credentials" }),
      /* @__PURE__ */ jsx("p", { children: "You can leave some of this fields empty to use default values." })
    ] }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "Host", children: /* @__PURE__ */ jsx(TextInput, { ...register("dbHost") }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "Port", children: /* @__PURE__ */ jsx(TextInput, { ...register("dbPort") }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "Database name", children: /* @__PURE__ */ jsx(TextInput, { ...register("dbName") }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "User", children: /* @__PURE__ */ jsx(TextInput, { ...register("dbUser") }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "Password", children: /* @__PURE__ */ jsx(TextInput, { ...register("dbPassword") }) }),
    /* @__PURE__ */ jsx(FieldContainer, { label: "SSL", children: /* @__PURE__ */ jsx(Select, { ...register("dbSsl"), options: [
      { value: "", label: "default" },
      { value: "yes", label: "yes" },
      { value: "no", label: "no" }
    ] }) }),
    /* @__PURE__ */ jsx(Divider, {}),
    /* @__PURE__ */ jsx(Button, { type: "submit", distinction: "primary", disabled: isSubmitting, children: "Create new project" })
  ] }) });
};
export {
  CreateProjectForm
};
//# sourceMappingURL=CreateProjectForm.js.map
