"use client";
import { jsx } from "react/jsx-runtime";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { useRouter } from "next/navigation";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { createCrud } from "@open-mercato/ui/backend/utils/crud";
import { collectCustomFieldValues } from "@open-mercato/ui/backend/utils/customFieldValues";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { useChannelFields, buildChannelPayload } from "@open-mercato/core/modules/sales/components/channels/channelFormFields";
import { E } from "../../../../../../generated/entities.ids.generated.js";
function CreateChannelPage() {
  const t = useT();
  const { fields, groups } = useChannelFields();
  const router = useRouter();
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
    CrudForm,
    {
      title: t("sales.channels.form.createTitle", "Create sales channel"),
      entityId: E.sales.sales_channel,
      fields,
      groups: [
        ...groups,
        { id: "custom", title: t("entities.customFields.title", "Custom Attributes"), column: 2, kind: "customFields" }
      ],
      initialValues: { isActive: true },
      submitLabel: t("sales.channels.form.createSubmit", "Create channel"),
      cancelHref: "/backend/sales/channels",
      onSubmit: async (values) => {
        const payload = buildChannelPayload(values);
        const customFields = collectCustomFieldValues(values);
        if (Object.keys(customFields).length) {
          payload.customFields = customFields;
        }
        await createCrud("sales/channels", payload, {
          errorMessage: t("sales.channels.form.errors.create", "Failed to create channel.")
        });
        flash(t("sales.channels.form.messages.created", "Channel created."), "success");
        router.push("/backend/sales/channels");
      }
    }
  ) }) });
}
export {
  CreateChannelPage as default
};
//# sourceMappingURL=page.js.map
