import {
  HandleInput
} from "./chunk-HJ56YXBU.js";
import {
  SwitchBox
} from "./chunk-YVCBMOJG.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-RLRWDHME.js";
import "./chunk-K5324KIX.js";
import "./chunk-FV76G2D7.js";
import "./chunk-GHDLST2P.js";
import "./chunk-YVYYOBAE.js";
import "./chunk-L2NJPDUD.js";
import "./chunk-P7UBW2EI.js";
import "./chunk-MR6REQKT.js";
import "./chunk-EPSDO2MJ.js";
import "./chunk-BHEU34FL.js";
import "./chunk-W34KDYZA.js";
import "./chunk-II3CLUXA.js";
import "./chunk-XKEYHWBS.js";
import "./chunk-J2XUEO2E.js";
import "./chunk-656G74I2.js";
import "./chunk-VWPNMEOE.js";
import "./chunk-O72AIZUN.js";
import "./chunk-VUWAOX7H.js";
import "./chunk-5DKUSZQ7.js";
import {
  useCreateOrderShipment,
  useOrder
} from "./chunk-DIAWPVYS.js";
import "./chunk-4U2BLH45.js";
import "./chunk-YAUGB4LD.js";
import "./chunk-A5DJIOHN.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/orders/[id]/shipment/index.tsx
import { useParams } from "react-router-dom";

// src/pages/orders/[id]/shipment/order-create-shipment-form/order-create-shipment-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { useTranslation } from "react-i18next";
import { Button, Heading, Input, toast } from "@medusajs/ui";
import { useFieldArray, useForm } from "react-hook-form";

// src/pages/orders/[id]/shipment/order-create-shipment-form/constants.ts
import { z } from "zod";
var CreateShipmentSchema = z.object({
  labels: z.array(
    z.object({
      tracking_number: z.string(),
      tracking_url: z.string().optional()
    })
  ),
  notify: z.boolean().optional()
});

// src/pages/orders/[id]/shipment/order-create-shipment-form/order-create-shipment-form.tsx
import { jsx, jsxs } from "react/jsx-runtime";
function OrderCreateShipmentForm({
  order,
  fulfillment
}) {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const { mutateAsync: createShipment, isPending: isMutating } = useCreateOrderShipment(order.id, fulfillment?.id);
  const form = useForm({
    defaultValues: {
      labels: [{ tracking_number: "", tracking_url: "" }],
      notify: false
    },
    resolver: zodResolver(CreateShipmentSchema)
  });
  const { fields: labels, append } = useFieldArray({
    name: "labels",
    control: form.control
  });
  const handleSubmit = form.handleSubmit(async (data) => {
    await createShipment(
      {
        items: fulfillment?.items?.map((i) => ({ id: i?.line_item_id, quantity: i.quantity })).filter((item) => !!item.id) ?? [],
        labels: data.labels.filter((l) => !!l.tracking_number).map((l) => ({
          tracking_number: l.tracking_number,
          tracking_url: l.tracking_url ?? "",
          label_url: ""
        }))
      },
      {
        onSuccess: () => {
          toast.success(t("orders.shipment.toastCreated"));
          handleSuccess(`/orders/${order.id}`);
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-col items-center overflow-y-auto p-16", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[736px] flex-col gap-8", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
            /* @__PURE__ */ jsx(Heading, { children: t("orders.shipment.title") }),
            /* @__PURE__ */ jsx(
              Button,
              {
                type: "button",
                size: "small",
                variant: "secondary",
                onClick: () => append({
                  tracking_number: "",
                  tracking_url: ""
                }),
                "data-testid": "shipment-add-tracking",
                children: t("orders.shipment.addTracking")
              }
            )
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-8 divide-y divide-dashed", children: [
            labels.map((label, index) => /* @__PURE__ */ jsxs(
              "div",
              {
                className: "flex flex-col gap-y-4 [&:not(:first-child)]:pt-8",
                children: [
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: `labels.${index}.tracking_url`,
                      render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                        /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("orders.shipment.trackingUrl") }),
                        /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                          HandleInput,
                          {
                            ...field,
                            value: field.value ?? "",
                            prefix: "/",
                            placeholder: t(
                              "orders.shipment.trackingUrlPlaceholder"
                            ),
                            "data-testid": `shipment-tracking-url-${index}`
                          }
                        ) }),
                        /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                      ] })
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: `labels.${index}.tracking_number`,
                      render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                        /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("orders.shipment.trackingNumber") }),
                        /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                          Input,
                          {
                            ...field,
                            "data-testid": `shipment-tracking-number-${index}`
                          }
                        ) }),
                        /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                      ] })
                    }
                  )
                ]
              },
              label.id
            )),
            /* @__PURE__ */ jsx("div", { className: "pt-8", children: /* @__PURE__ */ jsx(
              SwitchBox,
              {
                control: form.control,
                name: "notify",
                label: t("orders.shipment.sendNotification"),
                description: t("orders.shipment.sendNotificationHint")
              }
            ) })
          ] })
        ] }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.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", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(
            Button,
            {
              size: "small",
              type: "submit",
              isLoading: isMutating,
              "data-testid": "shipment-confirm",
              children: t("actions.confirm")
            }
          )
        ] }) })
      ]
    }
  ) });
}

// src/pages/orders/[id]/shipment/index.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Component = () => {
  const { id, f_id } = useParams();
  const { order, isLoading, isError, error } = useOrder(id, {
    // Use `fulfillments.*` + `fulfillments.<rel>.*` rather than stacking
    // multiple `*fulfillments…` segments — the query parser reads
    // `*fulfillments.items` as a literal property on Order and 500s.
    fields: "fulfillments.*,fulfillments.items.*,fulfillments.labels.*"
  });
  if (isError) {
    throw error;
  }
  const ready = !isLoading && order;
  const fulfillment = order?.fulfillments?.find((f) => f.id === f_id);
  return /* @__PURE__ */ jsx2(RouteFocusModal, { children: ready && fulfillment && /* @__PURE__ */ jsx2(
    OrderCreateShipmentForm,
    {
      order,
      fulfillment
    }
  ) });
};
export {
  Component
};
