import {
  GeoZoneForm
} from "./chunk-SDPR7PGR.js";
import "./chunk-TUJIAA5O.js";
import {
  GEO_ZONE_STACKED_MODAL_ID
} from "./chunk-LXSBG6LK.js";
import "./chunk-JRTZWK77.js";
import "./chunk-FQERBIVS.js";
import "./chunk-ULJ3OMWU.js";
import "./chunk-YBZWO4ZV.js";
import "./chunk-KAZ5BYTQ.js";
import "./chunk-3EF54XFY.js";
import "./chunk-IGTRIAUC.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import "./chunk-INNFZYX2.js";
import "./chunk-Y7QKP6QU.js";
import "./chunk-KBYZLKI4.js";
import "./chunk-YWWUOGJA.js";
import "./chunk-THHRRYRS.js";
import "./chunk-UZWFZMOX.js";
import "./chunk-XQNVC4Q7.js";
import {
  RouteFocusModal,
  StackedFocusModal,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCreateFulfillmentSetServiceZone
} from "./chunk-TPPONKYW.js";
import "./chunk-O4STPJR2.js";
import {
  useStockLocation
} from "./chunk-OFKUNYQJ.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/locations/location-service-zone-create/location-service-zone-create.tsx
import { useParams } from "react-router-dom";

// src/pages/locations/location-service-zone-create/components/create-service-zone-form/create-service-zone-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Heading, InlineTip, Input, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var CreateServiceZoneSchema = z.object({
  name: z.string().min(1),
  countries: z.array(z.object({ iso_2: z.string().min(2), display_name: z.string() })).min(1)
});
function CreateServiceZoneForm({
  fulfillmentSet,
  type,
  location
}) {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      name: "",
      countries: []
    },
    resolver: zodResolver(CreateServiceZoneSchema)
  });
  const { mutateAsync, isPending } = useCreateFulfillmentSetServiceZone(
    fulfillmentSet.id
  );
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        name: data.name,
        geo_zones: data.countries.map(({ iso_2 }) => ({
          country_code: iso_2,
          type: "country"
        }))
      },
      {
        onSuccess: () => {
          toast.success(
            t("stockLocations.serviceZones.create.successToast", {
              name: data.name
            })
          );
          handleSuccess(`/settings/locations/${location.id}`);
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "location-service-zone-create-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      className: "flex h-full flex-col overflow-hidden",
      onSubmit: handleSubmit,
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "location-service-zone-create-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col items-center overflow-auto", "data-testid": "location-service-zone-create-form-body", children: /* @__PURE__ */ jsxs(StackedFocusModal, { id: GEO_ZONE_STACKED_MODAL_ID, "data-testid": "location-service-zone-create-form-stacked-modal", children: [
          /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8 px-2 py-16", children: [
            /* @__PURE__ */ jsx(Heading, { "data-testid": "location-service-zone-create-form-heading", children: type === "pickup" /* Pickup */ ? t("stockLocations.serviceZones.create.headerPickup", {
              location: location.name
            }) : t("stockLocations.serviceZones.create.headerShipping", {
              location: location.name
            }) }),
            /* @__PURE__ */ jsx("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", children: /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "name",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-service-zone-create-form-name-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "location-service-zone-create-form-name-label", children: t("fields.name") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-service-zone-create-form-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "location-service-zone-create-form-name-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-service-zone-create-form-name-error" })
                  ] });
                }
              }
            ) }),
            /* @__PURE__ */ jsx(InlineTip, { label: t("general.tip"), "data-testid": "location-service-zone-create-form-tip", children: t("stockLocations.serviceZones.fields.tip") }),
            /* @__PURE__ */ jsx(GeoZoneForm, { form })
          ] }) }),
          /* @__PURE__ */ jsx(GeoZoneForm.AreaDrawer, { form })
        ] }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "location-service-zone-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, { variant: "secondary", size: "small", "data-testid": "location-service-zone-create-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading: isPending, "data-testid": "location-service-zone-create-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
}

// src/pages/locations/location-service-zone-create/location-service-zone-create.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
function LocationCreateServiceZone() {
  const { fset_id, location_id } = useParams();
  const { stock_location, isPending, isFetching, isError, error } = useStockLocation(location_id, {
    fields: "*fulfillment_sets"
  });
  const fulfillmentSet = stock_location?.fulfillment_sets?.find(
    (f) => f.id === fset_id
  );
  const type = fulfillmentSet?.type === "pickup" /* Pickup */ ? "pickup" /* Pickup */ : "shipping" /* Shipping */;
  if (!isPending && !isFetching && !fulfillmentSet) {
    throw new Response(
      JSON.stringify({ message: `Fulfillment set with ID: ${fset_id} was not found.` }),
      { status: 404, headers: { "Content-Type": "application/json" } }
    );
  }
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx2(RouteFocusModal, { prev: `/settings/locations/${location_id}`, "data-testid": "location-service-zone-create-modal", children: fulfillmentSet && /* @__PURE__ */ jsx2(
    CreateServiceZoneForm,
    {
      fulfillmentSet,
      location: stock_location,
      type
    }
  ) });
}
export {
  LocationCreateServiceZone as Component
};
