import "./chunk-NXDFDEKP.js";
import {
  CountrySelect
} from "./chunk-V74Y5NIV.js";
import "./chunk-JRTZWK77.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCreateCustomerAddress
} from "./chunk-PRBE5CM3.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/customers/customer-create-address/components/create-customer-address-form/create-customer-address-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 { useParams } from "react-router-dom";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var CreateCustomerAddressSchema = zod.object({
  address_name: zod.string().min(1),
  address_1: zod.string().min(1),
  address_2: zod.string().optional(),
  country_code: zod.string().min(2).max(2),
  city: zod.string().optional(),
  postal_code: zod.string().optional(),
  province: zod.string().optional(),
  company: zod.string().optional(),
  phone: zod.string().optional()
});
var CreateCustomerAddressForm = () => {
  const { t } = useTranslation();
  const { id } = useParams();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      address_name: "",
      address_1: "",
      address_2: "",
      city: "",
      company: "",
      country_code: "",
      phone: "",
      postal_code: "",
      province: ""
    },
    resolver: zodResolver(CreateCustomerAddressSchema)
  });
  const { mutateAsync, isPending } = useCreateCustomerAddress(id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        address_name: values.address_name,
        address_1: values.address_1,
        address_2: values.address_2,
        country_code: values.country_code,
        city: values.city,
        postal_code: values.postal_code,
        province: values.province,
        company: values.company,
        phone: values.phone
      },
      {
        onSuccess: () => {
          toast.success(t("customers.addresses.create.successToast"));
          handleSuccess(`/customers/${id}`);
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "create-customer-address-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      "data-testid": "create-customer-address-form-keybound",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "create-customer-address-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", "data-testid": "create-customer-address-form-body", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", "data-testid": "create-customer-address-form-body-content", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8 px-2 py-16", "data-testid": "create-customer-address-form-content", children: [
          /* @__PURE__ */ jsxs("div", { "data-testid": "create-customer-address-form-header-section", children: [
            /* @__PURE__ */ jsx(Heading, { className: "capitalize", "data-testid": "create-customer-address-form-title", children: t("customers.addresses.create.header") }),
            /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "create-customer-address-form-hint", children: t("customers.addresses.create.hint") })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 gap-4", "data-testid": "create-customer-address-form-fields-row-1", children: /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "address_name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-address-name-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "create-customer-address-form-address-name-label", children: t("customers.addresses.fields.addressName") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-address-name-control", children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      size: "small",
                      autoComplete: "address_name",
                      ...field,
                      "data-testid": "create-customer-address-form-address-name-input"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-address-name-error" })
                ] });
              }
            }
          ) }),
          /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", "data-testid": "create-customer-address-form-fields-row-2", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address_1",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-address-1-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "create-customer-address-form-address-1-label", children: t("fields.address") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-address-1-control", children: /* @__PURE__ */ jsx(
                      Input,
                      {
                        size: "small",
                        autoComplete: "address_1",
                        ...field,
                        "data-testid": "create-customer-address-form-address-1-input"
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-address-1-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address_2",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-address-2-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-address-form-address-2-label", children: t("fields.address2") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-address-2-control", children: /* @__PURE__ */ jsx(
                      Input,
                      {
                        size: "small",
                        autoComplete: "address_2",
                        ...field,
                        "data-testid": "create-customer-address-form-address-2-input"
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-address-2-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "postal_code",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-postal-code-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-address-form-postal-code-label", children: t("fields.postalCode") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-postal-code-control", children: /* @__PURE__ */ jsx(
                      Input,
                      {
                        size: "small",
                        autoComplete: "postal_code",
                        ...field,
                        "data-testid": "create-customer-address-form-postal-code-input"
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-postal-code-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "city",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-city-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-address-form-city-label", children: t("fields.city") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-city-control", children: /* @__PURE__ */ jsx(Input, { size: "small", autoComplete: "city", ...field, "data-testid": "create-customer-address-form-city-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-city-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "country_code",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-country-code-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "create-customer-address-form-country-code-label", children: t("fields.country") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-country-code-control", children: /* @__PURE__ */ jsx(
                      CountrySelect,
                      {
                        autoComplete: "country_code",
                        ...field,
                        "data-testid": "create-customer-address-form-country-code-select"
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-country-code-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "province",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-province-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-address-form-province-label", children: t("fields.state") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-province-control", children: /* @__PURE__ */ jsx(
                      Input,
                      {
                        size: "small",
                        autoComplete: "province",
                        ...field,
                        "data-testid": "create-customer-address-form-province-input"
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-province-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "company",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-company-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-address-form-company-label", children: t("fields.company") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-company-control", children: /* @__PURE__ */ jsx(
                      Input,
                      {
                        size: "small",
                        autoComplete: "company",
                        ...field,
                        "data-testid": "create-customer-address-form-company-input"
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-company-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "phone",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "create-customer-address-form-phone-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "create-customer-address-form-phone-label", children: t("fields.phone") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "create-customer-address-form-phone-control", children: /* @__PURE__ */ jsx(Input, { size: "small", autoComplete: "phone", ...field, "data-testid": "create-customer-address-form-phone-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "create-customer-address-form-phone-error" })
                  ] });
                }
              }
            )
          ] })
        ] }) }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "create-customer-address-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "create-customer-address-form-footer-actions", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "create-customer-address-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading: isPending, "data-testid": "create-customer-address-form-submit-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

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