import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useShippingOptionType,
  useUpdateShippingOptionType
} from "./chunk-PBQ5M44A.js";
import "./chunk-OH4AABUT.js";
import "./chunk-YKDEE7ZL.js";
import "./chunk-AEMDNTW5.js";
import "./chunk-LYD3C73Z.js";
import "./chunk-7O5NDICV.js";
import "./chunk-46M463BR.js";
import "./chunk-YFRC6GEN.js";
import "./chunk-N6SI4IIT.js";
import "./chunk-JPNPTILP.js";
import "./chunk-DAXXLQKB.js";
import "./chunk-OHOWEMHO.js";
import "./chunk-GKGLVDDV.js";
import "./chunk-WJJXUXSO.js";
import "./chunk-LAPEZATU.js";
import "./chunk-DBNPVU3L.js";
import "./chunk-U7WZYWGA.js";
import "./chunk-ZN7LWXVC.js";
import "./chunk-VYMODH6P.js";
import "./chunk-LEG67A2R.js";
import "./chunk-PRBE5CM3.js";
import "./chunk-5TRY5IMZ.js";
import "./chunk-TAKEQAZZ.js";
import "./chunk-B6FG7A6W.js";
import "./chunk-EPHVGD5Z.js";
import "./chunk-ESPLVOFC.js";
import "./chunk-FXWKGNRV.js";
import "./chunk-VDKOJ5WD.js";
import "./chunk-DL2ZIXBO.js";
import "./chunk-Z2QQZOG3.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/shipping-option-types/shipping-option-type-edit/shipping-option-type-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/shipping-option-types/shipping-option-type-edit/components/edit-shipping-option-type-form/edit-shipping-option-type-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, Text, 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 EditShippingOptionTypeSchema = z.object({
  label: z.string().min(1),
  code: z.string().min(1),
  description: z.string().optional()
});
var EditShippingOptionTypeForm = ({
  shippingOptionType
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      label: shippingOptionType.label,
      code: shippingOptionType.code,
      description: shippingOptionType.description
    },
    resolver: zodResolver(EditShippingOptionTypeSchema)
  });
  const { mutateAsync, isPending } = useUpdateShippingOptionType(
    shippingOptionType.id
  );
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        label: data.label,
        code: data.code,
        description: data.description
      },
      {
        onSuccess: ({ shipping_option_type }) => {
          toast.success(
            t("shippingOptionTypes.edit.successToast", {
              label: shipping_option_type.label
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "shipping-option-type-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", "data-testid": "shipping-option-type-edit-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "label",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "shipping-option-type-edit-form-label-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "shipping-option-type-edit-form-label-label", children: t("shippingOptionTypes.fields.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "shipping-option-type-edit-form-label-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "shipping-option-type-edit-form-label-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "shipping-option-type-edit-form-label-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "code",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "shipping-option-type-edit-form-code-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "shipping-option-type-edit-form-code-label", children: t("shippingOptionTypes.fields.code") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "shipping-option-type-edit-form-code-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "shipping-option-type-edit-form-code-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "shipping-option-type-edit-form-code-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "description",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "shipping-option-type-edit-form-description-item", children: [
                  /* @__PURE__ */ jsxs(Form.Label, { "data-testid": "shipping-option-type-edit-form-description-label", children: [
                    t("shippingOptionTypes.fields.description"),
                    /* @__PURE__ */ jsxs(
                      Text,
                      {
                        size: "small",
                        leading: "compact",
                        className: "text-ui-fg-muted ml-1 inline",
                        children: [
                          "(",
                          t("fields.optional"),
                          ")"
                        ]
                      }
                    )
                  ] }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "shipping-option-type-edit-form-description-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "shipping-option-type-edit-form-description-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "shipping-option-type-edit-form-description-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "shipping-option-type-edit-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "shipping-option-type-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "shipping-option-type-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/shipping-option-types/shipping-option-type-edit/shipping-option-type-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ShippingOptionTypeEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { shipping_option_type, isPending, isError, error } = useShippingOptionType(id);
  const ready = !isPending && !!shipping_option_type;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "shipping-option-type-edit-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "shipping-option-type-edit-drawer-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "shipping-option-type-edit-drawer-heading", children: t("shippingOptionTypes.edit.header") }) }),
    ready && /* @__PURE__ */ jsx2(EditShippingOptionTypeForm, { shippingOptionType: shipping_option_type })
  ] });
};
export {
  ShippingOptionTypeEdit as Component
};
