"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ui = require("@contember/ui");
const react = require("react");
const createProject = require("../../mutations/project/createProject.cjs");
const useForm = require("../../lib/useForm.cjs");
const reactRouting = require("@contember/react-routing");
const emptyForm = {
  slug: "",
  name: "",
  dbHost: "",
  dbName: "",
  dbUser: "",
  dbPassword: "",
  dbPort: "",
  dbSsl: ""
};
const CreateProjectForm = ({ projectListLink }) => {
  const { register, values } = useForm.useForm(emptyForm);
  const [isSubmitting, setSubmitting] = react.useState(false);
  const createProject$1 = createProject.useCreateProject();
  const toaster = ui.useShowToast();
  const redirect = reactRouting.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$1({
        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__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { depth: 3, children: "New project" }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Project slug", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("slug"), pattern: "[a-z][-a-z0-9]*", required: true }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Project name", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("name"), placeholder: values.slug }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, {}),
    /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "gap", children: [
      /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { depth: 3, children: "Database credentials" }),
      /* @__PURE__ */ jsxRuntime.jsx("p", { children: "You can leave some of this fields empty to use default values." })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Host", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("dbHost") }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Port", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("dbPort") }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Database name", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("dbName") }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "User", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("dbUser") }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Password", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("dbPassword") }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "SSL", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select, { ...register("dbSsl"), options: [
      { value: "", label: "default" },
      { value: "yes", label: "yes" },
      { value: "no", label: "no" }
    ] }) }),
    /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, {}),
    /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", distinction: "primary", disabled: isSubmitting, children: "Create new project" })
  ] }) });
};
exports.CreateProjectForm = CreateProjectForm;
//# sourceMappingURL=CreateProjectForm.cjs.map
