import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useProductType,
  useUpdateProductType
} from "./chunk-46M463BR.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/product-types/product-type-edit/components/edit-product-type-form/edit-product-type-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, 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 EditProductTypeSchema = z.object({
  value: z.string().min(1)
});
var EditProductTypeForm = ({
  productType
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      value: productType.value
    },
    resolver: zodResolver(EditProductTypeSchema)
  });
  const { mutateAsync, isPending } = useUpdateProductType(productType.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        value: data.value
      },
      {
        onSuccess: ({ product_type }) => {
          toast.success(
            t("productTypes.edit.successToast", {
              value: product_type.value
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "product-type-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", "data-testid": "product-type-edit-form-body", children: /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "value",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-type-edit-form-value-item", children: [
                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-type-edit-form-value-label", children: t("productTypes.fields.value") }),
                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-type-edit-form-value-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "product-type-edit-form-value-input" }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-type-edit-form-value-error" })
              ] });
            }
          }
        ) }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "product-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": "product-type-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "product-type-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/product-types/product-type-edit/product-type-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ProductTypeEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { product_type, isPending, isError, error } = useProductType(id);
  const ready = !isPending && !!product_type;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "product-type-edit-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "product-type-edit-drawer-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "product-type-edit-drawer-heading", children: t("productTypes.edit.header") }) }),
    ready && /* @__PURE__ */ jsx2(EditProductTypeForm, { productType: product_type })
  ] });
};
export {
  ProductTypeEdit as Component
};
