"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { createCrud } from "@open-mercato/ui/backend/utils/crud";
import { createCrudFormError } from "@open-mercato/ui/backend/utils/serverErrors";
import { collectCustomFieldValues } from "@open-mercato/ui/backend/utils/customFieldValues";
import { slugify } from "@open-mercato/shared/lib/slugify";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { E } from "../../../../../../generated/entities.ids.generated.js";
import { CategorySelect } from "../../../../components/categories/CategorySelect.js";
import { CategorySlugFieldSync } from "../../../../components/categories/CategorySlugFieldSync.js";
async function submitCategoryCreate(values, t) {
  const name = typeof values.name === "string" ? values.name.trim() : "";
  if (!name) {
    const message = t("catalog.categories.form.errors.name", "Provide the category name.");
    throw createCrudFormError(message, { name: message });
  }
  const providedSlug = typeof values.slug === "string" ? values.slug.trim() : "";
  const generatedSlug = providedSlug.length ? providedSlug : slugify(name);
  const slug = generatedSlug.length ? generatedSlug : void 0;
  const description = typeof values.description === "string" && values.description.trim().length ? values.description.trim() : void 0;
  const parentId = typeof values.parentId === "string" && values.parentId.trim().length ? values.parentId.trim() : null;
  const customFields = collectCustomFieldValues(values);
  const payload = {
    name,
    slug,
    description,
    parentId,
    isActive: values.isActive !== false
  };
  if (Object.keys(customFields).length > 0) payload.customFields = customFields;
  await createCrud("catalog/categories", payload);
}
function CreateCatalogCategoryPage() {
  const t = useT();
  const fields = React.useMemo(() => [
    {
      id: "name",
      label: t("catalog.categories.form.field.name", "Name"),
      type: "text",
      required: true,
      placeholder: t("catalog.categories.form.field.namePlaceholder", "e.g., Footwear")
    },
    {
      id: "slug",
      label: t("catalog.categories.form.field.slug", "Slug"),
      type: "text",
      description: t("catalog.categories.form.field.slugHelp", "Lowercase identifier for URLs or imports.")
    },
    {
      id: "description",
      label: t("catalog.categories.form.field.description", "Description"),
      type: "textarea"
    },
    {
      id: "parentId",
      label: t("catalog.categories.form.field.parent", "Parent"),
      type: "custom",
      component: ({ id, value, setValue }) => /* @__PURE__ */ jsx(
        CategorySelect,
        {
          id,
          value: typeof value === "string" ? value : null,
          onChange: (next) => setValue(next ?? ""),
          includeEmptyOption: true,
          className: "w-full h-9 rounded border px-2 text-sm"
        }
      )
    },
    {
      id: "isActive",
      label: t("catalog.categories.form.field.isActive", "Active"),
      type: "checkbox"
    }
  ], [t]);
  const groups = React.useMemo(() => [
    {
      id: "details",
      title: t("catalog.categories.form.group.details", "Details"),
      column: 1,
      fields: ["name", "slug", "description", "parentId", "isActive"],
      component: (ctx) => /* @__PURE__ */ jsx(CategorySlugFieldSync, { ...ctx })
    },
    {
      id: "custom",
      title: t("catalog.categories.form.group.custom", "Custom data"),
      column: 2,
      kind: "customFields"
    }
  ], [t]);
  const successMessage = encodeURIComponent(t("catalog.categories.flash.created", "Category created"));
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
    CrudForm,
    {
      title: t("catalog.categories.form.createTitle", "Create category"),
      backHref: "/backend/catalog/categories",
      fields,
      groups,
      entityId: E.catalog.catalog_product_category,
      initialValues: { name: "", slug: "", description: "", parentId: "", isActive: true },
      submitLabel: t("catalog.categories.form.action.create", "Create"),
      cancelHref: "/backend/catalog/categories",
      successRedirect: `/backend/catalog/categories?flash=${successMessage}&type=success`,
      onSubmit: async (values) => {
        await submitCategoryCreate(values, t);
      }
    }
  ) }) });
}
export {
  CreateCatalogCategoryPage as default
};
//# sourceMappingURL=page.js.map
