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

// src/pages/customers/customer-create/components/create-customer-form/create-customer-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Heading, Input, Text, 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 CreateCustomerSchema = zod.object({
  email: zod.string().email(),
  first_name: zod.string().optional(),
  last_name: zod.string().optional(),
  company_name: zod.string().optional(),
  phone: zod.string().optional()
});
var CreateCustomerForm = () => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const { mutateAsync, isPending } = useCreateCustomer();
  const form = useForm({
    defaultValues: {
      email: "",
      first_name: "",
      last_name: "",
      phone: "",
      company_name: ""
    },
    resolver: zodResolver(CreateCustomerSchema)
  });
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        email: data.email,
        first_name: data.first_name || void 0,
        last_name: data.last_name || void 0,
        company_name: data.company_name || void 0,
        phone: data.phone || void 0
      },
      {
        onSuccess: ({ customer }) => {
          toast.success(
            t("customers.create.successToast", {
              email: customer.email
            })
          );
          handleSuccess(`/customers/${customer.id}`);
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "create-customer-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "create-customer-form-keybound",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "create-customer-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col items-center overflow-y-auto py-16", "data-testid": "create-customer-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8", "data-testid": "create-customer-form-content", children: [
          /* @__PURE__ */ jsxs("div", { "data-testid": "create-customer-form-header-section", children: [
            /* @__PURE__ */ jsx(Heading, { "data-testid": "create-customer-form-title", children: t("customers.create.header") }),
            /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "create-customer-form-hint", children: t("customers.create.hint") })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", "data-testid": "create-customer-form-fields", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "first_name",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-form-first-name-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-form-first-name-label", children: t("fields.firstName") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-form-first-name-control", children: /* @__PURE__ */ jsx(Input, { autoComplete: "off", ...field, "data-testid": "create-customer-form-first-name-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-form-first-name-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "last_name",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-form-last-name-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-form-last-name-label", children: t("fields.lastName") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-form-last-name-control", children: /* @__PURE__ */ jsx(Input, { autoComplete: "off", ...field, "data-testid": "create-customer-form-last-name-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-form-last-name-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "email",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-form-email-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "create-customer-form-email-label", children: t("fields.email") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-form-email-control", children: /* @__PURE__ */ jsx(Input, { autoComplete: "off", ...field, "data-testid": "create-customer-form-email-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-form-email-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "company_name",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-form-company-name-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-form-company-name-label", children: t("fields.company") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-form-company-name-control", children: /* @__PURE__ */ jsx(Input, { autoComplete: "off", ...field, "data-testid": "create-customer-form-company-name-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-form-company-name-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "phone",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-form-phone-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-form-phone-label", children: t("fields.phone") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-form-phone-control", children: /* @__PURE__ */ jsx(Input, { autoComplete: "off", ...field, "data-testid": "create-customer-form-phone-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-form-phone-error" })
                  ] });
                }
              }
            )
          ] })
        ] }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "create-customer-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "create-customer-form-footer-actions", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "create-customer-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(
            Button,
            {
              size: "small",
              variant: "primary",
              type: "submit",
              isLoading: isPending,
              "data-testid": "create-customer-form-submit-button",
              children: t("actions.create")
            }
          )
        ] }) })
      ]
    }
  ) });
};

// src/pages/customers/customer-create/customer-create.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var CustomerCreate = () => {
  return /* @__PURE__ */ jsx2(RouteFocusModal, { children: /* @__PURE__ */ jsx2(CreateCustomerForm, {}) });
};
export {
  CustomerCreate as Component
};
