import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useUpdateFulfillmentSetServiceZone
} from "./chunk-YFRC6GEN.js";
import "./chunk-5TRY5IMZ.js";
import {
  useStockLocation
} from "./chunk-EPHVGD5Z.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/locations/location-service-zone-edit/location-service-zone-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/locations/location-service-zone-edit/components/edit-region-form/edit-service-zone-form.tsx
import { Button, InlineTip, Input, 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 EditServiceZoneSchema = zod.object({
  name: zod.string().min(1)
});
var EditServiceZoneForm = ({
  zone,
  fulfillmentSetId,
  locationId
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      name: zone.name
    }
  });
  const { mutateAsync, isPending: isLoading } = useUpdateFulfillmentSetServiceZone(fulfillmentSetId, zone.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        name: values.name
      },
      {
        onSuccess: () => {
          toast.success(
            t("stockLocations.serviceZones.edit.successToast", {
              name: values.name
            })
          );
          handleSuccess(`/settings/locations/${locationId}`);
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "location-service-zone-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex size-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex-1 overflow-auto", "data-testid": "location-service-zone-edit-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-8", children: [
          /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "location-service-zone-edit-form-name-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "location-service-zone-edit-form-name-label", children: t("fields.name") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "location-service-zone-edit-form-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "location-service-zone-edit-form-name-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "location-service-zone-edit-form-name-error" })
                ] });
              }
            }
          ) }),
          /* @__PURE__ */ jsx(InlineTip, { label: t("general.tip"), "data-testid": "location-service-zone-edit-form-tip", children: t("stockLocations.serviceZones.fields.tip") })
        ] }) }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "location-service-zone-edit-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "location-service-zone-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading, "data-testid": "location-service-zone-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/locations/location-service-zone-edit/location-service-zone-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var LocationServiceZoneEdit = () => {
  const { t } = useTranslation2();
  const { location_id, fset_id, zone_id } = useParams();
  const { stock_location, isPending, isFetching, isError, error } = useStockLocation(location_id, {
    fields: "*fulfillment_sets.service_zones"
  });
  const serviceZone = stock_location?.fulfillment_sets?.find((f) => f.id === fset_id)?.service_zones.find((z) => z.id === zone_id);
  if (!isPending && !isFetching && !serviceZone) {
    throw new Response(
      JSON.stringify({ message: `Service zone with ID ${zone_id} was not found` }),
      { status: 404, headers: { "Content-Type": "application/json" } }
    );
  }
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { prev: `/settings/locations/${location_id}`, "data-testid": "location-service-zone-edit-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "location-service-zone-edit-drawer-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "location-service-zone-edit-drawer-heading", children: t("stockLocations.serviceZones.edit.header") }) }),
    serviceZone && /* @__PURE__ */ jsx2(
      EditServiceZoneForm,
      {
        zone: serviceZone,
        fulfillmentSetId: fset_id,
        locationId: location_id
      }
    )
  ] });
};
export {
  LocationServiceZoneEdit as Component
};
