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 {
  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]/edit/index.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Heading, Input, 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 { useLinkQuery } from "@mercurjs/dashboard-shared";
import { jsx, jsxs } from "react/jsx-runtime";
var Schema = z.object({ sku: z.string().max(64).optional() });
var EditOfferVariantForm = ({ offer }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: { sku: offer.sku ?? "" },
    resolver: zodResolver(Schema)
  });
  const { mutateAsync, isPending } = useUpdateOffer(offer.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    const sku = values.sku?.trim();
    await mutateAsync(
      sku ? { sku } : {},
      {
        onSuccess: () => {
          toast.success(t("offers.variant.edit.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-edit-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: "sku",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
              /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("offers.fields.sku") }),
              /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { maxLength: 64, autoComplete: "off", ...field }) }),
              /* @__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 OfferVariantEditPage = () => {
  const { offer_id } = useParams();
  const { t } = useTranslation();
  const query = useLinkQuery("offer", OFFER_VARIANT_DETAIL_FIELDS);
  const { offer, isPending, isError, error } = useOffer(offer_id, query);
  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.offerVariant")}` }) }),
      /* @__PURE__ */ jsx(RouteDrawer.Description, { className: "sr-only", children: t("offers.edit.description") })
    ] }),
    ready && /* @__PURE__ */ jsx(EditOfferVariantForm, { offer })
  ] });
};
var Component = OfferVariantEditPage;
export {
  Component,
  OfferVariantEditPage
};
