import {
  transformNullableFormData
} from "./chunk-7RPRNVBS.js";
import {
  PRODUCT_DETAIL_QUERY
} from "./chunk-35NDMIYZ.js";
import "./chunk-SMQT3ZNO.js";
import {
  SwitchBox
} from "./chunk-2EME4AWX.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  useDocumentDirection
} from "./chunk-Y7QKP6QU.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useProduct,
  useUpdateProduct
} from "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/products/product-edit/components/edit-product-form/edit-product-form.tsx
import { Button, Input, Select, Text, Textarea, toast } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditProductSchema = zod.object({
  status: zod.enum(["draft", "published", "proposed", "rejected"]),
  title: zod.string().min(1),
  subtitle: zod.string().optional(),
  handle: zod.string().min(1),
  material: zod.string().optional(),
  description: zod.string().optional(),
  discountable: zod.boolean()
});
var EditProductForm = ({ product }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const direction = useDocumentDirection();
  const form = useForm({
    defaultValues: {
      status: product.status,
      title: product.title,
      material: product.material || "",
      subtitle: product.subtitle || "",
      handle: product.handle || "",
      description: product.description || "",
      discountable: product.discountable
    },
    resolver: zodResolver(EditProductSchema)
  });
  const { mutateAsync, isPending } = useUpdateProduct(product.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    const { title, discountable, handle, status, ...optional } = data;
    const nullableData = transformNullableFormData(optional);
    await mutateAsync(
      {
        title,
        discountable,
        handle,
        status,
        ...nullableData
      },
      {
        onSuccess: ({ product: product2 }) => {
          toast.success(
            t("products.edit.successToast", { title: product2.title })
          );
          handleSuccess();
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "product-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "product-edit-keybound-form",
      children: [
        /* @__PURE__ */ jsx(
          RouteDrawer.Body,
          {
            className: "flex flex-1 flex-col gap-y-8 overflow-y-auto",
            "data-testid": "product-edit-form-body",
            children: /* @__PURE__ */ jsxs(
              "div",
              {
                className: "flex flex-col gap-y-8",
                "data-testid": "product-edit-form-fields",
                children: [
                  /* @__PURE__ */ jsxs(
                    "div",
                    {
                      className: "flex flex-col gap-y-4",
                      "data-testid": "product-edit-form-main-fields",
                      children: [
                        /* @__PURE__ */ jsx(
                          Form.Field,
                          {
                            control: form.control,
                            name: "status",
                            render: ({ field: { onChange, ref, ...field } }) => {
                              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-form-status-item", children: [
                                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-edit-form-status-label", children: t("fields.status") }),
                                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-form-status-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-form-status-select-wrapper", children: /* @__PURE__ */ jsxs(
                                  Select,
                                  {
                                    dir: direction,
                                    ...field,
                                    onValueChange: onChange,
                                    "data-testid": "product-edit-form-status-select",
                                    children: [
                                      /* @__PURE__ */ jsx(
                                        Select.Trigger,
                                        {
                                          ref,
                                          "data-testid": "product-edit-form-status-trigger",
                                          children: /* @__PURE__ */ jsx(Select.Value, { "data-testid": "product-edit-form-status-value" })
                                        }
                                      ),
                                      /* @__PURE__ */ jsx(Select.Content, { "data-testid": "product-edit-form-status-content", children: [
                                        "draft",
                                        "published",
                                        "proposed",
                                        "rejected"
                                      ].map((status) => {
                                        return /* @__PURE__ */ jsx(
                                          Select.Item,
                                          {
                                            value: status,
                                            "data-testid": `product-edit-form-status-option-${status}`,
                                            children: t(`products.productStatus.${status}`)
                                          },
                                          status
                                        );
                                      }) })
                                    ]
                                  }
                                ) }) }),
                                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-form-status-error" })
                              ] });
                            }
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          Form.Field,
                          {
                            control: form.control,
                            name: "title",
                            render: ({ field }) => {
                              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-form-title-item", children: [
                                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-edit-form-title-label", children: t("fields.title") }),
                                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-form-title-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-form-title-input-wrapper", children: /* @__PURE__ */ jsx(
                                  Input,
                                  {
                                    ...field,
                                    "data-testid": "product-edit-form-title-input"
                                  }
                                ) }) }),
                                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-form-title-error" })
                              ] });
                            }
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          Form.Field,
                          {
                            control: form.control,
                            name: "subtitle",
                            render: ({ field }) => {
                              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-form-subtitle-item", children: [
                                /* @__PURE__ */ jsx(
                                  Form.Label,
                                  {
                                    optional: true,
                                    "data-testid": "product-edit-form-subtitle-label",
                                    children: t("fields.subtitle")
                                  }
                                ),
                                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-form-subtitle-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-form-subtitle-input-wrapper", children: /* @__PURE__ */ jsx(
                                  Input,
                                  {
                                    ...field,
                                    "data-testid": "product-edit-form-subtitle-input"
                                  }
                                ) }) }),
                                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-form-subtitle-error" })
                              ] });
                            }
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          Form.Field,
                          {
                            control: form.control,
                            name: "handle",
                            render: ({ field }) => {
                              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-form-handle-item", children: [
                                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-edit-form-handle-label", children: t("fields.handle") }),
                                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-form-handle-control", children: /* @__PURE__ */ jsxs(
                                  "div",
                                  {
                                    className: "relative",
                                    "data-testid": "product-edit-form-handle-wrapper",
                                    children: [
                                      /* @__PURE__ */ jsx(
                                        "div",
                                        {
                                          className: "absolute inset-y-0 left-0 z-10 flex w-8 items-center justify-center border-r",
                                          "data-testid": "product-edit-form-handle-prefix",
                                          children: /* @__PURE__ */ jsx(
                                            Text,
                                            {
                                              className: "text-ui-fg-muted",
                                              size: "small",
                                              leading: "compact",
                                              weight: "plus",
                                              "data-testid": "product-edit-form-handle-prefix-text",
                                              children: "/"
                                            }
                                          )
                                        }
                                      ),
                                      /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-form-handle-input-wrapper", children: /* @__PURE__ */ jsx(
                                        Input,
                                        {
                                          ...field,
                                          className: "pl-10",
                                          "data-testid": "product-edit-form-handle-input"
                                        }
                                      ) })
                                    ]
                                  }
                                ) }),
                                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-form-handle-error" })
                              ] });
                            }
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          Form.Field,
                          {
                            control: form.control,
                            name: "material",
                            render: ({ field }) => {
                              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-form-material-item", children: [
                                /* @__PURE__ */ jsx(
                                  Form.Label,
                                  {
                                    optional: true,
                                    "data-testid": "product-edit-form-material-label",
                                    children: t("fields.material")
                                  }
                                ),
                                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-form-material-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-form-material-input-wrapper", children: /* @__PURE__ */ jsx(
                                  Input,
                                  {
                                    ...field,
                                    "data-testid": "product-edit-form-material-input"
                                  }
                                ) }) }),
                                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-form-material-error" })
                              ] });
                            }
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          Form.Field,
                          {
                            control: form.control,
                            name: "description",
                            render: ({ field }) => {
                              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-form-description-item", children: [
                                /* @__PURE__ */ jsx(
                                  Form.Label,
                                  {
                                    optional: true,
                                    "data-testid": "product-edit-form-description-label",
                                    children: t("fields.description")
                                  }
                                ),
                                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-form-description-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-form-description-textarea-wrapper", children: /* @__PURE__ */ jsx(
                                  Textarea,
                                  {
                                    ...field,
                                    "data-testid": "product-edit-form-description-textarea"
                                  }
                                ) }) }),
                                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-form-description-error" })
                              ] });
                            }
                          }
                        )
                      ]
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    SwitchBox,
                    {
                      control: form.control,
                      name: "discountable",
                      label: t("fields.discountable"),
                      description: t("products.discountableHint"),
                      "data-testid": "product-edit-form-discountable-switch"
                    }
                  )
                ]
              }
            )
          }
        ),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "product-edit-form-footer", children: /* @__PURE__ */ jsxs(
          "div",
          {
            className: "flex items-center justify-end gap-x-2",
            "data-testid": "product-edit-form-footer-actions",
            children: [
              /* @__PURE__ */ jsx(
                RouteDrawer.Close,
                {
                  asChild: true,
                  "data-testid": "product-edit-form-cancel-button-wrapper",
                  children: /* @__PURE__ */ jsx(
                    Button,
                    {
                      size: "small",
                      variant: "secondary",
                      "data-testid": "product-edit-form-cancel-button",
                      children: t("actions.cancel")
                    }
                  )
                }
              ),
              /* @__PURE__ */ jsx(
                Button,
                {
                  size: "small",
                  type: "submit",
                  isLoading: isPending,
                  "data-testid": "product-edit-form-save-button",
                  children: t("actions.save")
                }
              )
            ]
          }
        ) })
      ]
    }
  ) });
};

// src/pages/products/product-edit/product-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ProductEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { product, isLoading, isError, error } = useProduct(id, PRODUCT_DETAIL_QUERY);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "product-edit-drawer", children: [
    /* @__PURE__ */ jsxs2(RouteDrawer.Header, { "data-testid": "product-edit-drawer-header", children: [
      /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, "data-testid": "product-edit-drawer-title", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "product-edit-drawer-title-text", children: t("products.edit.header") }) }),
      /* @__PURE__ */ jsx2(
        RouteDrawer.Description,
        {
          className: "sr-only",
          "data-testid": "product-edit-drawer-description",
          children: t("products.edit.description")
        }
      )
    ] }),
    !isLoading && product && /* @__PURE__ */ jsx2(EditProductForm, { product })
  ] });
};
export {
  ProductEdit as Component
};
