import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCreateSalesChannel
} from "./chunk-N4JU7GWK.js";
import "./chunk-BA2HXHTW.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/sales-channels/sales-channel-create/components/create-sales-channel-form/create-sales-channel-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import {
  Button,
  Heading,
  Input,
  Switch,
  Text,
  Textarea,
  toast
} from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var CreateSalesChannelSchema = zod.object({
  name: zod.string().min(1),
  description: zod.string().min(1),
  enabled: zod.boolean()
});
var CreateSalesChannelForm = () => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      name: "",
      description: "",
      enabled: true
    },
    resolver: zodResolver(CreateSalesChannelSchema)
  });
  const { mutateAsync, isPending } = useCreateSalesChannel();
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        name: values.name,
        description: values.description,
        is_disabled: !values.enabled
      },
      {
        onSuccess: ({ sales_channel }) => {
          toast.success(t("salesChannels.toast.create"));
          handleSuccess(`../${sales_channel.id}`);
        },
        onError: (error) => toast.error(error.message)
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "sales-channel-create-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "sales-channel-create-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", "data-testid": "sales-channel-create-form-body", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8 px-2 py-16", children: [
          /* @__PURE__ */ jsxs("div", { "data-testid": "sales-channel-create-form-header-section", children: [
            /* @__PURE__ */ jsx(Heading, { className: "capitalize", "data-testid": "sales-channel-create-form-heading", children: t("salesChannels.createSalesChannel") }),
            /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "sales-channel-create-form-hint", children: t("salesChannels.createSalesChannelHint") })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
            /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2", children: /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "name",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "sales-channel-create-form-name-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "sales-channel-create-form-name-label", children: t("fields.name") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "sales-channel-create-form-name-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "sales-channel-create-form-name-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "sales-channel-create-form-name-error" })
                  ] });
                }
              }
            ) }),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "description",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "sales-channel-create-form-description-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "sales-channel-create-form-description-label", children: t("fields.description") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "sales-channel-create-form-description-control", children: /* @__PURE__ */ jsx(Textarea, { ...field, "data-testid": "sales-channel-create-form-description-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "sales-channel-create-form-description-error" })
                  ] });
                }
              }
            )
          ] }),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "enabled",
              render: ({ field: { value, onChange, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "sales-channel-create-form-enabled-item", children: [
                  /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "sales-channel-create-form-enabled-label", children: t("general.enabled") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "sales-channel-create-form-enabled-control", children: /* @__PURE__ */ jsx(
                      Switch,
                      {
                        dir: "ltr",
                        className: "rtl:rotate-180",
                        ...field,
                        checked: value,
                        onCheckedChange: onChange,
                        "data-testid": "sales-channel-create-form-enabled-switch"
                      }
                    ) })
                  ] }),
                  /* @__PURE__ */ jsx(Form.Hint, { "data-testid": "sales-channel-create-form-enabled-hint", children: t("salesChannels.enabledHint") }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "sales-channel-create-form-enabled-error" })
                ] });
              }
            }
          )
        ] }) }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "sales-channel-create-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "sales-channel-create-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "sales-channel-create-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/sales-channels/sales-channel-create/sales-channel-create.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var SalesChannelCreate = () => {
  return /* @__PURE__ */ jsx2(RouteFocusModal, { "data-testid": "sales-channel-create-modal", children: /* @__PURE__ */ jsx2(CreateSalesChannelForm, {}) });
};
export {
  SalesChannelCreate as Component
};
