import {
  OFFER_VARIANT_DETAIL_FIELDS
} from "./chunk-XQUF3WIS.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useDocumentDirection
} from "./chunk-Y7QKP6QU.js";
import {
  useShippingProfiles
} from "./chunk-EPSDO2MJ.js";
import {
  useOffer,
  useUpdateOffer
} from "./chunk-5DKUSZQ7.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/offers/[id]/variants/[offer_id]/shipping/index.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Heading, Select, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var Schema = z.object({ shipping_profile_id: z.string().min(1) });
var EditShippingForm = ({ offer }) => {
  const { t } = useTranslation();
  const direction = useDocumentDirection();
  const { handleSuccess } = useRouteModal();
  const { shipping_profiles } = useShippingProfiles({ limit: 1e3 });
  const form = useForm({
    defaultValues: { shipping_profile_id: offer.shipping_profile_id ?? "" },
    resolver: zodResolver(Schema)
  });
  const { mutateAsync, isPending } = useUpdateOffer(offer.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      { shipping_profile_id: values.shipping_profile_id },
      {
        onSuccess: () => {
          toast.success(t("offers.variant.shipping.successToast"));
          handleSuccess();
        },
        onError: (error) => toast.error(error.message)
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "offer-variant-shipping-form",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-4 overflow-auto", children: /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "shipping_profile_id",
            render: ({ field: { ref, onChange, ...f } }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
              /* @__PURE__ */ jsx(Form.Label, { children: t("offers.fields.shippingProfile") }),
              /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(Select, { ...f, onValueChange: onChange, dir: direction, children: [
                /* @__PURE__ */ jsx(
                  Select.Trigger,
                  {
                    ref,
                    "data-testid": "offer-variant-shipping-select",
                    children: /* @__PURE__ */ jsx(
                      Select.Value,
                      {
                        placeholder: t("offers.fields.shippingProfile")
                      }
                    )
                  }
                ),
                /* @__PURE__ */ jsx(Select.Content, { children: (shipping_profiles ?? []).map((p) => /* @__PURE__ */ jsx(Select.Item, { value: p.id, children: p.name ?? "" }, p.id)) })
              ] }) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, {})
            ] })
          }
        ) }),
        /* @__PURE__ */ jsx(RouteDrawer.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", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};
var OfferVariantShippingPage = () => {
  const { offer_id } = useParams();
  const { t } = useTranslation();
  const { offer, isPending, isError, error } = useOffer(offer_id, {
    fields: OFFER_VARIANT_DETAIL_FIELDS
  });
  if (isError) throw error;
  const ready = !isPending && !!offer;
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
    /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
      /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: `${t("actions.edit")} ${t("offers.detail.shippingConfiguration")}` }) }),
      /* @__PURE__ */ jsx(RouteDrawer.Description, { className: "sr-only", children: t("offers.edit.description") })
    ] }),
    ready && /* @__PURE__ */ jsx(EditShippingForm, { offer })
  ] });
};
var Component = OfferVariantShippingPage;
export {
  Component,
  OfferVariantShippingPage
};
