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 {
  useCreateStockLocation
} from "./chunk-EPHVGD5Z.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/locations/location-create/components/create-location-form/create-location-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 CreateLocationSchema = zod.object({
  name: zod.string().min(1),
  address: zod.object({
    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 CreateLocationForm = () => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      name: "",
      address: {
        address_1: "",
        address_2: "",
        city: "",
        company: "",
        country_code: "",
        phone: "",
        postal_code: "",
        province: ""
      }
    },
    resolver: zodResolver(CreateLocationSchema)
  });
  const { mutateAsync, isPending } = useCreateStockLocation();
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        name: values.name,
        address: values.address
      },
      {
        onSuccess: ({ stock_location }) => {
          toast.success(t("locations.toast.create"));
          handleSuccess(`/settings/locations/${stock_location.id}`);
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "location-create-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "location-create-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", "data-testid": "location-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": "location-create-form-header-section", children: [
            /* @__PURE__ */ jsx(Heading, { className: "capitalize", "data-testid": "location-create-form-heading", children: t("stockLocations.create.header") }),
            /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "location-create-form-hint", children: t("stockLocations.create.hint") })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 gap-4", children: /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-name-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "location-create-form-name-label", children: t("fields.name") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-name-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-name-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-name-error" })
                ] });
              }
            }
          ) }),
          /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", "data-testid": "location-create-form-address-fields", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.address_1",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-address-1-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "location-create-form-address-1-label", children: t("fields.address") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-address-1-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-address-1-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-address-1-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.address_2",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-address-2-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "location-create-form-address-2-label", children: t("fields.address2") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-address-2-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-address-2-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-address-2-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.postal_code",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-postal-code-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "location-create-form-postal-code-label", children: t("fields.postalCode") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-postal-code-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-postal-code-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-postal-code-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.city",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-city-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "location-create-form-city-label", children: t("fields.city") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-city-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-city-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-city-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.country_code",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-country-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "location-create-form-country-label", children: t("fields.country") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-country-control", children: /* @__PURE__ */ jsx(CountrySelect, { ...field, "data-testid": "location-create-form-country-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-country-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.province",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-province-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "location-create-form-province-label", children: t("fields.state") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-province-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-province-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-province-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.company",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-company-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "location-create-form-company-label", children: t("fields.company") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-company-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-company-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-company-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "address.phone",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-create-form-phone-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "location-create-form-phone-label", children: t("fields.phone") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-create-form-phone-control", children: /* @__PURE__ */ jsx(Input, { size: "small", ...field, "data-testid": "location-create-form-phone-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-create-form-phone-error" })
                  ] });
                }
              }
            )
          ] })
        ] }) }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "location-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": "location-create-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading: isPending, "data-testid": "location-create-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

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