"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { useRouter } from "next/navigation";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { apiFetch } from "@open-mercato/ui/backend/utils/api";
import { readJsonSafe } from "@open-mercato/ui/backend/utils/serverErrors";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import {
  workflowDefinitionFormSchema,
  createFormGroups,
  createFieldDefinitions,
  defaultFormValues,
  buildWorkflowPayload
} from "../../../components/formConfig.js";
import { StepsEditor } from "../../../components/StepsEditor.js";
import { TransitionsEditor } from "../../../components/TransitionsEditor.js";
import { Alert, AlertDescription, AlertTitle } from "@open-mercato/ui/primitives/alert";
import { Zap } from "lucide-react";
import { formatWorkflowValidationError } from "../../../lib/format-validation-error.js";
function CreateWorkflowDefinitionPage() {
  const router = useRouter();
  const t = useT();
  const handleSubmit = async (values) => {
    const payload = buildWorkflowPayload(values);
    const response = await apiFetch("/api/workflows/definitions", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload)
    });
    if (!response.ok) {
      const errorBody = await readJsonSafe(response, null);
      throw new Error(formatWorkflowValidationError(errorBody, t("workflows.errors.createFailed")));
    }
    router.push("/backend/definitions");
    router.refresh();
  };
  const fields = React.useMemo(() => createFieldDefinitions(t), [t]);
  const formGroups = React.useMemo(
    () => createFormGroups(t, StepsEditor, TransitionsEditor),
    [t]
  );
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsxs(PageBody, { children: [
    /* @__PURE__ */ jsxs(Alert, { variant: "info", icon: /* @__PURE__ */ jsx(Zap, { "aria-hidden": "true" }), className: "mb-6", children: [
      /* @__PURE__ */ jsx(AlertTitle, { children: t("workflows.create.eventTriggersTitle") }),
      /* @__PURE__ */ jsx(AlertDescription, { children: t("workflows.create.eventTriggersDescription") })
    ] }),
    /* @__PURE__ */ jsx(
      CrudForm,
      {
        title: t("workflows.create.title"),
        backHref: "/backend/definitions",
        schema: workflowDefinitionFormSchema,
        fields,
        initialValues: defaultFormValues,
        onSubmit: handleSubmit,
        cancelHref: "/backend/definitions",
        groups: formGroups,
        submitLabel: t("workflows.form.create")
      }
    )
  ] }) });
}
export {
  CreateWorkflowDefinitionPage as default
};
//# sourceMappingURL=page.js.map
